blob: ee3a60252626f062d1891265ab759b60d2e4fc00 [file] [log] [blame] [view]
jbudorick900a8d832016-07-18 18:56:051# Android Studio
2
jbudorick900a8d832016-07-18 18:56:053[TOC]
4
5## Usage
6
7```shell
agrievee244c1a2016-09-22 19:35:188build/android/gradle/generate_gradle.py --output-directory out-gn/Debug
jbudorick900a8d832016-07-18 18:56:059```
10
agrievee244c1a2016-09-22 19:35:1811This creates a project at `out-gn/Debug/gradle`. To create elsewhere:
12
13```shell
14build/android/gradle/generate_gradle.py --output-directory out-gn/Debug --project-dir my-project
15```
16
17By default, only common targets are generated. To customize the list of targets
18to generate projects for:
19
20```shell
21build/android/gradle/generate_gradle.py --output-directory out-gn/Debug --target //some:target_apk --target //some/other:target_apk
22```
jbudorick900a8d832016-07-18 18:56:0523
agrieve0c28e4f2016-09-22 01:05:2024For first-time Android Studio users:
25
wnwend65703a2016-11-24 18:47:3126* Avoid running the setup wizard.
agrievec62a52d2016-09-22 01:48:2427 * The wizard will force you to download unwanted SDK components to `//third_party/android_tools`.
28 * To skip it, select "Cancel" when it comes up.
agrieve0c28e4f2016-09-22 01:05:2029
30To import the project:
31
wnwen67b64972016-12-19 19:53:1532* Use "Import Project", and select the directory containing the generated project, by default `out-gn/Debug/gradle`.
agrieve0c28e4f2016-09-22 01:05:2033
34You need to re-run `generate_gradle.py` whenever `BUILD.gn` files change.
35
wnwend65703a2016-11-24 18:47:3136* After regenerating, Android Studio should prompt you to "Sync". If it doesn't, use:
37 * Help -> Find Action -> "Sync Project with Gradle Files"
agrieve0c28e4f2016-09-22 01:05:2038
39
40## How it Works
41
42Android Studio integration works by generating `build.gradle` files based on GN
43targets. Each `android_apk` and `android_library` target produces a separate
44Gradle sub-project.
45
46### Symlinks and .srcjars
47
48Gradle supports source directories but not source files. However, some
49`java/src/` directories in Chromium are split amonst multiple GN targets. To
qyearsleyc0dc6f42016-12-02 22:13:3950accommodate this, the script detects such targets and creates a `symlinked-java/`
agrieve0c28e4f2016-09-22 01:05:2051directory to point gradle at. Be warned that creating new files from Android
52Studio within these symlink-based projects will cause new files to be created in
53the generated `symlinked-java/` rather than the source tree where you want it.
54
55*** note
56** TLDR:** Always create new files outside of Android Studio.
57***
58
59Most generated .java files in GN are stored as `.srcjars`. Android Studio does
60not have support for them, and so the generator script builds and extracts them
61all to `extracted-srcjars/` subdirectories for each target that contains them.
62
63*** note
64** TLDR:** Always re-generate project files when `.srcjars` change (this
65includes `R.java`).
66***
67
jbudorick900a8d832016-07-18 18:56:0568## Android Studio Tips
69
wnwend65703a2016-11-24 18:47:3170* Configuration instructions can be found [here](http://tools.android.com/tech-docs/configuration). One suggestions:
agrieve0c28e4f2016-09-22 01:05:2071 * Launch it with more RAM: `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-stable/bin/studio-launcher.sh`
wnwend65703a2016-11-24 18:47:3172* If you ever need to reset it: `rm -r ~/.AndroidStudio*/`
73* Import Android style settings:
74 * Help -> Find Action -> "Code Style" (settings) -> Java -> Manage -> Import
75 * Select `third_party/android_platform/development/ide/intellij/codestyles/AndroidStyle.xml`
76* Turn on automatic import:
77 * Help -> Find Action -> "Auto Import"
78 * Tick all the boxes under "Java" and change the dropdown to "All".
wnwen67b64972016-12-19 19:53:1579* Turn on documentation on mouse hover:
80 * Help -> Find Action -> "Show quick documentation on mouse move"
81* Turn on line numbers:
82 * Help -> Find Action -> "Show line numbers"
agrieve0c28e4f2016-09-22 01:05:2083
84### Useful Shortcuts
85
wnwend65703a2016-11-24 18:47:3186* `Shift - Shift`: Search to open file or perform IDE action
87* `Ctrl + N`: Jump to class
88* `Ctrl + Shift + T`: Jump to test
89* `Ctrl + Shift + N`: Jump to file
90* `Ctrl + F12`: Jump to method
91* `Ctrl + G`: Jump to line
92* `Shift + F6`: Rename variable
93* `Ctrl + Alt + O`: Organize imports
94* `Alt + Enter`: Quick Fix (use on underlined errors)
jbudorick900a8d832016-07-18 18:56:0595
agrievec62a52d2016-09-22 01:48:2496### Building from the Command Line
97
98Gradle builds can be done from the command-line after importing the project into
99Android Studio (importing into the IDE causes the Gradle wrapper to be added).
wnwen660b1312016-10-21 16:46:22100This wrapper can also be used to invoke gradle commands.
agrievec62a52d2016-09-22 01:48:24101
102 cd $GRADLE_PROJECT_DIR && bash gradlew
103
104The resulting artifacts are not terribly useful. They are missing assets,
105resources, native libraries, etc.
106
wnwend65703a2016-11-24 18:47:31107* Use a [gradle daemon](https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html) to speed up builds:
wnwen660b1312016-10-21 16:46:22108 * Add the line `org.gradle.daemon=true` to `~/.gradle/gradle.properties`, creating it if necessary.
109
agrievec62a52d2016-09-22 01:48:24110## Status (as of Sept 21, 2016)
111
112### What works
113
wnwend65703a2016-11-24 18:47:31114* Tested with Android Studio v2.2.
115* Basic Java editing and compiling works.
agrievec62a52d2016-09-22 01:48:24116
117### What doesn't work (yet) ([crbug](https://bugs.chromium.org/p/chromium/issues/detail?id=620034))
118
wnwend65703a2016-11-24 18:47:31119* Better support for instrumentation tests (they are treated as non-test .apks right now)
120* Make gradle aware of resources and assets
121* Make gradle aware of native code via pointing it at the location of our .so
122* Add a mode in which gradle is responsible for generating `R.java`
123* Add support for native code editing
124* Make the "Make Project" button work correctly