Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Android Studio | 1 # Android Studio |
| 2 | 2 |
| 3 Android Studio integration works by generating .gradle files from our BUILD.gn f iles. | |
| 4 | |
| 5 [TOC] | 3 [TOC] |
| 6 | 4 |
| 7 ## Usage | 5 ## Usage |
| 8 | 6 |
| 9 ```shell | 7 ```shell |
| 10 build/android/gradle/generate_gradle.py --output-directory out-gn/Debug --target //chrome/android:chrome_public_apk | 8 build/android/gradle/generate_gradle.py --output-directory out-gn/Debug --target //chrome/android:chrome_public_test_apk |
| 11 ``` | 9 ``` |
| 12 | 10 |
| 13 This creates a project at `out-gn/Debug/gradle`. To create elsewhere: `--project -dir foo` | 11 This creates a project at `out-gn/Debug/gradle`. To create elsewhere: `--project -dir foo` |
| 14 | 12 |
| 15 ## Status (as of July 14, 2016) | 13 For first-time Android Studio users: |
| 14 | |
| 15 * Avoid running the setup wizard. | |
| 16 * The wizard will force you to download unwanted SDK componentns to `//third _party/android_tools`. | |
| 17 * To skip it. Select "Cancel" when it comes up. | |
|
jbudorick
2016/09/21 20:14:02
super nit: . -> ,
agrieve
2016/09/22 00:58:07
Done (but did it in follow-up: https://codereview.
| |
| 18 | |
| 19 To import the project: | |
| 20 | |
| 21 * Use "Import Project", and select the directory containing the generated proje ct. | |
| 22 | |
| 23 You need to re-run `generate_gradle.py` whenever `BUILD.gn` files change. | |
| 24 | |
| 25 * After regenerating, Android Studio should prompt you to "Sync". If it doesn't , use: | |
| 26 * Help->Find Action->Sync Project with Gradle Files | |
| 27 | |
| 28 | |
| 29 ## How it Works | |
| 30 | |
| 31 Android Studio integration works by generating `build.gradle` files based on GN | |
| 32 targets. Each `android_apk` and `android_library` target produces a separate | |
| 33 Gradle sub-project. | |
| 34 | |
| 35 ### Symlinks and .srcjars | |
| 36 | |
| 37 Gradle supports source directories but not source files. However, some | |
| 38 `java/src/` directories in Chromium are split amonst multiple GN targets. To | |
| 39 accomodate this, the script detects such targets and creates a `symlinked-java/` | |
| 40 directory to point gradle at. Be warned that creating new files from Android | |
| 41 Studio within these symlink-based projects will cause new files to be created in | |
| 42 the generated `symlinked-java/` rather than the source tree where you want it. | |
| 43 | |
| 44 *** note | |
| 45 ** TLDR:** Always create new files outside of Android Studio. | |
| 46 *** | |
| 47 | |
| 48 Most generated .java files in GN are stored as `.srcjars`. Android Studio does | |
| 49 not have support for them, and so the generator script builds and extracts them | |
| 50 all to `extracted-srcjars/` subdirectories for each target that contains them. | |
| 51 | |
| 52 *** note | |
| 53 ** TLDR:** Always re-generate project files when `.srcjars` change (this | |
| 54 includes `R.java`). | |
| 55 *** | |
| 56 | |
| 57 ### Building with Gradle | |
| 58 | |
| 59 Gradle builds can be done from the command-line after importing the project into | |
| 60 Android Studio (importing into the IDE causes the Gradle wrapper to be added). | |
| 61 | |
| 62 cd $GRADLE_PROJECT_DIR && bash gradlew | |
| 63 | |
| 64 The resulting artifacts are not terribly useful. They are missing assets, | |
| 65 resources, native libraries, etc. | |
| 66 | |
| 67 ## Status (as of Sept 21, 2016) | |
| 16 | 68 |
| 17 ### What currently works | 69 ### What currently works |
| 18 | 70 |
| 19 - Basic Java editing and compiling | 71 * Tested with Android Studio v2.2. |
| 72 * Basic Java editing and compiling works. | |
| 20 | 73 |
| 21 ### Roadmap / what's not yet implemented ([crbug](https://bugs.chromium.org/p/ch romium/issues/detail?id=620034)) | 74 ### Roadmap / what's not yet implemented ([crbug](https://bugs.chromium.org/p/ch romium/issues/detail?id=620034)) |
| 22 | 75 |
| 23 - Test targets (although they *somewhat* work via `--target=//chrome/android:ch rome_public_test_apk__apk`) | 76 * JUnit Test targets |
| 24 - Make gradle aware of resources and assets | 77 * Better support for instrumtation tests (they are treated as non-test .apks ri ght now) |
| 25 - Make gradle aware of native code via pointing it at the location of our .so | 78 * Make gradle aware of resources and assets |
| 26 - Add a mode in which gradle is responsible for generating `R.java` | 79 * Make gradle aware of native code via pointing it at the location of our .so |
| 27 - Add support for native code editing | 80 * Add a mode in which gradle is responsible for generating `R.java` |
| 28 | 81 * Add support for native code editing |
| 29 ### What's odd about our integration | 82 * Make the "Make Project" button work correctly |
| 30 | |
| 31 - We disable generation of `R.java`, `BuildConfig.java`, `AndroidManifest.java` | |
| 32 - Generated .java files (.srcjars) are extracted to the project directory upon project creation. They are not re-extracted unless you manually run `generate_gr adle.py` again | |
| 33 | 83 |
| 34 ## Android Studio Tips | 84 ## Android Studio Tips |
| 35 | 85 |
| 36 - Configuration instructions can be found [here](http://tools.android.com/tech- docs/configuration). Some suggestions: | 86 * Configuration instructions can be found [here](http://tools.android.com/tech- docs/configuration). One suggestions: |
| 37 - Launch it with more RAM: `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-s table/bin/studio-launcher.sh` | 87 * Launch it with more RAM: `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio- stable/bin/studio-launcher.sh` |
| 38 - Setup wizard advice: | 88 * If you ever need to reset it: `rm -r ~/.AndroidStudio*/` |
| 39 - Choose "Standard", it then fails (at least for me) from "SDK tools director y is missing". Oh well... | |
| 40 - Choose "Import" and select your generated project directory | |
| 41 - Choose "OK" to set up gradle wrapper | |
| 42 - If you ever need to reset it: `rm -r ~/.AndroidStudio*/` | |
| 43 | 89 |
| 90 ### Useful Shortcuts | |
| 91 | |
| 92 * `Shift - Shift`: Search to open file or perform IDE action | |
| 93 * `Ctrl + N`: Jump to class | |
| 94 * `Ctrl + Shift + T`: Jump to test | |
| 95 * `Ctrl + Shift + N`: Jump to file | |
| 96 * `Ctrl + F12`: Jump to method | |
| 97 * `Ctrl + G`: Jump to line | |
| 98 * `Shift + F6`: Rename variable | |
| 99 * `Ctrl + Alt + O`: Organize imports | |
| 100 * `Alt + Enter`: Quick Fix (use on underlined errors) | |
| 101 | |
| OLD | NEW |