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 |
wnwen | e397fec6 | 2017-01-19 20:25:21 | [diff] [blame] | 11 | build/android/gradle/generate_gradle.py |
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 | |
| 20 | By default, only common targets are generated. To customize the list of targets |
| 21 | to generate projects for: |
| 22 | |
| 23 | ```shell |
wnwen | 0227e4a5 | 2017-02-09 21:14:51 | [diff] [blame] | 24 | build/android/gradle/generate_gradle.py --target //chrome/android:chrome_public_apk --target //android_webview/test:android_webview_apk |
agrieve | e244c1a | 2016-09-22 19:35:18 | [diff] [blame] | 25 | ``` |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 26 | |
wnwen | fdea62ce | 2017-04-05 13:12:54 | [diff] [blame^] | 27 | For those upgrading from Android Studio 2.2 to 2.3: |
| 28 | |
| 29 | * Regenerate with `generate_gradle.py`. |
| 30 | * Clean up in `//third_party/android_tools` with `git clean -ffd`. |
| 31 | * Restart Android Studio with File -> "Invalidate Caches / Restart". |
| 32 | |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 33 | For first-time Android Studio users: |
| 34 | |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 35 | * Avoid running the setup wizard. |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 36 | * The wizard will force you to download unwanted SDK components to |
| 37 | `//third_party/android_tools`. |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 38 | * To skip it, select "Cancel" when it comes up. |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 39 | |
| 40 | To import the project: |
| 41 | |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 42 | * Use "Import Project", and select the directory containing the generated |
wnwen | 0227e4a5 | 2017-02-09 21:14:51 | [diff] [blame] | 43 | project, by default `out/Debug/gradle`. |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 44 | |
| 45 | You need to re-run `generate_gradle.py` whenever `BUILD.gn` files change. |
| 46 | |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 47 | * After regenerating, Android Studio should prompt you to "Sync". If it |
| 48 | doesn't, use: |
wnwen | 0227e4a5 | 2017-02-09 21:14:51 | [diff] [blame] | 49 | * Button with two arrows on the right side of the top strip. |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 50 | * Help -> Find Action -> "Sync Project with Gradle Files" |
wnwen | 0227e4a5 | 2017-02-09 21:14:51 | [diff] [blame] | 51 | * After `gn clean` you may need to restart Android Studio. |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 52 | |
| 53 | ## How it Works |
| 54 | |
| 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 |
| 57 | Gradle sub-project. |
| 58 | |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 59 | ### Excluded files and .srcjars |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 60 | |
| 61 | Gradle supports source directories but not source files. However, some |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 62 | directories in Chromium are split amonst multiple GN targets. To accommodate |
| 63 | this, the script detects such targets and creates exclude patterns to exclude |
wnwen | fdea62ce | 2017-04-05 13:12:54 | [diff] [blame^] | 64 | files not in the current target. You still see them when editing, but they are |
| 65 | excluded in gradle tasks. |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 66 | *** |
| 67 | |
| 68 | 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 |
| 70 | all to `extracted-srcjars/` subdirectories for each target that contains them. |
| 71 | |
| 72 | *** note |
| 73 | ** TLDR:** Always re-generate project files when `.srcjars` change (this |
| 74 | includes `R.java`). |
| 75 | *** |
| 76 | |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 77 | ## Android Studio Tips |
| 78 | |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 79 | * Configuration instructions can be found |
| 80 | [here](http://tools.android.com/tech-docs/configuration). One suggestions: |
| 81 | * Launch it with more RAM: |
| 82 | `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-stable/bin/studio-launcher.sh` |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 83 | * If you ever need to reset it: `rm -r ~/.AndroidStudio*/` |
| 84 | * Import Android style settings: |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 85 | * Help -> Find Action -> "Code Style" (settings) -> Java -> |
| 86 | Manage -> Import |
nyquist | 6e560d03 | 2017-02-28 07:49:59 | [diff] [blame] | 87 | * Select `tools/android/android_studio/ChromiumStyle.xml` |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 88 | * Turn on automatic import: |
| 89 | * Help -> Find Action -> "Auto Import" |
| 90 | * Tick all the boxes under "Java" and change the dropdown to "All". |
wnwen | 67b6497 | 2016-12-19 19:53:15 | [diff] [blame] | 91 | * Turn on documentation on mouse hover: |
| 92 | * Help -> Find Action -> "Show quick documentation on mouse move" |
| 93 | * Turn on line numbers: |
| 94 | * Help -> Find Action -> "Show line numbers" |
agrieve | 0c28e4f | 2016-09-22 01:05:20 | [diff] [blame] | 95 | |
| 96 | ### Useful Shortcuts |
| 97 | |
wnwen | d65703a | 2016-11-24 18:47:31 | [diff] [blame] | 98 | * `Shift - Shift`: Search to open file or perform IDE action |
| 99 | * `Ctrl + N`: Jump to class |
| 100 | * `Ctrl + Shift + T`: Jump to test |
| 101 | * `Ctrl + Shift + N`: Jump to file |
| 102 | * `Ctrl + F12`: Jump to method |
| 103 | * `Ctrl + G`: Jump to line |
| 104 | * `Shift + F6`: Rename variable |
| 105 | * `Ctrl + Alt + O`: Organize imports |
| 106 | * `Alt + Enter`: Quick Fix (use on underlined errors) |
wnwen | fdea62ce | 2017-04-05 13:12:54 | [diff] [blame^] | 107 | * `F2`: Find next error |
jbudorick | 900a8d83 | 2016-07-18 18:56:05 | [diff] [blame] | 108 | |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 109 | ### Building from the Command Line |
| 110 | |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 111 | Gradle builds can be done from the command-line after importing the project |
| 112 | into Android Studio (importing into the IDE causes the Gradle wrapper to be |
| 113 | added). This wrapper can also be used to invoke gradle commands. |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 114 | |
| 115 | cd $GRADLE_PROJECT_DIR && bash gradlew |
| 116 | |
| 117 | The resulting artifacts are not terribly useful. They are missing assets, |
| 118 | resources, native libraries, etc. |
| 119 | |
wnwen | 9032b35 | 2017-02-09 15:53:33 | [diff] [blame] | 120 | * Use a |
| 121 | [gradle daemon](https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html) |
| 122 | to speed up builds using the gradlew script: |
| 123 | * Add the line `org.gradle.daemon=true` to `~/.gradle/gradle.properties`, |
| 124 | creating it if necessary. |
wnwen | 660b131 | 2016-10-21 16:46:22 | [diff] [blame] | 125 | |
wnwen | fdea62ce | 2017-04-05 13:12:54 | [diff] [blame^] | 126 | ## Status (as of April 4th, 2017) |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 127 | |
| 128 | ### What works |
| 129 | |
wnwen | fdea62ce | 2017-04-05 13:12:54 | [diff] [blame^] | 130 | * Tested with Android Studio v2.3. |
wnwen | e397fec6 | 2017-01-19 20:25:21 | [diff] [blame] | 131 | * Java editing and gradle compile works. |
| 132 | * Instrumentation tests included as androidTest. |
| 133 | * Symlinks to existing .so files in jniLibs (doesn't generate them). |
wnwen | 0227e4a5 | 2017-02-09 21:14:51 | [diff] [blame] | 134 | * Editing resource xml files. |
estevenson | 8c9318ff | 2017-03-10 22:16:35 | [diff] [blame] | 135 | * Java debugging (see |
wnwen | fdea62ce | 2017-04-05 13:12:54 | [diff] [blame^] | 136 | [here](/docs/android_debugging_instructions.md#Android-Studio)). |
agrieve | c62a52d | 2016-09-22 01:48:24 | [diff] [blame] | 137 | |
| 138 | ### What doesn't work (yet) ([crbug](https://bugs.chromium.org/p/chromium/issues/detail?id=620034)) |
| 139 | |
wnwen | fdea62ce | 2017-04-05 13:12:54 | [diff] [blame^] | 140 | * Proper file resolution and imports for overlapping modules. |
| 141 | * Make gradle aware of assets. |
| 142 | * Layout editor. |
| 143 | * Add a mode in which gradle is responsible for generating `R.java`. |
| 144 | * Add support for native code editing. |
| 145 | * Make the "Make Project" button work correctly. |