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 | e244c1a | 2016-09-22 19:35:18 | [diff] [blame] | 8 | build/android/gradle/generate_gradle.py --output-directory out-gn/Debug |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 9 | ``` |
| 10 | |
agrieve | e244c1a | 2016-09-22 19:35:18 | [diff] [blame] | 11 | This creates a project at `out-gn/Debug/gradle`. To create elsewhere: |
| 12 | |
| 13 | ```shell |
| 14 | build/android/gradle/generate_gradle.py --output-directory out-gn/Debug --project-dir my-project |
| 15 | ``` |
| 16 | |
| 17 | By default, only common targets are generated. To customize the list of targets |
| 18 | to generate projects for: |
| 19 | |
| 20 | ```shell |
| 21 | build/android/gradle/generate_gradle.py --output-directory out-gn/Debug --target //some:target_apk --target //some/other:target_apk |
| 22 | ``` |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 23 | |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 24 | For first-time Android Studio users: |
| 25 | |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 26 | * Avoid running the setup wizard. |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 27 | * The wizard will force you to download unwanted SDK components to `//third_party/android_tools`. |
| 28 | * To skip it, select "Cancel" when it comes up. |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 29 | |
| 30 | To import the project: |
| 31 | |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 32 | * Use "Import Project", and select the directory containing the generated project. |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 33 | |
| 34 | You need to re-run `generate_gradle.py` whenever `BUILD.gn` files change. |
| 35 | |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 36 | * After regenerating, Android Studio should prompt you to "Sync". If it doesn't, use: |
| 37 | * Help -> Find Action -> "Sync Project with Gradle Files" |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 38 | |
| 39 | |
| 40 | ## How it Works |
| 41 | |
| 42 | Android Studio integration works by generating `build.gradle` files based on GN |
| 43 | targets. Each `android_apk` and `android_library` target produces a separate |
| 44 | Gradle sub-project. |
| 45 | |
| 46 | ### Symlinks and .srcjars |
| 47 | |
| 48 | Gradle supports source directories but not source files. However, some |
| 49 | `java/src/` directories in Chromium are split amonst multiple GN targets. To |
qyearsley | c0dc6f4 | 2016-12-02 22:13:39 | [diff] [blame] | 50 | accommodate this, the script detects such targets and creates a `symlinked-java/` |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 51 | directory to point gradle at. Be warned that creating new files from Android |
| 52 | Studio within these symlink-based projects will cause new files to be created in |
| 53 | the generated `symlinked-java/` rather than the source tree where you want it. |
| 54 | |
| 55 | *** note |
| 56 | ** TLDR:** Always create new files outside of Android Studio. |
| 57 | *** |
| 58 | |
| 59 | Most generated .java files in GN are stored as `.srcjars`. Android Studio does |
| 60 | not have support for them, and so the generator script builds and extracts them |
| 61 | all to `extracted-srcjars/` subdirectories for each target that contains them. |
| 62 | |
| 63 | *** note |
| 64 | ** TLDR:** Always re-generate project files when `.srcjars` change (this |
| 65 | includes `R.java`). |
| 66 | *** |
| 67 | |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 68 | ## Android Studio Tips |
| 69 | |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 70 | * Configuration instructions can be found [here](http://tools.android.com/tech-docs/configuration). One suggestions: |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 71 | * Launch it with more RAM: `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-stable/bin/studio-launcher.sh` |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 72 | * If you ever need to reset it: `rm -r ~/.AndroidStudio*/` |
| 73 | * Import Android style settings: |
| 74 | * Help -> Find Action -> "Code Style" (settings) -> Java -> Manage -> Import |
| 75 | * Select `third_party/android_platform/development/ide/intellij/codestyles/AndroidStyle.xml` |
| 76 | * Turn on automatic import: |
| 77 | * Help -> Find Action -> "Auto Import" |
| 78 | * Tick all the boxes under "Java" and change the dropdown to "All". |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 79 | |
| 80 | ### Useful Shortcuts |
| 81 | |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 82 | * `Shift - Shift`: Search to open file or perform IDE action |
| 83 | * `Ctrl + N`: Jump to class |
| 84 | * `Ctrl + Shift + T`: Jump to test |
| 85 | * `Ctrl + Shift + N`: Jump to file |
| 86 | * `Ctrl + F12`: Jump to method |
| 87 | * `Ctrl + G`: Jump to line |
| 88 | * `Shift + F6`: Rename variable |
| 89 | * `Ctrl + Alt + O`: Organize imports |
| 90 | * `Alt + Enter`: Quick Fix (use on underlined errors) |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 91 | |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 92 | ### Building from the Command Line |
| 93 | |
| 94 | Gradle builds can be done from the command-line after importing the project into |
| 95 | Android Studio (importing into the IDE causes the Gradle wrapper to be added). |
wnwen | 660b131 | 2016-10-21 16:46:22 | [diff] [blame] | 96 | This wrapper can also be used to invoke gradle commands. |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 97 | |
| 98 | cd $GRADLE_PROJECT_DIR && bash gradlew |
| 99 | |
| 100 | The resulting artifacts are not terribly useful. They are missing assets, |
| 101 | resources, native libraries, etc. |
| 102 | |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 103 | * Use a [gradle daemon](https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html) to speed up builds: |
wnwen | 660b131 | 2016-10-21 16:46:22 | [diff] [blame] | 104 | * Add the line `org.gradle.daemon=true` to `~/.gradle/gradle.properties`, creating it if necessary. |
| 105 | |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 106 | ## Status (as of Sept 21, 2016) |
| 107 | |
| 108 | ### What works |
| 109 | |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 110 | * Tested with Android Studio v2.2. |
| 111 | * Basic Java editing and compiling works. |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 112 | |
| 113 | ### What doesn't work (yet) ([crbug](https://bugs.chromium.org/p/chromium/issues/detail?id=620034)) |
| 114 | |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 115 | * Better support for instrumentation tests (they are treated as non-test .apks right now) |
| 116 | * Make gradle aware of resources and assets |
| 117 | * Make gradle aware of native code via pointing it at the location of our .so |
| 118 | * Add a mode in which gradle is responsible for generating `R.java` |
| 119 | * Add support for native code editing |
| 120 | * Make the "Make Project" button work correctly |