blob: 900f090be13ea88574c3076a01984be4010d8fa9 [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
20By default, only common targets are generated. To customize the list of targets
21to generate projects for:
22
23```shell
wnwen0227e4a52017-02-09 21:14:5124build/android/gradle/generate_gradle.py --target //chrome/android:chrome_public_apk --target //android_webview/test:android_webview_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
39You need to re-run `generate_gradle.py` whenever `BUILD.gn` files change.
40
wnwen9032b352017-02-09 15:53:3341* After regenerating, Android Studio should prompt you to "Sync". If it
42 doesn't, use:
wnwen0227e4a52017-02-09 21:14:5143 * Button with two arrows on the right side of the top strip.
wnwend65703a2016-11-24 18:47:3144 * Help -> Find Action -> "Sync Project with Gradle Files"
wnwen0227e4a52017-02-09 21:14:5145 * After `gn clean` you may need to restart Android Studio.
agrieve0c28e4f2016-09-22 01:05:2046
47## How it Works
48
49Android Studio integration works by generating `build.gradle` files based on GN
50targets. Each `android_apk` and `android_library` target produces a separate
51Gradle sub-project.
52
wnwen9032b352017-02-09 15:53:3353### Excluded files and .srcjars
agrieve0c28e4f2016-09-22 01:05:2054
55Gradle supports source directories but not source files. However, some
wnwen9032b352017-02-09 15:53:3356directories in Chromium are split amonst multiple GN targets. To accommodate
57this, the script detects such targets and creates exclude patterns to exclude
58files not in the current target. You may still see them when editing, but they
59are excluded in gradle tasks.
agrieve0c28e4f2016-09-22 01:05:2060***
61
62Most generated .java files in GN are stored as `.srcjars`. Android Studio does
63not have support for them, and so the generator script builds and extracts them
64all to `extracted-srcjars/` subdirectories for each target that contains them.
65
66*** note
67** TLDR:** Always re-generate project files when `.srcjars` change (this
68includes `R.java`).
69***
70
jbudorick900a8d832016-07-18 18:56:0571## Android Studio Tips
72
wnwen9032b352017-02-09 15:53:3373* Configuration instructions can be found
74 [here](http://tools.android.com/tech-docs/configuration). One suggestions:
75 * Launch it with more RAM:
76 `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-stable/bin/studio-launcher.sh`
wnwend65703a2016-11-24 18:47:3177* If you ever need to reset it: `rm -r ~/.AndroidStudio*/`
78* Import Android style settings:
wnwen9032b352017-02-09 15:53:3379 * Help -> Find Action -> "Code Style" (settings) -> Java ->
80 Manage -> Import
nyquist6e560d032017-02-28 07:49:5981 * Select `tools/android/android_studio/ChromiumStyle.xml`
wnwend65703a2016-11-24 18:47:3182* Turn on automatic import:
83 * Help -> Find Action -> "Auto Import"
84 * Tick all the boxes under "Java" and change the dropdown to "All".
wnwen67b64972016-12-19 19:53:1585* Turn on documentation on mouse hover:
86 * Help -> Find Action -> "Show quick documentation on mouse move"
87* Turn on line numbers:
88 * Help -> Find Action -> "Show line numbers"
agrieve0c28e4f2016-09-22 01:05:2089
90### Useful Shortcuts
91
wnwend65703a2016-11-24 18:47:3192* `Shift - Shift`: Search to open file or perform IDE action
93* `Ctrl + N`: Jump to class
94* `Ctrl + Shift + T`: Jump to test
95* `Ctrl + Shift + N`: Jump to file
96* `Ctrl + F12`: Jump to method
97* `Ctrl + G`: Jump to line
98* `Shift + F6`: Rename variable
99* `Ctrl + Alt + O`: Organize imports
100* `Alt + Enter`: Quick Fix (use on underlined errors)
jbudorick900a8d832016-07-18 18:56:05101
agrievec62a52d2016-09-22 01:48:24102### Building from the Command Line
103
wnwen9032b352017-02-09 15:53:33104Gradle builds can be done from the command-line after importing the project
105into Android Studio (importing into the IDE causes the Gradle wrapper to be
106added). This wrapper can also be used to invoke gradle commands.
agrievec62a52d2016-09-22 01:48:24107
108 cd $GRADLE_PROJECT_DIR && bash gradlew
109
110The resulting artifacts are not terribly useful. They are missing assets,
111resources, native libraries, etc.
112
wnwen9032b352017-02-09 15:53:33113* Use a
114 [gradle daemon](https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html)
115 to speed up builds using the gradlew script:
116 * Add the line `org.gradle.daemon=true` to `~/.gradle/gradle.properties`,
117 creating it if necessary.
wnwen660b1312016-10-21 16:46:22118
wnwen0227e4a52017-02-09 21:14:51119## Status (as of Feb 7th, 2017)
agrievec62a52d2016-09-22 01:48:24120
121### What works
122
wnwend65703a2016-11-24 18:47:31123* Tested with Android Studio v2.2.
wnwene397fec62017-01-19 20:25:21124* Java editing and gradle compile works.
125* Instrumentation tests included as androidTest.
126* Symlinks to existing .so files in jniLibs (doesn't generate them).
wnwen0227e4a52017-02-09 21:14:51127* Editing resource xml files.
estevenson8c9318ff2017-03-10 22:16:35128* Java debugging (see
129[here](/docs/android_debugging_instructions.md#Android-Studio))
agrievec62a52d2016-09-22 01:48:24130
131### What doesn't work (yet) ([crbug](https://bugs.chromium.org/p/chromium/issues/detail?id=620034))
132
wnwen0227e4a52017-02-09 21:14:51133* Make gradle aware of assets
134* Layout editor
wnwend65703a2016-11-24 18:47:31135* Add a mode in which gradle is responsible for generating `R.java`
136* Add support for native code editing
137* Make the "Make Project" button work correctly