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