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 | |
| 7 | ```shell |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame^] | 8 | build/android/gradle/generate_gradle.py --output-directory out-gn/Debug --target //chrome/android:chrome_public_test_apk |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 9 | ``` |
| 10 | |
| 11 | This creates a project at `out-gn/Debug/gradle`. To create elsewhere: `--project-dir foo` |
| 12 | |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame^] | 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. |
| 18 | |
| 19 | To import the project: |
| 20 | |
| 21 | * Use "Import Project", and select the directory containing the generated project. |
| 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) |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 68 | |
| 69 | ### What currently works |
| 70 | |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame^] | 71 | * Tested with Android Studio v2.2. |
| 72 | * Basic Java editing and compiling works. |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 73 | |
| 74 | ### Roadmap / what's not yet implemented ([crbug](https://bugs.chromium.org/p/chromium/issues/detail?id=620034)) |
| 75 | |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame^] | 76 | * JUnit Test targets |
| 77 | * Better support for instrumtation tests (they are treated as non-test .apks right now) |
| 78 | * Make gradle aware of resources and assets |
| 79 | * Make gradle aware of native code via pointing it at the location of our .so |
| 80 | * Add a mode in which gradle is responsible for generating `R.java` |
| 81 | * Add support for native code editing |
| 82 | * Make the "Make Project" button work correctly |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 83 | |
| 84 | ## Android Studio Tips |
| 85 | |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame^] | 86 | * Configuration instructions can be found [here](http://tools.android.com/tech-docs/configuration). One suggestions: |
| 87 | * Launch it with more RAM: `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-stable/bin/studio-launcher.sh` |
| 88 | * If you ever need to reset it: `rm -r ~/.AndroidStudio*/` |
| 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) |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 101 | |