blob: b46f863179339d3ed63b4d58665b48362156e812 [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
wnwenfdea62ce2017-04-05 13:12:5427For those upgrading from Android Studio 2.2 to 2.3:
28
29* Regenerate with `generate_gradle.py`.
30* Clean up in `//third_party/android_tools` with `git clean -ffd`.
31* Restart Android Studio with File -> "Invalidate Caches / Restart".
32
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
53## How it Works
54
55Android Studio integration works by generating `build.gradle` files based on GN
56targets. Each `android_apk` and `android_library` target produces a separate
57Gradle sub-project.
58
wnwen9032b352017-02-09 15:53:3359### Excluded files and .srcjars
agrieve0c28e4f2016-09-22 01:05:2060
61Gradle supports source directories but not source files. However, some
wnwen9032b352017-02-09 15:53:3362directories in Chromium are split amonst multiple GN targets. To accommodate
63this, the script detects such targets and creates exclude patterns to exclude
wnwenfdea62ce2017-04-05 13:12:5464files not in the current target. You still see them when editing, but they are
65excluded in gradle tasks.
agrieve0c28e4f2016-09-22 01:05:2066***
67
68Most generated .java files in GN are stored as `.srcjars`. Android Studio does
69not have support for them, and so the generator script builds and extracts them
70all to `extracted-srcjars/` subdirectories for each target that contains them.
71
72*** note
73** TLDR:** Always re-generate project files when `.srcjars` change (this
74includes `R.java`).
75***
76
jbudorick900a8d832016-07-18 18:56:0577## Android Studio Tips
78
wnwen9032b352017-02-09 15:53:3379* Configuration instructions can be found
80 [here](http://tools.android.com/tech-docs/configuration). One suggestions:
81 * Launch it with more RAM:
82 `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-stable/bin/studio-launcher.sh`
wnwend65703a2016-11-24 18:47:3183* If you ever need to reset it: `rm -r ~/.AndroidStudio*/`
84* Import Android style settings:
wnwen9032b352017-02-09 15:53:3385 * Help -> Find Action -> "Code Style" (settings) -> Java ->
86 Manage -> Import
nyquist6e560d032017-02-28 07:49:5987 * Select `tools/android/android_studio/ChromiumStyle.xml`
wnwend65703a2016-11-24 18:47:3188* Turn on automatic import:
89 * Help -> Find Action -> "Auto Import"
90 * Tick all the boxes under "Java" and change the dropdown to "All".
wnwen67b64972016-12-19 19:53:1591* Turn on documentation on mouse hover:
92 * Help -> Find Action -> "Show quick documentation on mouse move"
93* Turn on line numbers:
94 * Help -> Find Action -> "Show line numbers"
agrieve0c28e4f2016-09-22 01:05:2095
96### Useful Shortcuts
97
wnwend65703a2016-11-24 18:47:3198* `Shift - Shift`: Search to open file or perform IDE action
99* `Ctrl + N`: Jump to class
100* `Ctrl + Shift + T`: Jump to test
101* `Ctrl + Shift + N`: Jump to file
102* `Ctrl + F12`: Jump to method
103* `Ctrl + G`: Jump to line
104* `Shift + F6`: Rename variable
105* `Ctrl + Alt + O`: Organize imports
106* `Alt + Enter`: Quick Fix (use on underlined errors)
wnwenfdea62ce2017-04-05 13:12:54107* `F2`: Find next error
jbudorick900a8d832016-07-18 18:56:05108
agrievec62a52d2016-09-22 01:48:24109### Building from the Command Line
110
wnwen9032b352017-02-09 15:53:33111Gradle builds can be done from the command-line after importing the project
112into Android Studio (importing into the IDE causes the Gradle wrapper to be
113added). This wrapper can also be used to invoke gradle commands.
agrievec62a52d2016-09-22 01:48:24114
115 cd $GRADLE_PROJECT_DIR && bash gradlew
116
117The resulting artifacts are not terribly useful. They are missing assets,
118resources, native libraries, etc.
119
wnwen9032b352017-02-09 15:53:33120* Use a
121 [gradle daemon](https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html)
122 to speed up builds using the gradlew script:
123 * Add the line `org.gradle.daemon=true` to `~/.gradle/gradle.properties`,
124 creating it if necessary.
wnwen660b1312016-10-21 16:46:22125
wnwenfdea62ce2017-04-05 13:12:54126## Status (as of April 4th, 2017)
agrievec62a52d2016-09-22 01:48:24127
128### What works
129
wnwenfdea62ce2017-04-05 13:12:54130* Tested with Android Studio v2.3.
wnwene397fec62017-01-19 20:25:21131* Java editing and gradle compile works.
132* Instrumentation tests included as androidTest.
133* Symlinks to existing .so files in jniLibs (doesn't generate them).
wnwen0227e4a52017-02-09 21:14:51134* Editing resource xml files.
estevenson8c9318ff2017-03-10 22:16:35135* Java debugging (see
wnwenfdea62ce2017-04-05 13:12:54136[here](/docs/android_debugging_instructions.md#Android-Studio)).
agrievec62a52d2016-09-22 01:48:24137
138### What doesn't work (yet) ([crbug](https://bugs.chromium.org/p/chromium/issues/detail?id=620034))
139
wnwenfdea62ce2017-04-05 13:12:54140* Proper file resolution and imports for overlapping modules.
141* Make gradle aware of assets.
142* Layout editor.
143* Add a mode in which gradle is responsible for generating `R.java`.
144* Add support for native code editing.
145* Make the "Make Project" button work correctly.