jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 1 | # Android Studio |
| 2 | |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 3 | [TOC] |
| 4 | |
| 5 | ## Usage |
| 6 | |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 7 | Make sure you have followed |
| 8 | [android build instructions](android_build_instructions.md) already. |
wnwen | e397fec6 | 2017-01-19 20:25:21 | [diff] [blame] | 9 | |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 10 | ```shell |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 11 | build/android/gradle/generate_gradle.py --output-directory out/Debug |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 12 | ``` |
| 13 | |
Peter Wen | cb51957 | 2018-05-16 17:39:18 | [diff] [blame] | 14 | Use the flag `--sdk AndroidStudioDefault` to create and use a custom sdk |
| 15 | directory and avoid issues with `gclient sync` and to use emulators. This will |
| 16 | become the default soon. |
agrieve | e244c1a | 2016-09-22 19:35:18 | [diff] [blame] | 17 | |
| 18 | ```shell |
Peter Wen | cb51957 | 2018-05-16 17:39:18 | [diff] [blame] | 19 | build/android/gradle/generate_gradle.py --output-directory out/Debug --sdk AndroidStudioDefault |
agrieve | e244c1a | 2016-09-22 19:35:18 | [diff] [blame] | 20 | ``` |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 21 | |
Peter Wen | cb51957 | 2018-05-16 17:39:18 | [diff] [blame] | 22 | The above commands create a project dir `gradle` under your output directory. |
| 23 | Use `--project-dir <project-dir>` to change this. |
Anthony Berent | 8d3ce02 | 2018-01-15 12:24:36 | [diff] [blame] | 24 | |
Peter Wen | efd1727 | 2018-05-23 18:51:40 | [diff] [blame] | 25 | To import the project: |
| 26 | * Use "Import Project", and select the directory containing the generated |
| 27 | project, e.g. `out/Debug/gradle`. |
Anthony Berent | 8d3ce02 | 2018-01-15 12:24:36 | [diff] [blame] | 28 | |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 29 | For first-time Android Studio users: |
Anthony Berent | 8d3ce02 | 2018-01-15 12:24:36 | [diff] [blame] | 30 | * Only run the setup wizard if you are planning to use emulators. |
| 31 | * The wizard will force you to download SDK components that are only needed |
| 32 | for emulation. |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 33 | * To skip it, select "Cancel" when it comes up. |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 34 | |
Peter Wen | efd1727 | 2018-05-23 18:51:40 | [diff] [blame] | 35 | See [android_test_instructions.md](android_test_instructions.md#Using-Emulators) |
| 36 | for more information about building and running emulators. |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 37 | |
Andrew Grieve | 17b0314 | 2017-10-04 14:50:09 | [diff] [blame] | 38 | If you're asked to use Studio's Android SDK: |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 39 | * No. (Always use your project's SDK configured by generate_gradle.py) |
Andrew Grieve | 17b0314 | 2017-10-04 14:50:09 | [diff] [blame] | 40 | |
| 41 | If you're asked to use Studio's Gradle wrapper: |
Andrew Grieve | 17b0314 | 2017-10-04 14:50:09 | [diff] [blame] | 42 | * Yes. |
| 43 | |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 44 | You need to re-run `generate_gradle.py` whenever `BUILD.gn` files change. |
| 45 | |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 46 | Pass `--canary` or `--beta` to avoid the "A newer version of gradle is |
| 47 | available" notification. |
| 48 | |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 49 | * After regenerating, Android Studio should prompt you to "Sync". If it |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 50 | doesn't, try some of the following options: |
| 51 | * File -> "Sync Project with Gradle Files" |
wnwen | 0227e4a5 | 2017-02-09 21:14:51 | [diff] [blame] | 52 | * Button with two arrows on the right side of the top strip. |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 53 | * Help -> Find Action -> "Sync Project with Gradle Files" |
wnwen | 0227e4a5 | 2017-02-09 21:14:51 | [diff] [blame] | 54 | * After `gn clean` you may need to restart Android Studio. |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 55 | * File -> "Invalidate Caches / Restart..." |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 56 | |
wnwen | 03427bc | 2017-04-19 19:22:24 | [diff] [blame] | 57 | ## How It Works |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 58 | |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 59 | By default, only an `_all` module containing all java apk targets is generated. |
| 60 | If just one apk target is explicitly specified, then a single apk module is |
| 61 | generated. |
wnwen | e851ad47 | 2017-04-27 16:21:42 | [diff] [blame] | 62 | |
| 63 | To see more detailed structure of gn targets, the `--split-projects` flag can |
| 64 | be used. This will generate one module for every gn target in the dependency |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 65 | graph. This can be very slow when used with `--all` by default. |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 66 | |
wnwen | 03427bc | 2017-04-19 19:22:24 | [diff] [blame] | 67 | ### Excluded Files |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 68 | |
wnwen | 03427bc | 2017-04-19 19:22:24 | [diff] [blame] | 69 | Gradle supports source directories but not source files. However, files in |
| 70 | Chromium are used amongst multiple targets. To accommodate this, the script |
| 71 | detects such targets and creates exclude patterns to exclude files not in the |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 72 | current target. The editor does not respect these exclude patterns, so the |
| 73 | `_all` pseudo module is added which includes directories from all targets. This |
| 74 | allows imports and refactoring to be across all targets. |
wnwen | 03427bc | 2017-04-19 19:22:24 | [diff] [blame] | 75 | |
| 76 | ### Extracting .srcjars |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 77 | |
| 78 | Most generated .java files in GN are stored as `.srcjars`. Android Studio does |
Peter Wen | 9b5ef21 | 2018-07-13 03:55:31 | [diff] [blame^] | 79 | not support them. The generator script builds and extracts them to |
| 80 | `extracted-srcjars/` subdirectories for each target that contains generated |
| 81 | files. This is the reason that the `_all` pseudo module may contain multiple |
| 82 | copies of generated files. It can be slow to build all these generated files, |
| 83 | so if `--fast` is passed then the generator script skips building and |
| 84 | extracting them. |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 85 | |
| 86 | *** note |
Peter Wen | 9b5ef21 | 2018-07-13 03:55:31 | [diff] [blame^] | 87 | ** TLDR:** Always re-generate project files when generated files change (this |
| 88 | includes `R.java`). |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 89 | *** |
| 90 | |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 91 | ### Native Files |
| 92 | |
| 93 | A new experimental option is now available to enable editing native C/C++ files |
| 94 | with Android Studio. Pass in any number of `--native-target [target name]` flags |
Peter Wen | 667abd5 | 2018-05-17 00:12:25 | [diff] [blame] | 95 | in order to try it out. The target must be the full path and name of a valid gn |
| 96 | target (no shorthands). This will require you to install `cmake` and `ndk` when |
Peter Wen | 8393de8 | 2018-05-17 20:58:54 | [diff] [blame] | 97 | prompted. Accept Android Studio's prompts for these SDK packages. |
| 98 | |
| 99 | This is not necessary, but to avoid "This file is not part of the project...", |
| 100 | you can either add an extra `--native-target` flag or simply copy and paste the |
| 101 | absolute path to that file into the CMakeLists.txt file alongside the existing |
| 102 | file paths. Note that these changes will be overwritten on your next invocation |
| 103 | of `generate_gradle.py`. |
| 104 | |
| 105 | Example: |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 106 | |
| 107 | ```shell |
Peter Wen | 8393de8 | 2018-05-17 20:58:54 | [diff] [blame] | 108 | build/android/gradle/generate_gradle.py --native-target //chrome/android:monochrome |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 109 | ``` |
| 110 | |
Peter Wen | cb51957 | 2018-05-16 17:39:18 | [diff] [blame] | 111 | ## Tips |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 112 | |
Peter Wen | cb51957 | 2018-05-16 17:39:18 | [diff] [blame] | 113 | * Use environment variables to avoid having to specify `--output-directory`. |
| 114 | * Example: Append `export CHROMIUM_OUT_DIR=out; export BUILDTYPE=Debug` to |
| 115 | your `~/.bashrc` to always default to `out/Debug`. |
Peter Wen | 667abd5 | 2018-05-17 00:12:25 | [diff] [blame] | 116 | * Using the Java debugger is documented [here](android_debugging_instructions.md#android-studio). |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 117 | * Configuration instructions can be found |
| 118 | [here](http://tools.android.com/tech-docs/configuration). One suggestions: |
| 119 | * Launch it with more RAM: |
| 120 | `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-stable/bin/studio-launcher.sh` |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 121 | * If you ever need to reset it: `rm -r ~/.AndroidStudio*/` |
Boris Sazonov | a084e8f9 | 2017-09-14 12:29:18 | [diff] [blame] | 122 | * Import Chromium-specific style and inspections settings: |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 123 | * Help -> Find Action -> "Code Style" (settings) -> Java -> |
Boris Sazonov | a084e8f9 | 2017-09-14 12:29:18 | [diff] [blame] | 124 | Scheme -> Import Scheme |
| 125 | * Select `tools/android/android_studio/ChromiumStyle.xml` -> OK |
| 126 | * Help -> Find Action -> "Inspections" (settings) -> |
| 127 | Profile -> Import profile |
| 128 | * Select `tools/android/android_studio/ChromiumInspections.xml` -> OK |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 129 | * Turn on automatic import: |
| 130 | * Help -> Find Action -> "Auto Import" |
| 131 | * Tick all the boxes under "Java" and change the dropdown to "All". |
wnwen | 67b6497 | 2016-12-19 19:53:15 | [diff] [blame] | 132 | * Turn on documentation on mouse hover: |
| 133 | * Help -> Find Action -> "Show quick documentation on mouse move" |
| 134 | * Turn on line numbers: |
| 135 | * Help -> Find Action -> "Show line numbers" |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 136 | * Turn off indent notification: |
| 137 | * Help -> Find Action -> "Show notifications about detected indents" |
zpeng | 38bf00de | 2017-05-10 16:05:03 | [diff] [blame] | 138 | * Format changed files (Useful for changes made by running code inspection): |
| 139 | * Set up version control |
| 140 | * File -> Settings -> Version Control |
| 141 | * Add src directories |
| 142 | * Commit changes and reformat |
| 143 | * Help -> Find Action -> "Commit Changes" |
| 144 | * Check "Reformat code" & "Optimize imports" and commit |
Peter Wen | cf4236de | 2018-05-23 19:26:36 | [diff] [blame] | 145 | * Change theme from GTK+ to another one to avoid invisible menus. |
| 146 | * Help -> Find Action -> "Theme: Settings > Appearance" |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 147 | |
| 148 | ### Useful Shortcuts |
| 149 | |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 150 | * `Shift - Shift`: Search to open file or perform IDE action |
| 151 | * `Ctrl + N`: Jump to class |
| 152 | * `Ctrl + Shift + T`: Jump to test |
| 153 | * `Ctrl + Shift + N`: Jump to file |
| 154 | * `Ctrl + F12`: Jump to method |
| 155 | * `Ctrl + G`: Jump to line |
| 156 | * `Shift + F6`: Rename variable |
| 157 | * `Ctrl + Alt + O`: Organize imports |
| 158 | * `Alt + Enter`: Quick Fix (use on underlined errors) |
wnwen | fdea62ce | 2017-04-05 13:12:54 | [diff] [blame] | 159 | * `F2`: Find next error |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 160 | |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 161 | ### Building from the Command Line |
| 162 | |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 163 | Gradle builds can be done from the command-line after importing the project |
| 164 | into Android Studio (importing into the IDE causes the Gradle wrapper to be |
| 165 | added). This wrapper can also be used to invoke gradle commands. |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 166 | |
| 167 | cd $GRADLE_PROJECT_DIR && bash gradlew |
| 168 | |
| 169 | The resulting artifacts are not terribly useful. They are missing assets, |
| 170 | resources, native libraries, etc. |
| 171 | |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 172 | * Use a |
| 173 | [gradle daemon](https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html) |
| 174 | to speed up builds using the gradlew script: |
| 175 | * Add the line `org.gradle.daemon=true` to `~/.gradle/gradle.properties`, |
| 176 | creating it if necessary. |
wnwen | 660b131 | 2016-10-21 16:46:22 | [diff] [blame] | 177 | |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 178 | ## Status (as of May 10, 2018) |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 179 | |
| 180 | ### What works |
| 181 | |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 182 | * Android Studio v3.0-v3.2. |
| 183 | * Java editing. |
Peter Wen | 9222aa2f | 2018-06-13 17:50:06 | [diff] [blame] | 184 | * Application code in `main` sourceset. |
| 185 | * Instrumentation test code in `androidTest` sourceset. |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 186 | * Native code editing (experimental). |
wnwen | e397fec6 | 2017-01-19 20:25:21 | [diff] [blame] | 187 | * Symlinks to existing .so files in jniLibs (doesn't generate them). |
Andrew Grieve | 5b89b23 | 2017-11-21 18:41:10 | [diff] [blame] | 188 | * Editing resource xml files |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 189 | * Layout editor (limited functionality). |
estevenson | 8c9318ff | 2017-03-10 22:16:35 | [diff] [blame] | 190 | * Java debugging (see |
wnwen | fdea62ce | 2017-04-05 13:12:54 | [diff] [blame] | 191 | [here](/docs/android_debugging_instructions.md#Android-Studio)). |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 192 | * Import resolution and refactoring across java files. |
wnwen | e851ad47 | 2017-04-27 16:21:42 | [diff] [blame] | 193 | * Correct lint and AndroidManifest when only one target is specified. |
Peter Wen | cb51957 | 2018-05-16 17:39:18 | [diff] [blame] | 194 | * Emulators (more docs coming soon). |
Peter Wen | 667abd5 | 2018-05-17 00:12:25 | [diff] [blame] | 195 | * Separate Android SDK for Android Studio. |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 196 | |
| 197 | ### What doesn't work (yet) ([crbug](https://bugs.chromium.org/p/chromium/issues/detail?id=620034)) |
| 198 | |
wnwen | 03427bc | 2017-04-19 19:22:24 | [diff] [blame] | 199 | * Gradle being aware of assets. |
Andrew Grieve | 17b0314 | 2017-10-04 14:50:09 | [diff] [blame] | 200 | * Having the "Make Project" button work correctly. |