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 | fac7ae4 | 2023-01-31 02:24:35 | [diff] [blame] | 24 | Feel free to accept Android Studio's recommended actions. `generate_gradle.py` |
| 25 | should have already set up a working version of the gradle wrapper and the |
| 26 | android gradle plugin, as well as a default Android SDK location at |
| 27 | `~/Android/Sdk`. Since the same script needs to support various versions of |
| 28 | Android Studio, the defaults may have lower version than the one recommended by |
| 29 | your version of Android Studio. After you accept Android Studio's update actions |
| 30 | the `generate_gradle.py` script will try to keep the newer versions when it is |
| 31 | re-run. |
Andrew Grieve | 17b0314 | 2017-10-04 14:50:09 | [diff] [blame] | 32 | |
Peter Wen | fac7ae4 | 2023-01-31 02:24:35 | [diff] [blame] | 33 | You'll need to re-run `generate_gradle.py` whenever new directories containing |
| 34 | source files are added. |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 35 | |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 36 | * After regenerating, Android Studio should prompt you to "Sync". If it |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 37 | doesn't, try some of the following options: |
| 38 | * File -> "Sync Project with Gradle Files" |
wnwen | 0227e4a5 | 2017-02-09 21:14:51 | [diff] [blame] | 39 | * Button with two arrows on the right side of the top strip. |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 40 | * Help -> Find Action -> "Sync Project with Gradle Files" |
wnwen | 0227e4a5 | 2017-02-09 21:14:51 | [diff] [blame] | 41 | * After `gn clean` you may need to restart Android Studio. |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 42 | * File -> "Invalidate Caches / Restart..." |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 43 | |
wnwen | 03427bc | 2017-04-19 19:22:24 | [diff] [blame] | 44 | ## How It Works |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 45 | |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 46 | By default, only an `_all` module containing all java apk targets is generated. |
| 47 | If just one apk target is explicitly specified, then a single apk module is |
| 48 | generated. |
wnwen | e851ad47 | 2017-04-27 16:21:42 | [diff] [blame] | 49 | |
Peter Wen | fac7ae4 | 2023-01-31 02:24:35 | [diff] [blame] | 50 | If you really prefer a more detailed structure of gn targets, the deprecated |
| 51 | `--split-projects` flag can be used. This will generate one module for every gn |
| 52 | target in the dependency graph. This can be very slow and is no longer |
| 53 | supported. |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 54 | |
Peter Wen | fac7ae4 | 2023-01-31 02:24:35 | [diff] [blame] | 55 | ### Generated files |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 56 | |
| 57 | Most generated .java files in GN are stored as `.srcjars`. Android Studio does |
Peter Wen | fac7ae4 | 2023-01-31 02:24:35 | [diff] [blame] | 58 | not support them. Our build will automatically extract them to a |
| 59 | `generated_java` directory in the output directory during the build. Thus if a |
| 60 | generated file is missing in Android Studio, build it with ninja first and it |
| 61 | should show up in Android Studio afterwards. |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 62 | |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 63 | ### Native Files |
| 64 | |
Peter Wen | fac7ae4 | 2023-01-31 02:24:35 | [diff] [blame] | 65 | This option is deprecated and no longer supported since Android Studio is very |
| 66 | slow when editing in a code base with a large number of C++ files, and Chromium |
| 67 | has a lot of C++ code. It is recommended to use [VS Code](vscode.md) to edit |
| 68 | native files and stick to just editing java files in Android Studio. |
Peter Wen | d46a867 | 2023-01-24 20:36:10 | [diff] [blame] | 69 | |
| 70 | If you still want to enable editing native C/C++ files with Android Studio, pass |
| 71 | in any number of `--native-target [target name]` flags in order to use it. The |
| 72 | target must be the full path and name of a valid gn target (no short-forms). |
| 73 | This will require you to install `cmake` and `ndk` when prompted. Accept Android |
| 74 | Studio's prompts for these SDK packages. |
Peter Wen | 8393de8 | 2018-05-17 20:58:54 | [diff] [blame] | 75 | |
Peter Wen | f8cc36fe | 2019-01-09 17:52:31 | [diff] [blame] | 76 | You need to disable a new gradle option in order to edit native files: |
| 77 | File -> Settings -> Experimental |
| 78 | -> Gradle and uncheck "Only resolve selected variants". |
| 79 | |
Peter Wen | 8393de8 | 2018-05-17 20:58:54 | [diff] [blame] | 80 | This is not necessary, but to avoid "This file is not part of the project...", |
| 81 | you can either add an extra `--native-target` flag or simply copy and paste the |
| 82 | absolute path to that file into the CMakeLists.txt file alongside the existing |
Peter Wen | f8cc36fe | 2019-01-09 17:52:31 | [diff] [blame] | 83 | file paths. Note that changes to CMakeLists.txt will be overwritten on your next |
| 84 | invocation of `generate_gradle.py`. |
Peter Wen | 8393de8 | 2018-05-17 20:58:54 | [diff] [blame] | 85 | |
| 86 | Example: |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 87 | |
| 88 | ```shell |
Richard He | b08a694 | 2020-08-11 00:36:26 | [diff] [blame] | 89 | build/android/gradle/generate_gradle.py --native-target //chrome/android:libchrome |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 90 | ``` |
| 91 | |
Peter Wen | cb51957 | 2018-05-16 17:39:18 | [diff] [blame] | 92 | ## Tips |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 93 | |
Peter Wen | cb51957 | 2018-05-16 17:39:18 | [diff] [blame] | 94 | * Use environment variables to avoid having to specify `--output-directory`. |
| 95 | * Example: Append `export CHROMIUM_OUT_DIR=out; export BUILDTYPE=Debug` to |
| 96 | your `~/.bashrc` to always default to `out/Debug`. |
Peter Wen | fac7ae4 | 2023-01-31 02:24:35 | [diff] [blame] | 97 | * Using the Java debugger is documented |
| 98 | [here](android_debugging_instructions.md#android-studio). |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 99 | * Configuration instructions can be found |
| 100 | [here](http://tools.android.com/tech-docs/configuration). One suggestions: |
| 101 | * Launch it with more RAM: |
| 102 | `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-stable/bin/studio-launcher.sh` |
Peter Wen | fac7ae4 | 2023-01-31 02:24:35 | [diff] [blame] | 103 | * If you ever need to reset it: `rm -r ~/.config/Google/AndroidStudio*/` |
Boris Sazonov | a084e8f9 | 2017-09-14 12:29:18 | [diff] [blame] | 104 | * Import Chromium-specific style and inspections settings: |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 105 | * Help -> Find Action -> "Code Style" (settings) -> Java -> |
Boris Sazonov | a084e8f9 | 2017-09-14 12:29:18 | [diff] [blame] | 106 | Scheme -> Import Scheme |
| 107 | * Select `tools/android/android_studio/ChromiumStyle.xml` -> OK |
| 108 | * Help -> Find Action -> "Inspections" (settings) -> |
| 109 | Profile -> Import profile |
| 110 | * Select `tools/android/android_studio/ChromiumInspections.xml` -> OK |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 111 | * Turn on automatic import: |
| 112 | * Help -> Find Action -> "Auto Import" |
| 113 | * Tick all the boxes under "Java" and change the dropdown to "All". |
wnwen | 67b6497 | 2016-12-19 19:53:15 | [diff] [blame] | 114 | * Turn on documentation on mouse hover: |
| 115 | * Help -> Find Action -> "Show quick documentation on mouse move" |
| 116 | * Turn on line numbers: |
| 117 | * Help -> Find Action -> "Show line numbers" |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 118 | * Turn off indent notification: |
| 119 | * Help -> Find Action -> "Show notifications about detected indents" |
zpeng | 38bf00de | 2017-05-10 16:05:03 | [diff] [blame] | 120 | * Format changed files (Useful for changes made by running code inspection): |
| 121 | * Set up version control |
| 122 | * File -> Settings -> Version Control |
| 123 | * Add src directories |
| 124 | * Commit changes and reformat |
| 125 | * Help -> Find Action -> "Commit Changes" |
| 126 | * Check "Reformat code" & "Optimize imports" and commit |
Peter Wen | cf4236de | 2018-05-23 19:26:36 | [diff] [blame] | 127 | * Change theme from GTK+ to another one to avoid invisible menus. |
| 128 | * Help -> Find Action -> "Theme: Settings > Appearance" |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 129 | |
| 130 | ### Useful Shortcuts |
| 131 | |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 132 | * `Shift - Shift`: Search to open file or perform IDE action |
| 133 | * `Ctrl + N`: Jump to class |
| 134 | * `Ctrl + Shift + T`: Jump to test |
| 135 | * `Ctrl + Shift + N`: Jump to file |
| 136 | * `Ctrl + F12`: Jump to method |
| 137 | * `Ctrl + G`: Jump to line |
| 138 | * `Shift + F6`: Rename variable |
| 139 | * `Ctrl + Alt + O`: Organize imports |
| 140 | * `Alt + Enter`: Quick Fix (use on underlined errors) |
wnwen | fdea62ce | 2017-04-05 13:12:54 | [diff] [blame] | 141 | * `F2`: Find next error |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 142 | |
Peter Wen | fac7ae4 | 2023-01-31 02:24:35 | [diff] [blame] | 143 | ### Building with Gradle |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 144 | |
Peter Wen | fac7ae4 | 2023-01-31 02:24:35 | [diff] [blame] | 145 | Gradle builds are not supported. Only editing is supported in Android Studio. |
| 146 | Use ninja to build as usual. |
wnwen | 660b131 | 2016-10-21 16:46:22 | [diff] [blame] | 147 | |
Peter Wen | d46a867 | 2023-01-24 20:36:10 | [diff] [blame] | 148 | ## Status |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 149 | |
| 150 | ### What works |
| 151 | |
Peter Wen | fac7ae4 | 2023-01-31 02:24:35 | [diff] [blame] | 152 | * Android Studio v2021~v2023. |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 153 | * Java editing. |
Peter Wen | 9222aa2f | 2018-06-13 17:50:06 | [diff] [blame] | 154 | * Application code in `main` sourceset. |
| 155 | * Instrumentation test code in `androidTest` sourceset. |
Peter Wen | d46a867 | 2023-01-24 20:36:10 | [diff] [blame] | 156 | * Native code editing (deprecated, use [VS Code](vscode.md) instead). |
wnwen | e397fec6 | 2017-01-19 20:25:21 | [diff] [blame] | 157 | * Symlinks to existing .so files in jniLibs (doesn't generate them). |
Andrew Grieve | 5b89b23 | 2017-11-21 18:41:10 | [diff] [blame] | 158 | * Editing resource xml files |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 159 | * Layout editor (limited functionality). |
estevenson | 8c9318ff | 2017-03-10 22:16:35 | [diff] [blame] | 160 | * Java debugging (see |
wnwen | fdea62ce | 2017-04-05 13:12:54 | [diff] [blame] | 161 | [here](/docs/android_debugging_instructions.md#Android-Studio)). |
Peter Wen | a2d9611 | 2018-05-10 20:29:43 | [diff] [blame] | 162 | * Import resolution and refactoring across java files. |
Peter Wen | 667abd5 | 2018-05-17 00:12:25 | [diff] [blame] | 163 | * Separate Android SDK for Android Studio. |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 164 | |
Peter Wen | d46a867 | 2023-01-24 20:36:10 | [diff] [blame] | 165 | ### What doesn't work |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 166 | |
Peter Wen | fac7ae4 | 2023-01-31 02:24:35 | [diff] [blame] | 167 | * Building with Gradle. |
| 168 | * The "Make Project" button doesn't work. |
Peter Wen | d46a867 | 2023-01-24 20:36:10 | [diff] [blame] | 169 | * Stick to using `autoninja` to build targets and just use Android Studio |
| 170 | for editing java source files. |
| 171 | * No active work is underway or planned to expand Android Studio support. |