blob: e8ca8ec0b093b50cd393909a267a6b441be72344 [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:1814Use the flag `--sdk AndroidStudioDefault` to create and use a custom sdk
15directory and avoid issues with `gclient sync` and to use emulators. This will
16become the default soon.
agrievee244c1a2016-09-22 19:35:1817
18```shell
Peter Wencb519572018-05-16 17:39:1819build/android/gradle/generate_gradle.py --output-directory out/Debug --sdk AndroidStudioDefault
agrievee244c1a2016-09-22 19:35:1820```
jbudorick900a8d832016-07-18 18:56:0521
Peter Wencb519572018-05-16 17:39:1822The above commands create a project dir `gradle` under your output directory.
23Use `--project-dir <project-dir>` to change this.
Anthony Berent8d3ce022018-01-15 12:24:3624
25See [android_test_instructions.md](android_test_instructions.md#Using-Emulators)
26for more information about building and running emulators.
27
agrieve0c28e4f2016-09-22 01:05:2028For first-time Android Studio users:
Anthony Berent8d3ce022018-01-15 12:24:3629* Only run the setup wizard if you are planning to use emulators.
30 * The wizard will force you to download SDK components that are only needed
31 for emulation.
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:
wnwen9032b352017-02-09 15:53:3335* Use "Import Project", and select the directory containing the generated
Peter Wencb519572018-05-16 17:39:1836 project, e.g. `out/Debug/gradle`.
agrieve0c28e4f2016-09-22 01:05:2037
Andrew Grieve17b03142017-10-04 14:50:0938If you're asked to use Studio's Android SDK:
Peter Wena2d96112018-05-10 20:29:4339* No. (Always use your project's SDK configured by generate_gradle.py)
Andrew Grieve17b03142017-10-04 14:50:0940
41If you're asked to use Studio's Gradle wrapper:
Andrew Grieve17b03142017-10-04 14:50:0942* Yes.
43
agrieve0c28e4f2016-09-22 01:05:2044You need to re-run `generate_gradle.py` whenever `BUILD.gn` files change.
45
Peter Wena2d96112018-05-10 20:29:4346Pass `--canary` or `--beta` to avoid the "A newer version of gradle is
47available" notification.
48
wnwen9032b352017-02-09 15:53:3349* After regenerating, Android Studio should prompt you to "Sync". If it
Peter Wena2d96112018-05-10 20:29:4350 doesn't, try some of the following options:
51 * File -&gt; "Sync Project with Gradle Files"
wnwen0227e4a52017-02-09 21:14:5152 * Button with two arrows on the right side of the top strip.
wnwend65703a2016-11-24 18:47:3153 * Help -&gt; Find Action -&gt; "Sync Project with Gradle Files"
wnwen0227e4a52017-02-09 21:14:5154 * After `gn clean` you may need to restart Android Studio.
Peter Wena2d96112018-05-10 20:29:4355 * File -&gt; "Invalidate Caches / Restart..."
agrieve0c28e4f2016-09-22 01:05:2056
wnwen03427bc2017-04-19 19:22:2457## How It Works
agrieve0c28e4f2016-09-22 01:05:2058
Peter Wena2d96112018-05-10 20:29:4359By default, only an `_all` module containing all java apk targets is generated.
60If just one apk target is explicitly specified, then a single apk module is
61generated.
wnwene851ad472017-04-27 16:21:4262
63To see more detailed structure of gn targets, the `--split-projects` flag can
64be used. This will generate one module for every gn target in the dependency
Peter Wena2d96112018-05-10 20:29:4365graph. This can be very slow when used with `--all` by default.
agrieve0c28e4f2016-09-22 01:05:2066
wnwen03427bc2017-04-19 19:22:2467### Excluded Files
agrieve0c28e4f2016-09-22 01:05:2068
wnwen03427bc2017-04-19 19:22:2469Gradle supports source directories but not source files. However, files in
70Chromium are used amongst multiple targets. To accommodate this, the script
71detects such targets and creates exclude patterns to exclude files not in the
Peter Wena2d96112018-05-10 20:29:4372current target. The editor does not respect these exclude patterns, so the
73`_all` pseudo module is added which includes directories from all targets. This
74allows imports and refactoring to be across all targets.
wnwen03427bc2017-04-19 19:22:2475
76### Extracting .srcjars
agrieve0c28e4f2016-09-22 01:05:2077
78Most generated .java files in GN are stored as `.srcjars`. Android Studio does
Peter Wena2d96112018-05-10 20:29:4379not support them. It is very slow to build all these generated files and they
80rarely change. The generator script does not do anything with them by default.
81If `--full` is passed then the generator script builds and extracts them all to
wnwen03427bc2017-04-19 19:22:2482`extracted-srcjars/` subdirectories for each target that contains them. This is
83the reason that the `_all` pseudo module may contain multiple copies of
84generated files.
agrieve0c28e4f2016-09-22 01:05:2085
86*** note
Peter Wena2d96112018-05-10 20:29:4387** TLDR:** Re-generate project files with `--full` when generated files change (
88includes `R.java`) and to remove some red underlines in java files.
agrieve0c28e4f2016-09-22 01:05:2089***
90
Peter Wena2d96112018-05-10 20:29:4391### Native Files
92
93A new experimental option is now available to enable editing native C/C++ files
94with Android Studio. Pass in any number of `--native-target [target name]` flags
Peter Wencb519572018-05-16 17:39:1895in order to try it out. This will require you to install `cmake` and `ndk` when
96prompted. Accept Android Studio's prompts for these SDK packages. Example:
Peter Wena2d96112018-05-10 20:29:4397
98```shell
99build/android/gradle/generate_gradle.py --native-target //chrome/android:monochrome
100```
101
Peter Wencb519572018-05-16 17:39:18102## Tips
jbudorick900a8d832016-07-18 18:56:05103
Peter Wencb519572018-05-16 17:39:18104* Use environment variables to avoid having to specify `--output-directory`.
105 * Example: Append `export CHROMIUM_OUT_DIR=out; export BUILDTYPE=Debug` to
106 your `~/.bashrc` to always default to `out/Debug`.
Andrew Grieve17b03142017-10-04 14:50:09107* Using the Java debugger is documented at [android_debugging_instructions.md#android-studio](android_debugging_instructions.md#android-studio).
wnwen9032b352017-02-09 15:53:33108* Configuration instructions can be found
109 [here](http://tools.android.com/tech-docs/configuration). One suggestions:
110 * Launch it with more RAM:
111 `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-stable/bin/studio-launcher.sh`
wnwend65703a2016-11-24 18:47:31112* If you ever need to reset it: `rm -r ~/.AndroidStudio*/`
Boris Sazonova084e8f92017-09-14 12:29:18113* Import Chromium-specific style and inspections settings:
wnwen9032b352017-02-09 15:53:33114 * Help -&gt; Find Action -&gt; "Code Style" (settings) -&gt; Java -&gt;
Boris Sazonova084e8f92017-09-14 12:29:18115 Scheme -&gt; Import Scheme
116 * Select `tools/android/android_studio/ChromiumStyle.xml` -&gt; OK
117 * Help -&gt; Find Action -&gt; "Inspections" (settings) -&gt;
118 Profile -&gt; Import profile
119 * Select `tools/android/android_studio/ChromiumInspections.xml` -&gt; OK
wnwend65703a2016-11-24 18:47:31120* Turn on automatic import:
121 * Help -&gt; Find Action -&gt; "Auto Import"
122 * Tick all the boxes under "Java" and change the dropdown to "All".
wnwen67b64972016-12-19 19:53:15123* Turn on documentation on mouse hover:
124 * Help -&gt; Find Action -&gt; "Show quick documentation on mouse move"
125* Turn on line numbers:
126 * Help -&gt; Find Action -&gt; "Show line numbers"
Peter Wena2d96112018-05-10 20:29:43127* Turn off indent notification:
128 * Help -&gt; Find Action -&gt; "Show notifications about detected indents"
zpeng38bf00de2017-05-10 16:05:03129* Format changed files (Useful for changes made by running code inspection):
130 * Set up version control
131 * File -&gt; Settings -&gt; Version Control
132 * Add src directories
133 * Commit changes and reformat
134 * Help -&gt; Find Action -&gt; "Commit Changes"
135 * Check "Reformat code" & "Optimize imports" and commit
agrieve0c28e4f2016-09-22 01:05:20136
137### Useful Shortcuts
138
wnwend65703a2016-11-24 18:47:31139* `Shift - Shift`: Search to open file or perform IDE action
140* `Ctrl + N`: Jump to class
141* `Ctrl + Shift + T`: Jump to test
142* `Ctrl + Shift + N`: Jump to file
143* `Ctrl + F12`: Jump to method
144* `Ctrl + G`: Jump to line
145* `Shift + F6`: Rename variable
146* `Ctrl + Alt + O`: Organize imports
147* `Alt + Enter`: Quick Fix (use on underlined errors)
wnwenfdea62ce2017-04-05 13:12:54148* `F2`: Find next error
jbudorick900a8d832016-07-18 18:56:05149
agrievec62a52d2016-09-22 01:48:24150### Building from the Command Line
151
wnwen9032b352017-02-09 15:53:33152Gradle builds can be done from the command-line after importing the project
153into Android Studio (importing into the IDE causes the Gradle wrapper to be
154added). This wrapper can also be used to invoke gradle commands.
agrievec62a52d2016-09-22 01:48:24155
156 cd $GRADLE_PROJECT_DIR && bash gradlew
157
158The resulting artifacts are not terribly useful. They are missing assets,
159resources, native libraries, etc.
160
wnwen9032b352017-02-09 15:53:33161* Use a
162 [gradle daemon](https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html)
163 to speed up builds using the gradlew script:
164 * Add the line `org.gradle.daemon=true` to `~/.gradle/gradle.properties`,
165 creating it if necessary.
wnwen660b1312016-10-21 16:46:22166
Peter Wena2d96112018-05-10 20:29:43167## Status (as of May 10, 2018)
agrievec62a52d2016-09-22 01:48:24168
169### What works
170
Peter Wena2d96112018-05-10 20:29:43171* Android Studio v3.0-v3.2.
172* Java editing.
173* Native code editing (experimental).
wnwene397fec62017-01-19 20:25:21174* Instrumentation tests included as androidTest.
175* Symlinks to existing .so files in jniLibs (doesn't generate them).
Andrew Grieve5b89b232017-11-21 18:41:10176* Editing resource xml files
Peter Wena2d96112018-05-10 20:29:43177* Layout editor (limited functionality).
estevenson8c9318ff2017-03-10 22:16:35178* Java debugging (see
wnwenfdea62ce2017-04-05 13:12:54179[here](/docs/android_debugging_instructions.md#Android-Studio)).
Peter Wena2d96112018-05-10 20:29:43180* Import resolution and refactoring across java files.
wnwene851ad472017-04-27 16:21:42181* Correct lint and AndroidManifest when only one target is specified.
Peter Wencb519572018-05-16 17:39:18182* Emulators (more docs coming soon).
agrievec62a52d2016-09-22 01:48:24183
184### What doesn't work (yet) ([crbug](https://bugs.chromium.org/p/chromium/issues/detail?id=620034))
185
wnwen03427bc2017-04-19 19:22:24186* Gradle being aware of assets.
Andrew Grieve17b03142017-10-04 14:50:09187* Having the "Make Project" button work correctly.