blob: 6a87f97ab702f98725fba8417abc2de3d86333c8 [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
wnwene397fec62017-01-19 20:25:2111build/android/gradle/generate_gradle.py
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
wnwenfdea62ce2017-04-05 13:12:5427For those upgrading from Android Studio 2.2 to 2.3:
28
wnwen03427bc2017-04-19 19:22:2429* Use `gn clean` and `gn gen`
wnwenfdea62ce2017-04-05 13:12:5430* Clean up in `//third_party/android_tools` with `git clean -ffd`.
wnwen03427bc2017-04-19 19:22:2431* Remove project from android studio and regenerate with `generate_gradle.py`.
wnwenfdea62ce2017-04-05 13:12:5432
agrieve0c28e4f2016-09-22 01:05:2033For first-time Android Studio users:
34
wnwend65703a2016-11-24 18:47:3135* Avoid running the setup wizard.
wnwen9032b352017-02-09 15:53:3336 * The wizard will force you to download unwanted SDK components to
37 `//third_party/android_tools`.
agrievec62a52d2016-09-22 01:48:2438 * To skip it, select "Cancel" when it comes up.
agrieve0c28e4f2016-09-22 01:05:2039
40To import the project:
41
wnwen9032b352017-02-09 15:53:3342* Use "Import Project", and select the directory containing the generated
wnwen0227e4a52017-02-09 21:14:5143 project, by default `out/Debug/gradle`.
agrieve0c28e4f2016-09-22 01:05:2044
45You need to re-run `generate_gradle.py` whenever `BUILD.gn` files change.
46
wnwen9032b352017-02-09 15:53:3347* After regenerating, Android Studio should prompt you to "Sync". If it
48 doesn't, use:
wnwen0227e4a52017-02-09 21:14:5149 * Button with two arrows on the right side of the top strip.
wnwend65703a2016-11-24 18:47:3150 * Help -> Find Action -> "Sync Project with Gradle Files"
wnwen0227e4a52017-02-09 21:14:5151 * After `gn clean` you may need to restart Android Studio.
agrieve0c28e4f2016-09-22 01:05:2052
wnwen03427bc2017-04-19 19:22:2453## How It Works
agrieve0c28e4f2016-09-22 01:05:2054
55Android Studio integration works by generating `build.gradle` files based on GN
wnwen03427bc2017-04-19 19:22:2456targets. Each valid target produces a separate Gradle sub-project.
57Instrumentation tests are combined with their `apk_under_test`.
agrieve0c28e4f2016-09-22 01:05:2058
wnwen03427bc2017-04-19 19:22:2459### Excluded Files
agrieve0c28e4f2016-09-22 01:05:2060
wnwen03427bc2017-04-19 19:22:2461Gradle supports source directories but not source files. However, files in
62Chromium are used amongst multiple targets. To accommodate this, the script
63detects such targets and creates exclude patterns to exclude files not in the
64current target. The editor does not respect these exclude patterns, so a `_all`
65pseudo module is added which includes directories from all targets. This allows
66imports and refactorings to be searched across all targets.
67
68### Extracting .srcjars
agrieve0c28e4f2016-09-22 01:05:2069
70Most generated .java files in GN are stored as `.srcjars`. Android Studio does
wnwen03427bc2017-04-19 19:22:2471not support them, and so the generator script builds and extracts them all to
72`extracted-srcjars/` subdirectories for each target that contains them. This is
73the reason that the `_all` pseudo module may contain multiple copies of
74generated files.
agrieve0c28e4f2016-09-22 01:05:2075
76*** note
77** TLDR:** Always re-generate project files when `.srcjars` change (this
78includes `R.java`).
79***
80
jbudorick900a8d832016-07-18 18:56:0581## Android Studio Tips
82
wnwen9032b352017-02-09 15:53:3383* Configuration instructions can be found
84 [here](http://tools.android.com/tech-docs/configuration). One suggestions:
85 * Launch it with more RAM:
86 `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-stable/bin/studio-launcher.sh`
wnwend65703a2016-11-24 18:47:3187* If you ever need to reset it: `rm -r ~/.AndroidStudio*/`
88* Import Android style settings:
wnwen9032b352017-02-09 15:53:3389 * Help -> Find Action -> "Code Style" (settings) -> Java ->
90 Manage -> Import
nyquist6e560d032017-02-28 07:49:5991 * Select `tools/android/android_studio/ChromiumStyle.xml`
wnwend65703a2016-11-24 18:47:3192* Turn on automatic import:
93 * Help -> Find Action -> "Auto Import"
94 * Tick all the boxes under "Java" and change the dropdown to "All".
wnwen67b64972016-12-19 19:53:1595* Turn on documentation on mouse hover:
96 * Help -> Find Action -> "Show quick documentation on mouse move"
97* Turn on line numbers:
98 * Help -> Find Action -> "Show line numbers"
agrieve0c28e4f2016-09-22 01:05:2099
100### Useful Shortcuts
101
wnwend65703a2016-11-24 18:47:31102* `Shift - Shift`: Search to open file or perform IDE action
103* `Ctrl + N`: Jump to class
104* `Ctrl + Shift + T`: Jump to test
105* `Ctrl + Shift + N`: Jump to file
106* `Ctrl + F12`: Jump to method
107* `Ctrl + G`: Jump to line
108* `Shift + F6`: Rename variable
109* `Ctrl + Alt + O`: Organize imports
110* `Alt + Enter`: Quick Fix (use on underlined errors)
wnwenfdea62ce2017-04-05 13:12:54111* `F2`: Find next error
jbudorick900a8d832016-07-18 18:56:05112
agrievec62a52d2016-09-22 01:48:24113### Building from the Command Line
114
wnwen9032b352017-02-09 15:53:33115Gradle builds can be done from the command-line after importing the project
116into Android Studio (importing into the IDE causes the Gradle wrapper to be
117added). This wrapper can also be used to invoke gradle commands.
agrievec62a52d2016-09-22 01:48:24118
119 cd $GRADLE_PROJECT_DIR && bash gradlew
120
121The resulting artifacts are not terribly useful. They are missing assets,
122resources, native libraries, etc.
123
wnwen9032b352017-02-09 15:53:33124* Use a
125 [gradle daemon](https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html)
126 to speed up builds using the gradlew script:
127 * Add the line `org.gradle.daemon=true` to `~/.gradle/gradle.properties`,
128 creating it if necessary.
wnwen660b1312016-10-21 16:46:22129
wnwen03427bc2017-04-19 19:22:24130## Status (as of April 19th, 2017)
agrievec62a52d2016-09-22 01:48:24131
132### What works
133
wnwen03427bc2017-04-19 19:22:24134* Android Studio v2.3.
135* Java editing and gradle compile.
wnwene397fec62017-01-19 20:25:21136* Instrumentation tests included as androidTest.
137* Symlinks to existing .so files in jniLibs (doesn't generate them).
wnwen0227e4a52017-02-09 21:14:51138* Editing resource xml files.
estevenson8c9318ff2017-03-10 22:16:35139* Java debugging (see
wnwenfdea62ce2017-04-05 13:12:54140[here](/docs/android_debugging_instructions.md#Android-Studio)).
wnwen03427bc2017-04-19 19:22:24141* Import resolution and refactoring across all modules.
agrievec62a52d2016-09-22 01:48:24142
143### What doesn't work (yet) ([crbug](https://bugs.chromium.org/p/chromium/issues/detail?id=620034))
144
wnwen03427bc2017-04-19 19:22:24145* Gradle being aware of assets.
wnwenfdea62ce2017-04-05 13:12:54146* Layout editor.
wnwenfdea62ce2017-04-05 13:12:54147* Add support for native code editing.
148* Make the "Make Project" button work correctly.