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 |
Andrew Grieve | 17b0314 | 2017-10-04 14:50:09 | [diff] [blame^] | 11 | build/android/gradle/generate_gradle.py [--canary] # Use --canary for Android Studio 3.0 beta |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 12 | ``` |
| 13 | |
wnwen | e397fec6 | 2017-01-19 20:25:21 | [diff] [blame] | 14 | This creates a project at `out/Debug/gradle`. To create elsewhere: |
agrieve | e244c1a | 2016-09-22 19:35:18 | [diff] [blame] | 15 | |
| 16 | ```shell |
wnwen | e397fec6 | 2017-01-19 20:25:21 | [diff] [blame] | 17 | build/android/gradle/generate_gradle.py --output-directory out/My-Out-Dir --project-dir my-project |
agrieve | e244c1a | 2016-09-22 19:35:18 | [diff] [blame] | 18 | ``` |
| 19 | |
wnwen | 03427bc | 2017-04-19 19:22:24 | [diff] [blame] | 20 | By default, common targets are generated. To add more targets to generate |
| 21 | projects for: |
agrieve | e244c1a | 2016-09-22 19:35:18 | [diff] [blame] | 22 | |
| 23 | ```shell |
wnwen | 03427bc | 2017-04-19 19:22:24 | [diff] [blame] | 24 | build/android/gradle/generate_gradle.py --extra-target //chrome/android:chrome_public_apk |
agrieve | e244c1a | 2016-09-22 19:35:18 | [diff] [blame] | 25 | ``` |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 26 | |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 27 | For first-time Android Studio users: |
| 28 | |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 29 | * Avoid running the setup wizard. |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 30 | * The wizard will force you to download unwanted SDK components to |
| 31 | `//third_party/android_tools`. |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 32 | * To skip it, select "Cancel" when it comes up. |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 33 | |
| 34 | To import the project: |
| 35 | |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 36 | * Use "Import Project", and select the directory containing the generated |
wnwen | 0227e4a5 | 2017-02-09 21:14:51 | [diff] [blame] | 37 | project, by default `out/Debug/gradle`. |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 38 | |
Andrew Grieve | 17b0314 | 2017-10-04 14:50:09 | [diff] [blame^] | 39 | If you're asked to use Studio's Android SDK: |
| 40 | |
| 41 | * No. |
| 42 | |
| 43 | If you're asked to use Studio's Gradle wrapper: |
| 44 | |
| 45 | * Yes. |
| 46 | |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 47 | You need to re-run `generate_gradle.py` whenever `BUILD.gn` files change. |
| 48 | |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 49 | * After regenerating, Android Studio should prompt you to "Sync". If it |
| 50 | doesn't, use: |
wnwen | 0227e4a5 | 2017-02-09 21:14:51 | [diff] [blame] | 51 | * Button with two arrows on the right side of the top strip. |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 52 | * Help -> Find Action -> "Sync Project with Gradle Files" |
wnwen | 0227e4a5 | 2017-02-09 21:14:51 | [diff] [blame] | 53 | * After `gn clean` you may need to restart Android Studio. |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 54 | |
wnwen | 03427bc | 2017-04-19 19:22:24 | [diff] [blame] | 55 | ## How It Works |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 56 | |
wnwen | e851ad47 | 2017-04-27 16:21:42 | [diff] [blame] | 57 | By default, only a single module is generated. If more than one apk target is |
| 58 | specified, then an `_all` module is generated. Otherwise a single apk module is |
| 59 | generated. Since instrumentation tests are combined with their `apk_under_test` |
| 60 | target, they count as one module together. |
| 61 | |
| 62 | To see more detailed structure of gn targets, the `--split-projects` flag can |
| 63 | be used. This will generate one module for every gn target in the dependency |
| 64 | graph. |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 65 | |
wnwen | 03427bc | 2017-04-19 19:22:24 | [diff] [blame] | 66 | ### Excluded Files |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 67 | |
wnwen | 03427bc | 2017-04-19 19:22:24 | [diff] [blame] | 68 | Gradle supports source directories but not source files. However, files in |
| 69 | Chromium are used amongst multiple targets. To accommodate this, the script |
| 70 | detects such targets and creates exclude patterns to exclude files not in the |
| 71 | current target. The editor does not respect these exclude patterns, so a `_all` |
| 72 | pseudo module is added which includes directories from all targets. This allows |
| 73 | imports and refactorings to be searched across all targets. |
| 74 | |
| 75 | ### Extracting .srcjars |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 76 | |
| 77 | Most generated .java files in GN are stored as `.srcjars`. Android Studio does |
wnwen | 03427bc | 2017-04-19 19:22:24 | [diff] [blame] | 78 | not support them, and so the generator script builds and extracts them all to |
| 79 | `extracted-srcjars/` subdirectories for each target that contains them. This is |
| 80 | the reason that the `_all` pseudo module may contain multiple copies of |
| 81 | generated files. |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 82 | |
| 83 | *** note |
| 84 | ** TLDR:** Always re-generate project files when `.srcjars` change (this |
| 85 | includes `R.java`). |
| 86 | *** |
| 87 | |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 88 | ## Android Studio Tips |
| 89 | |
Andrew Grieve | 17b0314 | 2017-10-04 14:50:09 | [diff] [blame^] | 90 | * Using the Java debugger is documented at [android_debugging_instructions.md#android-studio](android_debugging_instructions.md#android-studio). |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 91 | * Configuration instructions can be found |
| 92 | [here](http://tools.android.com/tech-docs/configuration). One suggestions: |
| 93 | * Launch it with more RAM: |
| 94 | `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-stable/bin/studio-launcher.sh` |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 95 | * If you ever need to reset it: `rm -r ~/.AndroidStudio*/` |
Boris Sazonov | a084e8f9 | 2017-09-14 12:29:18 | [diff] [blame] | 96 | * Import Chromium-specific style and inspections settings: |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 97 | * Help -> Find Action -> "Code Style" (settings) -> Java -> |
Boris Sazonov | a084e8f9 | 2017-09-14 12:29:18 | [diff] [blame] | 98 | Scheme -> Import Scheme |
| 99 | * Select `tools/android/android_studio/ChromiumStyle.xml` -> OK |
| 100 | * Help -> Find Action -> "Inspections" (settings) -> |
| 101 | Profile -> Import profile |
| 102 | * Select `tools/android/android_studio/ChromiumInspections.xml` -> OK |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 103 | * Turn on automatic import: |
| 104 | * Help -> Find Action -> "Auto Import" |
| 105 | * Tick all the boxes under "Java" and change the dropdown to "All". |
wnwen | 67b6497 | 2016-12-19 19:53:15 | [diff] [blame] | 106 | * Turn on documentation on mouse hover: |
| 107 | * Help -> Find Action -> "Show quick documentation on mouse move" |
| 108 | * Turn on line numbers: |
| 109 | * Help -> Find Action -> "Show line numbers" |
zpeng | 38bf00de | 2017-05-10 16:05:03 | [diff] [blame] | 110 | * Format changed files (Useful for changes made by running code inspection): |
| 111 | * Set up version control |
| 112 | * File -> Settings -> Version Control |
| 113 | * Add src directories |
| 114 | * Commit changes and reformat |
| 115 | * Help -> Find Action -> "Commit Changes" |
| 116 | * Check "Reformat code" & "Optimize imports" and commit |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 117 | |
| 118 | ### Useful Shortcuts |
| 119 | |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 120 | * `Shift - Shift`: Search to open file or perform IDE action |
| 121 | * `Ctrl + N`: Jump to class |
| 122 | * `Ctrl + Shift + T`: Jump to test |
| 123 | * `Ctrl + Shift + N`: Jump to file |
| 124 | * `Ctrl + F12`: Jump to method |
| 125 | * `Ctrl + G`: Jump to line |
| 126 | * `Shift + F6`: Rename variable |
| 127 | * `Ctrl + Alt + O`: Organize imports |
| 128 | * `Alt + Enter`: Quick Fix (use on underlined errors) |
wnwen | fdea62ce | 2017-04-05 13:12:54 | [diff] [blame] | 129 | * `F2`: Find next error |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 130 | |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 131 | ### Building from the Command Line |
| 132 | |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 133 | Gradle builds can be done from the command-line after importing the project |
| 134 | into Android Studio (importing into the IDE causes the Gradle wrapper to be |
| 135 | added). This wrapper can also be used to invoke gradle commands. |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 136 | |
| 137 | cd $GRADLE_PROJECT_DIR && bash gradlew |
| 138 | |
| 139 | The resulting artifacts are not terribly useful. They are missing assets, |
| 140 | resources, native libraries, etc. |
| 141 | |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 142 | * Use a |
| 143 | [gradle daemon](https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html) |
| 144 | to speed up builds using the gradlew script: |
| 145 | * Add the line `org.gradle.daemon=true` to `~/.gradle/gradle.properties`, |
| 146 | creating it if necessary. |
wnwen | 660b131 | 2016-10-21 16:46:22 | [diff] [blame] | 147 | |
Andrew Grieve | 17b0314 | 2017-10-04 14:50:09 | [diff] [blame^] | 148 | ## Status (as of Oct, 2017) |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 149 | |
| 150 | ### What works |
| 151 | |
Andrew Grieve | 17b0314 | 2017-10-04 14:50:09 | [diff] [blame^] | 152 | * Android Studio v2.3, and v3.0 beta with `--canary` flag. |
wnwen | 03427bc | 2017-04-19 19:22:24 | [diff] [blame] | 153 | * Java editing and gradle compile. |
wnwen | e397fec6 | 2017-01-19 20:25:21 | [diff] [blame] | 154 | * Instrumentation tests included as androidTest. |
| 155 | * Symlinks to existing .so files in jniLibs (doesn't generate them). |
wnwen | 0227e4a5 | 2017-02-09 21:14:51 | [diff] [blame] | 156 | * Editing resource xml files. |
estevenson | 8c9318ff | 2017-03-10 22:16:35 | [diff] [blame] | 157 | * Java debugging (see |
wnwen | fdea62ce | 2017-04-05 13:12:54 | [diff] [blame] | 158 | [here](/docs/android_debugging_instructions.md#Android-Studio)). |
wnwen | 03427bc | 2017-04-19 19:22:24 | [diff] [blame] | 159 | * Import resolution and refactoring across all modules. |
wnwen | e851ad47 | 2017-04-27 16:21:42 | [diff] [blame] | 160 | * Correct lint and AndroidManifest when only one target is specified. |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 161 | |
| 162 | ### What doesn't work (yet) ([crbug](https://bugs.chromium.org/p/chromium/issues/detail?id=620034)) |
| 163 | |
wnwen | 03427bc | 2017-04-19 19:22:24 | [diff] [blame] | 164 | * Gradle being aware of assets. |
wnwen | fdea62ce | 2017-04-05 13:12:54 | [diff] [blame] | 165 | * Layout editor. |
Andrew Grieve | 17b0314 | 2017-10-04 14:50:09 | [diff] [blame^] | 166 | * Native code editing. |
| 167 | * Having the "Make Project" button work correctly. |