blob: 1860d55def82697e8a10105e77732a20b4a7a6e2 [file] [log] [blame] [view]
jbudorick900a8d832016-07-18 18:56:051# Android Studio
2
jbudorick900a8d832016-07-18 18:56:053[TOC]
4
5## Usage
6
wnwen9032b352017-02-09 15:53:337Make sure you have followed
8[android build instructions](android_build_instructions.md) already.
wnwene397fec62017-01-19 20:25:219
jbudorick900a8d832016-07-18 18:56:0510```shell
Andrew Grieve17b03142017-10-04 14:50:0911build/android/gradle/generate_gradle.py [--canary] # Use --canary for Android Studio 3.0 beta
jbudorick900a8d832016-07-18 18:56:0512```
13
wnwene397fec62017-01-19 20:25:2114This creates a project at `out/Debug/gradle`. To create elsewhere:
agrievee244c1a2016-09-22 19:35:1815
16```shell
wnwene397fec62017-01-19 20:25:2117build/android/gradle/generate_gradle.py --output-directory out/My-Out-Dir --project-dir my-project
agrievee244c1a2016-09-22 19:35:1818```
19
wnwen03427bc2017-04-19 19:22:2420By default, common targets are generated. To add more targets to generate
21projects for:
agrievee244c1a2016-09-22 19:35:1822
23```shell
wnwen03427bc2017-04-19 19:22:2424build/android/gradle/generate_gradle.py --extra-target //chrome/android:chrome_public_apk
agrievee244c1a2016-09-22 19:35:1825```
jbudorick900a8d832016-07-18 18:56:0526
agrieve0c28e4f2016-09-22 01:05:2027For first-time Android Studio users:
28
wnwend65703a2016-11-24 18:47:3129* Avoid running the setup wizard.
wnwen9032b352017-02-09 15:53:3330 * The wizard will force you to download unwanted SDK components to
31 `//third_party/android_tools`.
agrievec62a52d2016-09-22 01:48:2432 * To skip it, select "Cancel" when it comes up.
agrieve0c28e4f2016-09-22 01:05:2033
34To import the project:
35
wnwen9032b352017-02-09 15:53:3336* Use "Import Project", and select the directory containing the generated
wnwen0227e4a52017-02-09 21:14:5137 project, by default `out/Debug/gradle`.
agrieve0c28e4f2016-09-22 01:05:2038
Andrew Grieve17b03142017-10-04 14:50:0939If you're asked to use Studio's Android SDK:
40
41* No.
42
43If you're asked to use Studio's Gradle wrapper:
44
45* Yes.
46
agrieve0c28e4f2016-09-22 01:05:2047You need to re-run `generate_gradle.py` whenever `BUILD.gn` files change.
48
wnwen9032b352017-02-09 15:53:3349* After regenerating, Android Studio should prompt you to "Sync". If it
50 doesn't, use:
wnwen0227e4a52017-02-09 21:14:5151 * Button with two arrows on the right side of the top strip.
wnwend65703a2016-11-24 18:47:3152 * Help -> Find Action -> "Sync Project with Gradle Files"
wnwen0227e4a52017-02-09 21:14:5153 * After `gn clean` you may need to restart Android Studio.
agrieve0c28e4f2016-09-22 01:05:2054
wnwen03427bc2017-04-19 19:22:2455## How It Works
agrieve0c28e4f2016-09-22 01:05:2056
wnwene851ad472017-04-27 16:21:4257By default, only a single module is generated. If more than one apk target is
58specified, then an `_all` module is generated. Otherwise a single apk module is
59generated. Since instrumentation tests are combined with their `apk_under_test`
60target, they count as one module together.
61
62To see more detailed structure of gn targets, the `--split-projects` flag can
63be used. This will generate one module for every gn target in the dependency
64graph.
agrieve0c28e4f2016-09-22 01:05:2065
wnwen03427bc2017-04-19 19:22:2466### Excluded Files
agrieve0c28e4f2016-09-22 01:05:2067
wnwen03427bc2017-04-19 19:22:2468Gradle supports source directories but not source files. However, files in
69Chromium are used amongst multiple targets. To accommodate this, the script
70detects such targets and creates exclude patterns to exclude files not in the
71current target. The editor does not respect these exclude patterns, so a `_all`
72pseudo module is added which includes directories from all targets. This allows
73imports and refactorings to be searched across all targets.
74
75### Extracting .srcjars
agrieve0c28e4f2016-09-22 01:05:2076
77Most generated .java files in GN are stored as `.srcjars`. Android Studio does
wnwen03427bc2017-04-19 19:22:2478not support them, and so the generator script builds and extracts them all to
79`extracted-srcjars/` subdirectories for each target that contains them. This is
80the reason that the `_all` pseudo module may contain multiple copies of
81generated files.
agrieve0c28e4f2016-09-22 01:05:2082
83*** note
84** TLDR:** Always re-generate project files when `.srcjars` change (this
85includes `R.java`).
86***
87
jbudorick900a8d832016-07-18 18:56:0588## Android Studio Tips
89
Andrew Grieve17b03142017-10-04 14:50:0990* Using the Java debugger is documented at [android_debugging_instructions.md#android-studio](android_debugging_instructions.md#android-studio).
wnwen9032b352017-02-09 15:53:3391* Configuration instructions can be found
92 [here](http://tools.android.com/tech-docs/configuration). One suggestions:
93 * Launch it with more RAM:
94 `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-stable/bin/studio-launcher.sh`
wnwend65703a2016-11-24 18:47:3195* If you ever need to reset it: `rm -r ~/.AndroidStudio*/`
Boris Sazonova084e8f92017-09-14 12:29:1896* Import Chromium-specific style and inspections settings:
wnwen9032b352017-02-09 15:53:3397 * Help -> Find Action -> "Code Style" (settings) -> Java ->
Boris Sazonova084e8f92017-09-14 12:29:1898 Scheme -> Import Scheme
99 * Select `tools/android/android_studio/ChromiumStyle.xml` -> OK
100 * Help -> Find Action -> "Inspections" (settings) ->
101 Profile -> Import profile
102 * Select `tools/android/android_studio/ChromiumInspections.xml` -> OK
wnwend65703a2016-11-24 18:47:31103* Turn on automatic import:
104 * Help -> Find Action -> "Auto Import"
105 * Tick all the boxes under "Java" and change the dropdown to "All".
wnwen67b64972016-12-19 19:53:15106* Turn on documentation on mouse hover:
107 * Help -> Find Action -> "Show quick documentation on mouse move"
108* Turn on line numbers:
109 * Help -> Find Action -> "Show line numbers"
zpeng38bf00de2017-05-10 16:05:03110* Format changed files (Useful for changes made by running code inspection):
111 * Set up version control
112 * File -> Settings -> Version Control
113 * Add src directories
114 * Commit changes and reformat
115 * Help -> Find Action -> "Commit Changes"
116 * Check "Reformat code" & "Optimize imports" and commit
agrieve0c28e4f2016-09-22 01:05:20117
118### Useful Shortcuts
119
wnwend65703a2016-11-24 18:47:31120* `Shift - Shift`: Search to open file or perform IDE action
121* `Ctrl + N`: Jump to class
122* `Ctrl + Shift + T`: Jump to test
123* `Ctrl + Shift + N`: Jump to file
124* `Ctrl + F12`: Jump to method
125* `Ctrl + G`: Jump to line
126* `Shift + F6`: Rename variable
127* `Ctrl + Alt + O`: Organize imports
128* `Alt + Enter`: Quick Fix (use on underlined errors)
wnwenfdea62ce2017-04-05 13:12:54129* `F2`: Find next error
jbudorick900a8d832016-07-18 18:56:05130
agrievec62a52d2016-09-22 01:48:24131### Building from the Command Line
132
wnwen9032b352017-02-09 15:53:33133Gradle builds can be done from the command-line after importing the project
134into Android Studio (importing into the IDE causes the Gradle wrapper to be
135added). This wrapper can also be used to invoke gradle commands.
agrievec62a52d2016-09-22 01:48:24136
137 cd $GRADLE_PROJECT_DIR && bash gradlew
138
139The resulting artifacts are not terribly useful. They are missing assets,
140resources, native libraries, etc.
141
wnwen9032b352017-02-09 15:53:33142* Use a
143 [gradle daemon](https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html)
144 to speed up builds using the gradlew script:
145 * Add the line `org.gradle.daemon=true` to `~/.gradle/gradle.properties`,
146 creating it if necessary.
wnwen660b1312016-10-21 16:46:22147
Andrew Grieve17b03142017-10-04 14:50:09148## Status (as of Oct, 2017)
agrievec62a52d2016-09-22 01:48:24149
150### What works
151
Andrew Grieve17b03142017-10-04 14:50:09152* Android Studio v2.3, and v3.0 beta with `--canary` flag.
wnwen03427bc2017-04-19 19:22:24153* Java editing and gradle compile.
wnwene397fec62017-01-19 20:25:21154* Instrumentation tests included as androidTest.
155* Symlinks to existing .so files in jniLibs (doesn't generate them).
wnwen0227e4a52017-02-09 21:14:51156* Editing resource xml files.
estevenson8c9318ff2017-03-10 22:16:35157* Java debugging (see
wnwenfdea62ce2017-04-05 13:12:54158[here](/docs/android_debugging_instructions.md#Android-Studio)).
wnwen03427bc2017-04-19 19:22:24159* Import resolution and refactoring across all modules.
wnwene851ad472017-04-27 16:21:42160* Correct lint and AndroidManifest when only one target is specified.
agrievec62a52d2016-09-22 01:48:24161
162### What doesn't work (yet) ([crbug](https://bugs.chromium.org/p/chromium/issues/detail?id=620034))
163
wnwen03427bc2017-04-19 19:22:24164* Gradle being aware of assets.
wnwenfdea62ce2017-04-05 13:12:54165* Layout editor.
Andrew Grieve17b03142017-10-04 14:50:09166* Native code editing.
167* Having the "Make Project" button work correctly.