blob: 753779c38bc729691cc8b9c1c2e56fb73a110bb9 [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
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
wnwene851ad472017-04-27 16:21:4234For those upgrading from Android Studio 2.2 to 2.3:
35
36* Use `gn clean` and `gn gen`
37* Clean up in `//third_party/android_tools` with `git clean -ffd`.
38* Remove project from android studio and regenerate with `generate_gradle.py`.
39
agrieve0c28e4f2016-09-22 01:05:2040To 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
wnwene851ad472017-04-27 16:21:4255By default, only a single module is generated. If more than one apk target is
56specified, then an `_all` module is generated. Otherwise a single apk module is
57generated. Since instrumentation tests are combined with their `apk_under_test`
58target, they count as one module together.
59
60To see more detailed structure of gn targets, the `--split-projects` flag can
61be used. This will generate one module for every gn target in the dependency
62graph.
agrieve0c28e4f2016-09-22 01:05:2063
wnwen03427bc2017-04-19 19:22:2464### Excluded Files
agrieve0c28e4f2016-09-22 01:05:2065
wnwen03427bc2017-04-19 19:22:2466Gradle supports source directories but not source files. However, files in
67Chromium are used amongst multiple targets. To accommodate this, the script
68detects such targets and creates exclude patterns to exclude files not in the
69current target. The editor does not respect these exclude patterns, so a `_all`
70pseudo module is added which includes directories from all targets. This allows
71imports and refactorings to be searched across all targets.
72
73### Extracting .srcjars
agrieve0c28e4f2016-09-22 01:05:2074
75Most generated .java files in GN are stored as `.srcjars`. Android Studio does
wnwen03427bc2017-04-19 19:22:2476not support them, and so the generator script builds and extracts them all to
77`extracted-srcjars/` subdirectories for each target that contains them. This is
78the reason that the `_all` pseudo module may contain multiple copies of
79generated files.
agrieve0c28e4f2016-09-22 01:05:2080
81*** note
82** TLDR:** Always re-generate project files when `.srcjars` change (this
83includes `R.java`).
84***
85
jbudorick900a8d832016-07-18 18:56:0586## Android Studio Tips
87
wnwen9032b352017-02-09 15:53:3388* Configuration instructions can be found
89 [here](http://tools.android.com/tech-docs/configuration). One suggestions:
90 * Launch it with more RAM:
91 `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-stable/bin/studio-launcher.sh`
wnwend65703a2016-11-24 18:47:3192* If you ever need to reset it: `rm -r ~/.AndroidStudio*/`
93* Import Android style settings:
wnwen9032b352017-02-09 15:53:3394 * Help -> Find Action -> "Code Style" (settings) -> Java ->
estevenson2ecfaf62017-05-04 18:33:0495 Manage -> Import -> select "Intellij IDEA code style XML" -> OK
nyquist6e560d032017-02-28 07:49:5996 * Select `tools/android/android_studio/ChromiumStyle.xml`
wnwend65703a2016-11-24 18:47:3197* Turn on automatic import:
98 * Help -> Find Action -> "Auto Import"
99 * Tick all the boxes under "Java" and change the dropdown to "All".
wnwen67b64972016-12-19 19:53:15100* Turn on documentation on mouse hover:
101 * Help -> Find Action -> "Show quick documentation on mouse move"
102* Turn on line numbers:
103 * Help -> Find Action -> "Show line numbers"
zpeng38bf00de2017-05-10 16:05:03104* Format changed files (Useful for changes made by running code inspection):
105 * Set up version control
106 * File -> Settings -> Version Control
107 * Add src directories
108 * Commit changes and reformat
109 * Help -> Find Action -> "Commit Changes"
110 * Check "Reformat code" & "Optimize imports" and commit
agrieve0c28e4f2016-09-22 01:05:20111
112### Useful Shortcuts
113
wnwend65703a2016-11-24 18:47:31114* `Shift - Shift`: Search to open file or perform IDE action
115* `Ctrl + N`: Jump to class
116* `Ctrl + Shift + T`: Jump to test
117* `Ctrl + Shift + N`: Jump to file
118* `Ctrl + F12`: Jump to method
119* `Ctrl + G`: Jump to line
120* `Shift + F6`: Rename variable
121* `Ctrl + Alt + O`: Organize imports
122* `Alt + Enter`: Quick Fix (use on underlined errors)
wnwenfdea62ce2017-04-05 13:12:54123* `F2`: Find next error
jbudorick900a8d832016-07-18 18:56:05124
agrievec62a52d2016-09-22 01:48:24125### Building from the Command Line
126
wnwen9032b352017-02-09 15:53:33127Gradle builds can be done from the command-line after importing the project
128into Android Studio (importing into the IDE causes the Gradle wrapper to be
129added). This wrapper can also be used to invoke gradle commands.
agrievec62a52d2016-09-22 01:48:24130
131 cd $GRADLE_PROJECT_DIR && bash gradlew
132
133The resulting artifacts are not terribly useful. They are missing assets,
134resources, native libraries, etc.
135
wnwen9032b352017-02-09 15:53:33136* Use a
137 [gradle daemon](https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html)
138 to speed up builds using the gradlew script:
139 * Add the line `org.gradle.daemon=true` to `~/.gradle/gradle.properties`,
140 creating it if necessary.
wnwen660b1312016-10-21 16:46:22141
wnwene851ad472017-04-27 16:21:42142## Status (as of April 27th, 2017)
agrievec62a52d2016-09-22 01:48:24143
144### What works
145
wnwen03427bc2017-04-19 19:22:24146* Android Studio v2.3.
147* Java editing and gradle compile.
wnwene397fec62017-01-19 20:25:21148* Instrumentation tests included as androidTest.
149* Symlinks to existing .so files in jniLibs (doesn't generate them).
wnwen0227e4a52017-02-09 21:14:51150* Editing resource xml files.
estevenson8c9318ff2017-03-10 22:16:35151* Java debugging (see
wnwenfdea62ce2017-04-05 13:12:54152[here](/docs/android_debugging_instructions.md#Android-Studio)).
wnwen03427bc2017-04-19 19:22:24153* Import resolution and refactoring across all modules.
wnwene851ad472017-04-27 16:21:42154* Correct lint and AndroidManifest when only one target is specified.
agrievec62a52d2016-09-22 01:48:24155
156### What doesn't work (yet) ([crbug](https://bugs.chromium.org/p/chromium/issues/detail?id=620034))
157
wnwen03427bc2017-04-19 19:22:24158* Gradle being aware of assets.
wnwenfdea62ce2017-04-05 13:12:54159* Layout editor.
wnwenfdea62ce2017-04-05 13:12:54160* Add support for native code editing.
161* Make the "Make Project" button work correctly.