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