| OLD | NEW |
| 1 # Android Studio | 1 # Android Studio |
| 2 | 2 |
| 3 [TOC] | 3 [TOC] |
| 4 | 4 |
| 5 ## Usage | 5 ## Usage |
| 6 | 6 |
| 7 Make sure you have followed | 7 Make sure you have followed |
| 8 [android build instructions](android_build_instructions.md) already. | 8 [android build instructions](android_build_instructions.md) already. |
| 9 | 9 |
| 10 ```shell | 10 ```shell |
| 11 build/android/gradle/generate_gradle.py | 11 build/android/gradle/generate_gradle.py |
| 12 ``` | 12 ``` |
| 13 | 13 |
| 14 This creates a project at `out/Debug/gradle`. To create elsewhere: | 14 This creates a project at `out/Debug/gradle`. To create elsewhere: |
| 15 | 15 |
| 16 ```shell | 16 ```shell |
| 17 build/android/gradle/generate_gradle.py --output-directory out/My-Out-Dir --proj
ect-dir my-project | 17 build/android/gradle/generate_gradle.py --output-directory out/My-Out-Dir --proj
ect-dir my-project |
| 18 ``` | 18 ``` |
| 19 | 19 |
| 20 By default, only common targets are generated. To customize the list of targets | 20 By default, common targets are generated. To add more targets to generate |
| 21 to generate projects for: | 21 projects for: |
| 22 | 22 |
| 23 ```shell | 23 ```shell |
| 24 build/android/gradle/generate_gradle.py --target //chrome/android:chrome_public_
apk --target //android_webview/test:android_webview_apk | 24 build/android/gradle/generate_gradle.py --extra-target //chrome/android:chrome_p
ublic_apk |
| 25 ``` | 25 ``` |
| 26 | 26 |
| 27 For those upgrading from Android Studio 2.2 to 2.3: | 27 For those upgrading from Android Studio 2.2 to 2.3: |
| 28 | 28 |
| 29 * Regenerate with `generate_gradle.py`. | 29 * Use `gn clean` and `gn gen` |
| 30 * Clean up in `//third_party/android_tools` with `git clean -ffd`. | 30 * Clean up in `//third_party/android_tools` with `git clean -ffd`. |
| 31 * Restart Android Studio with File -> "Invalidate Caches / Restart". | 31 * Remove project from android studio and regenerate with `generate_gradle.py`. |
| 32 | 32 |
| 33 For first-time Android Studio users: | 33 For first-time Android Studio users: |
| 34 | 34 |
| 35 * Avoid running the setup wizard. | 35 * Avoid running the setup wizard. |
| 36 * The wizard will force you to download unwanted SDK components to | 36 * The wizard will force you to download unwanted SDK components to |
| 37 `//third_party/android_tools`. | 37 `//third_party/android_tools`. |
| 38 * To skip it, select "Cancel" when it comes up. | 38 * To skip it, select "Cancel" when it comes up. |
| 39 | 39 |
| 40 To import the project: | 40 To import the project: |
| 41 | 41 |
| 42 * Use "Import Project", and select the directory containing the generated | 42 * Use "Import Project", and select the directory containing the generated |
| 43 project, by default `out/Debug/gradle`. | 43 project, by default `out/Debug/gradle`. |
| 44 | 44 |
| 45 You need to re-run `generate_gradle.py` whenever `BUILD.gn` files change. | 45 You need to re-run `generate_gradle.py` whenever `BUILD.gn` files change. |
| 46 | 46 |
| 47 * After regenerating, Android Studio should prompt you to "Sync". If it | 47 * After regenerating, Android Studio should prompt you to "Sync". If it |
| 48 doesn't, use: | 48 doesn't, use: |
| 49 * Button with two arrows on the right side of the top strip. | 49 * Button with two arrows on the right side of the top strip. |
| 50 * Help -> Find Action -> "Sync Project with Gradle Files" | 50 * Help -> Find Action -> "Sync Project with Gradle Files" |
| 51 * After `gn clean` you may need to restart Android Studio. | 51 * After `gn clean` you may need to restart Android Studio. |
| 52 | 52 |
| 53 ## How it Works | 53 ## How It Works |
| 54 | 54 |
| 55 Android Studio integration works by generating `build.gradle` files based on GN | 55 Android Studio integration works by generating `build.gradle` files based on GN |
| 56 targets. Each `android_apk` and `android_library` target produces a separate | 56 targets. Each valid target produces a separate Gradle sub-project. |
| 57 Gradle sub-project. | 57 Instrumentation tests are combined with their `apk_under_test`. |
| 58 | 58 |
| 59 ### Excluded files and .srcjars | 59 ### Excluded Files |
| 60 | 60 |
| 61 Gradle supports source directories but not source files. However, some | 61 Gradle supports source directories but not source files. However, files in |
| 62 directories in Chromium are split amonst multiple GN targets. To accommodate | 62 Chromium are used amongst multiple targets. To accommodate this, the script |
| 63 this, the script detects such targets and creates exclude patterns to exclude | 63 detects such targets and creates exclude patterns to exclude files not in the |
| 64 files not in the current target. You still see them when editing, but they are | 64 current target. The editor does not respect these exclude patterns, so a `_all` |
| 65 excluded in gradle tasks. | 65 pseudo module is added which includes directories from all targets. This allows |
| 66 *** | 66 imports and refactorings to be searched across all targets. |
| 67 |
| 68 ### Extracting .srcjars |
| 67 | 69 |
| 68 Most generated .java files in GN are stored as `.srcjars`. Android Studio does | 70 Most generated .java files in GN are stored as `.srcjars`. Android Studio does |
| 69 not have support for them, and so the generator script builds and extracts them | 71 not support them, and so the generator script builds and extracts them all to |
| 70 all to `extracted-srcjars/` subdirectories for each target that contains them. | 72 `extracted-srcjars/` subdirectories for each target that contains them. This is |
| 73 the reason that the `_all` pseudo module may contain multiple copies of |
| 74 generated files. |
| 71 | 75 |
| 72 *** note | 76 *** note |
| 73 ** TLDR:** Always re-generate project files when `.srcjars` change (this | 77 ** TLDR:** Always re-generate project files when `.srcjars` change (this |
| 74 includes `R.java`). | 78 includes `R.java`). |
| 75 *** | 79 *** |
| 76 | 80 |
| 77 ## Android Studio Tips | 81 ## Android Studio Tips |
| 78 | 82 |
| 79 * Configuration instructions can be found | 83 * Configuration instructions can be found |
| 80 [here](http://tools.android.com/tech-docs/configuration). One suggestions: | 84 [here](http://tools.android.com/tech-docs/configuration). One suggestions: |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 | 120 |
| 117 The resulting artifacts are not terribly useful. They are missing assets, | 121 The resulting artifacts are not terribly useful. They are missing assets, |
| 118 resources, native libraries, etc. | 122 resources, native libraries, etc. |
| 119 | 123 |
| 120 * Use a | 124 * Use a |
| 121 [gradle daemon](https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html) | 125 [gradle daemon](https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html) |
| 122 to speed up builds using the gradlew script: | 126 to speed up builds using the gradlew script: |
| 123 * Add the line `org.gradle.daemon=true` to `~/.gradle/gradle.properties`, | 127 * Add the line `org.gradle.daemon=true` to `~/.gradle/gradle.properties`, |
| 124 creating it if necessary. | 128 creating it if necessary. |
| 125 | 129 |
| 126 ## Status (as of April 4th, 2017) | 130 ## Status (as of April 19th, 2017) |
| 127 | 131 |
| 128 ### What works | 132 ### What works |
| 129 | 133 |
| 130 * Tested with Android Studio v2.3. | 134 * Android Studio v2.3. |
| 131 * Java editing and gradle compile works. | 135 * Java editing and gradle compile. |
| 132 * Instrumentation tests included as androidTest. | 136 * Instrumentation tests included as androidTest. |
| 133 * Symlinks to existing .so files in jniLibs (doesn't generate them). | 137 * Symlinks to existing .so files in jniLibs (doesn't generate them). |
| 134 * Editing resource xml files. | 138 * Editing resource xml files. |
| 135 * Java debugging (see | 139 * Java debugging (see |
| 136 [here](/docs/android_debugging_instructions.md#Android-Studio)). | 140 [here](/docs/android_debugging_instructions.md#Android-Studio)). |
| 141 * Import resolution and refactoring across all modules. |
| 137 | 142 |
| 138 ### What doesn't work (yet) ([crbug](https://bugs.chromium.org/p/chromium/issues
/detail?id=620034)) | 143 ### What doesn't work (yet) ([crbug](https://bugs.chromium.org/p/chromium/issues
/detail?id=620034)) |
| 139 | 144 |
| 140 * Proper file resolution and imports for overlapping modules. | 145 * Gradle being aware of assets. |
| 141 * Make gradle aware of assets. | |
| 142 * Layout editor. | 146 * Layout editor. |
| 143 * Add a mode in which gradle is responsible for generating `R.java`. | |
| 144 * Add support for native code editing. | 147 * Add support for native code editing. |
| 145 * Make the "Make Project" button work correctly. | 148 * Make the "Make Project" button work correctly. |
| OLD | NEW |