blob: 7a3f036ba9cae6a8071a0501414aad33533196a0 [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
Peter Wena2d96112018-05-10 20:29:4311build/android/gradle/generate_gradle.py --output-directory out/Debug
jbudorick900a8d832016-07-18 18:56:0512```
13
Peter Wencb519572018-05-16 17:39:1814The above commands create a project dir `gradle` under your output directory.
15Use `--project-dir <project-dir>` to change this.
Anthony Berent8d3ce022018-01-15 12:24:3616
Peter Wenefd17272018-05-23 18:51:4017To import the project:
18* Use "Import Project", and select the directory containing the generated
19 project, e.g. `out/Debug/gradle`.
Anthony Berent8d3ce022018-01-15 12:24:3620
Ken Rockot35028ca2019-05-30 18:50:4521See [android_test_instructions.md](testing/android_test_instructions.md#Using-Emulators)
Peter Wenefd17272018-05-23 18:51:4022for more information about building and running emulators.
agrieve0c28e4f2016-09-22 01:05:2023
Peter Wenf8cc36fe2019-01-09 17:52:3124If you're asked to use Studio's Android SDK: No.
Andrew Grieve17b03142017-10-04 14:50:0925
Peter Wenf8cc36fe2019-01-09 17:52:3126If you're asked to use Studio's Gradle wrapper: Yes.
Andrew Grieve17b03142017-10-04 14:50:0927
agrieve0c28e4f2016-09-22 01:05:2028You need to re-run `generate_gradle.py` whenever `BUILD.gn` files change.
29
Peter Wena2d96112018-05-10 20:29:4330Pass `--canary` or `--beta` to avoid the "A newer version of gradle is
31available" notification.
32
wnwen9032b352017-02-09 15:53:3333* After regenerating, Android Studio should prompt you to "Sync". If it
Peter Wena2d96112018-05-10 20:29:4334 doesn't, try some of the following options:
35 * File -&gt; "Sync Project with Gradle Files"
wnwen0227e4a52017-02-09 21:14:5136 * Button with two arrows on the right side of the top strip.
wnwend65703a2016-11-24 18:47:3137 * Help -&gt; Find Action -&gt; "Sync Project with Gradle Files"
wnwen0227e4a52017-02-09 21:14:5138 * After `gn clean` you may need to restart Android Studio.
Peter Wena2d96112018-05-10 20:29:4339 * File -&gt; "Invalidate Caches / Restart..."
agrieve0c28e4f2016-09-22 01:05:2040
Egor Pasko634a90a2021-06-24 17:31:1541## Troubleshooting
42
43If building the Gradle files in Android Studio prints an error like this:
44
45```
46Failed to find Platform SDK with path: platforms;android-31
47```
48
49Then it means the SDK is being updated to the new release. A workaround is to
50ask for an older SDK version in `args.gn`:
51
52```
53android_sdk_version = 30
54```
55
wnwen03427bc2017-04-19 19:22:2456## How It Works
agrieve0c28e4f2016-09-22 01:05:2057
Peter Wena2d96112018-05-10 20:29:4358By default, only an `_all` module containing all java apk targets is generated.
59If just one apk target is explicitly specified, then a single apk module is
60generated.
wnwene851ad472017-04-27 16:21:4261
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
Peter Wena2d96112018-05-10 20:29:4364graph. This can be very slow when used with `--all` by default.
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
Peter Wena2d96112018-05-10 20:29:4371current target. The editor does not respect these exclude patterns, so the
72`_all` pseudo module is added which includes directories from all targets. This
73allows imports and refactoring to be across all targets.
wnwen03427bc2017-04-19 19:22:2474
75### Extracting .srcjars
agrieve0c28e4f2016-09-22 01:05:2076
77Most generated .java files in GN are stored as `.srcjars`. Android Studio does
Peter Wen9b5ef212018-07-13 03:55:3178not support them. The generator script builds and extracts them to
79`extracted-srcjars/` subdirectories for each target that contains generated
80files. This is the reason that the `_all` pseudo module may contain multiple
81copies of generated files. It can be slow to build all these generated files,
82so if `--fast` is passed then the generator script skips building and
83extracting them.
agrieve0c28e4f2016-09-22 01:05:2084
85*** note
Peter Wen9b5ef212018-07-13 03:55:3186** TLDR:** Always re-generate project files when generated files change (this
87includes `R.java`).
agrieve0c28e4f2016-09-22 01:05:2088***
89
Peter Wena2d96112018-05-10 20:29:4390### Native Files
91
92A new experimental option is now available to enable editing native C/C++ files
93with Android Studio. Pass in any number of `--native-target [target name]` flags
Peter Wen667abd52018-05-17 00:12:2594in order to try it out. The target must be the full path and name of a valid gn
95target (no shorthands). This will require you to install `cmake` and `ndk` when
Peter Wen8393de82018-05-17 20:58:5496prompted. Accept Android Studio's prompts for these SDK packages.
97
Peter Wenf8cc36fe2019-01-09 17:52:3198You need to disable a new gradle option in order to edit native files:
99File -&gt; Settings -&gt; Experimental
100-&gt; Gradle and uncheck "Only resolve selected variants".
101
Peter Wen8393de82018-05-17 20:58:54102This is not necessary, but to avoid "This file is not part of the project...",
103you can either add an extra `--native-target` flag or simply copy and paste the
104absolute path to that file into the CMakeLists.txt file alongside the existing
Peter Wenf8cc36fe2019-01-09 17:52:31105file paths. Note that changes to CMakeLists.txt will be overwritten on your next
106invocation of `generate_gradle.py`.
Peter Wen8393de82018-05-17 20:58:54107
108Example:
Peter Wena2d96112018-05-10 20:29:43109
110```shell
Richard Heb08a6942020-08-11 00:36:26111build/android/gradle/generate_gradle.py --native-target //chrome/android:libchrome
Peter Wena2d96112018-05-10 20:29:43112```
113
Peter Wencb519572018-05-16 17:39:18114## Tips
jbudorick900a8d832016-07-18 18:56:05115
Peter Wencb519572018-05-16 17:39:18116* Use environment variables to avoid having to specify `--output-directory`.
117 * Example: Append `export CHROMIUM_OUT_DIR=out; export BUILDTYPE=Debug` to
118 your `~/.bashrc` to always default to `out/Debug`.
Peter Wen667abd52018-05-17 00:12:25119* Using the Java debugger is documented [here](android_debugging_instructions.md#android-studio).
wnwen9032b352017-02-09 15:53:33120* Configuration instructions can be found
121 [here](http://tools.android.com/tech-docs/configuration). One suggestions:
122 * Launch it with more RAM:
123 `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-stable/bin/studio-launcher.sh`
wnwend65703a2016-11-24 18:47:31124* If you ever need to reset it: `rm -r ~/.AndroidStudio*/`
Boris Sazonova084e8f92017-09-14 12:29:18125* Import Chromium-specific style and inspections settings:
wnwen9032b352017-02-09 15:53:33126 * Help -&gt; Find Action -&gt; "Code Style" (settings) -&gt; Java -&gt;
Boris Sazonova084e8f92017-09-14 12:29:18127 Scheme -&gt; Import Scheme
128 * Select `tools/android/android_studio/ChromiumStyle.xml` -&gt; OK
129 * Help -&gt; Find Action -&gt; "Inspections" (settings) -&gt;
130 Profile -&gt; Import profile
131 * Select `tools/android/android_studio/ChromiumInspections.xml` -&gt; OK
wnwend65703a2016-11-24 18:47:31132* Turn on automatic import:
133 * Help -&gt; Find Action -&gt; "Auto Import"
134 * Tick all the boxes under "Java" and change the dropdown to "All".
wnwen67b64972016-12-19 19:53:15135* Turn on documentation on mouse hover:
136 * Help -&gt; Find Action -&gt; "Show quick documentation on mouse move"
137* Turn on line numbers:
138 * Help -&gt; Find Action -&gt; "Show line numbers"
Peter Wena2d96112018-05-10 20:29:43139* Turn off indent notification:
140 * Help -&gt; Find Action -&gt; "Show notifications about detected indents"
zpeng38bf00de2017-05-10 16:05:03141* Format changed files (Useful for changes made by running code inspection):
142 * Set up version control
143 * File -&gt; Settings -&gt; Version Control
144 * Add src directories
145 * Commit changes and reformat
146 * Help -&gt; Find Action -&gt; "Commit Changes"
147 * Check "Reformat code" & "Optimize imports" and commit
Peter Wencf4236de2018-05-23 19:26:36148* Change theme from GTK+ to another one to avoid invisible menus.
149 * Help -&gt; Find Action -&gt; "Theme: Settings > Appearance"
agrieve0c28e4f2016-09-22 01:05:20150
151### Useful Shortcuts
152
wnwend65703a2016-11-24 18:47:31153* `Shift - Shift`: Search to open file or perform IDE action
154* `Ctrl + N`: Jump to class
155* `Ctrl + Shift + T`: Jump to test
156* `Ctrl + Shift + N`: Jump to file
157* `Ctrl + F12`: Jump to method
158* `Ctrl + G`: Jump to line
159* `Shift + F6`: Rename variable
160* `Ctrl + Alt + O`: Organize imports
161* `Alt + Enter`: Quick Fix (use on underlined errors)
wnwenfdea62ce2017-04-05 13:12:54162* `F2`: Find next error
jbudorick900a8d832016-07-18 18:56:05163
agrievec62a52d2016-09-22 01:48:24164### Building from the Command Line
165
wnwen9032b352017-02-09 15:53:33166Gradle builds can be done from the command-line after importing the project
167into Android Studio (importing into the IDE causes the Gradle wrapper to be
168added). This wrapper can also be used to invoke gradle commands.
agrievec62a52d2016-09-22 01:48:24169
170 cd $GRADLE_PROJECT_DIR && bash gradlew
171
172The resulting artifacts are not terribly useful. They are missing assets,
173resources, native libraries, etc.
174
wnwen9032b352017-02-09 15:53:33175* Use a
176 [gradle daemon](https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html)
177 to speed up builds using the gradlew script:
178 * Add the line `org.gradle.daemon=true` to `~/.gradle/gradle.properties`,
179 creating it if necessary.
wnwen660b1312016-10-21 16:46:22180
Peter Wena2d96112018-05-10 20:29:43181## Status (as of May 10, 2018)
agrievec62a52d2016-09-22 01:48:24182
183### What works
184
Peter Wena2d96112018-05-10 20:29:43185* Android Studio v3.0-v3.2.
186* Java editing.
Peter Wen9222aa2f2018-06-13 17:50:06187 * Application code in `main` sourceset.
188 * Instrumentation test code in `androidTest` sourceset.
Peter Wena2d96112018-05-10 20:29:43189* Native code editing (experimental).
wnwene397fec62017-01-19 20:25:21190* Symlinks to existing .so files in jniLibs (doesn't generate them).
Andrew Grieve5b89b232017-11-21 18:41:10191* Editing resource xml files
Peter Wena2d96112018-05-10 20:29:43192* Layout editor (limited functionality).
estevenson8c9318ff2017-03-10 22:16:35193* Java debugging (see
wnwenfdea62ce2017-04-05 13:12:54194[here](/docs/android_debugging_instructions.md#Android-Studio)).
Peter Wena2d96112018-05-10 20:29:43195* Import resolution and refactoring across java files.
wnwene851ad472017-04-27 16:21:42196* Correct lint and AndroidManifest when only one target is specified.
Peter Wencb519572018-05-16 17:39:18197* Emulators (more docs coming soon).
Peter Wen667abd52018-05-17 00:12:25198* Separate Android SDK for Android Studio.
agrievec62a52d2016-09-22 01:48:24199
200### What doesn't work (yet) ([crbug](https://bugs.chromium.org/p/chromium/issues/detail?id=620034))
201
wnwen03427bc2017-04-19 19:22:24202* Gradle being aware of assets.
Andrew Grieve17b03142017-10-04 14:50:09203* Having the "Make Project" button work correctly.