blob: 4016e62faedaa19c2b80212465e1bcfe2152f697 [file] [log] [blame] [view]
jbudorick900a8d832016-07-18 18:56:051# Android Studio
2
jbudorick900a8d832016-07-18 18:56:053[TOC]
4
5## Usage
6
wnwen9032b352017-02-09 15:53:337Make sure you have followed
8[android build instructions](android_build_instructions.md) already.
wnwene397fec62017-01-19 20:25:219
jbudorick900a8d832016-07-18 18:56:0510```shell
Andrew Grieve5b89b232017-11-21 18:41:1011build/android/gradle/generate_gradle.py --output-directory out/Debug [--canary] # Use --canary for Android Studio 3.1 canary
jbudorick900a8d832016-07-18 18:56:0512```
13
wnwene397fec62017-01-19 20:25:2114This creates a project at `out/Debug/gradle`. To create elsewhere:
agrievee244c1a2016-09-22 19:35:1815
16```shell
Andrew Grieve5b89b232017-11-21 18:41:1017build/android/gradle/generate_gradle.py --output-directory out/Debug --project-dir my-project
agrievee244c1a2016-09-22 19:35:1818```
jbudorick900a8d832016-07-18 18:56:0519
agrieve0c28e4f2016-09-22 01:05:2020For first-time Android Studio users:
21
wnwend65703a2016-11-24 18:47:3122* Avoid running the setup wizard.
wnwen9032b352017-02-09 15:53:3323 * The wizard will force you to download unwanted SDK components to
24 `//third_party/android_tools`.
agrievec62a52d2016-09-22 01:48:2425 * To skip it, select "Cancel" when it comes up.
agrieve0c28e4f2016-09-22 01:05:2026
27To import the project:
28
wnwen9032b352017-02-09 15:53:3329* Use "Import Project", and select the directory containing the generated
wnwen0227e4a52017-02-09 21:14:5130 project, by default `out/Debug/gradle`.
agrieve0c28e4f2016-09-22 01:05:2031
Andrew Grieve17b03142017-10-04 14:50:0932If you're asked to use Studio's Android SDK:
33
Peter Wena959a6c2017-11-06 18:24:3634* No. (Always use project's own SDK)
Andrew Grieve17b03142017-10-04 14:50:0935
36If you're asked to use Studio's Gradle wrapper:
37
38* Yes.
39
agrieve0c28e4f2016-09-22 01:05:2040You need to re-run `generate_gradle.py` whenever `BUILD.gn` files change.
41
wnwen9032b352017-02-09 15:53:3342* After regenerating, Android Studio should prompt you to "Sync". If it
43 doesn't, use:
wnwen0227e4a52017-02-09 21:14:5144 * Button with two arrows on the right side of the top strip.
wnwend65703a2016-11-24 18:47:3145 * Help -> Find Action -> "Sync Project with Gradle Files"
wnwen0227e4a52017-02-09 21:14:5146 * After `gn clean` you may need to restart Android Studio.
agrieve0c28e4f2016-09-22 01:05:2047
wnwen03427bc2017-04-19 19:22:2448## How It Works
agrieve0c28e4f2016-09-22 01:05:2049
wnwene851ad472017-04-27 16:21:4250By default, only a single module is generated. If more than one apk target is
51specified, then an `_all` module is generated. Otherwise a single apk module is
52generated. Since instrumentation tests are combined with their `apk_under_test`
53target, they count as one module together.
54
55To see more detailed structure of gn targets, the `--split-projects` flag can
56be used. This will generate one module for every gn target in the dependency
57graph.
agrieve0c28e4f2016-09-22 01:05:2058
wnwen03427bc2017-04-19 19:22:2459### Excluded Files
agrieve0c28e4f2016-09-22 01:05:2060
wnwen03427bc2017-04-19 19:22:2461Gradle supports source directories but not source files. However, files in
62Chromium are used amongst multiple targets. To accommodate this, the script
63detects such targets and creates exclude patterns to exclude files not in the
64current target. The editor does not respect these exclude patterns, so a `_all`
65pseudo module is added which includes directories from all targets. This allows
66imports and refactorings to be searched across all targets.
67
68### Extracting .srcjars
agrieve0c28e4f2016-09-22 01:05:2069
70Most generated .java files in GN are stored as `.srcjars`. Android Studio does
wnwen03427bc2017-04-19 19:22:2471not support them, and so the generator script builds and extracts them all to
72`extracted-srcjars/` subdirectories for each target that contains them. This is
73the reason that the `_all` pseudo module may contain multiple copies of
74generated files.
agrieve0c28e4f2016-09-22 01:05:2075
76*** note
77** TLDR:** Always re-generate project files when `.srcjars` change (this
78includes `R.java`).
79***
80
jbudorick900a8d832016-07-18 18:56:0581## Android Studio Tips
82
Andrew Grieve17b03142017-10-04 14:50:0983* Using the Java debugger is documented at [android_debugging_instructions.md#android-studio](android_debugging_instructions.md#android-studio).
wnwen9032b352017-02-09 15:53:3384* Configuration instructions can be found
85 [here](http://tools.android.com/tech-docs/configuration). One suggestions:
86 * Launch it with more RAM:
87 `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-stable/bin/studio-launcher.sh`
wnwend65703a2016-11-24 18:47:3188* If you ever need to reset it: `rm -r ~/.AndroidStudio*/`
Boris Sazonova084e8f92017-09-14 12:29:1889* Import Chromium-specific style and inspections settings:
wnwen9032b352017-02-09 15:53:3390 * Help -> Find Action -> "Code Style" (settings) -> Java ->
Boris Sazonova084e8f92017-09-14 12:29:1891 Scheme -> Import Scheme
92 * Select `tools/android/android_studio/ChromiumStyle.xml` -> OK
93 * Help -> Find Action -> "Inspections" (settings) ->
94 Profile -> Import profile
95 * Select `tools/android/android_studio/ChromiumInspections.xml` -> OK
wnwend65703a2016-11-24 18:47:3196* Turn on automatic import:
97 * Help -> Find Action -> "Auto Import"
98 * Tick all the boxes under "Java" and change the dropdown to "All".
wnwen67b64972016-12-19 19:53:1599* Turn on documentation on mouse hover:
100 * Help -> Find Action -> "Show quick documentation on mouse move"
101* Turn on line numbers:
102 * Help -> Find Action -> "Show line numbers"
zpeng38bf00de2017-05-10 16:05:03103* Format changed files (Useful for changes made by running code inspection):
104 * Set up version control
105 * File -> Settings -> Version Control
106 * Add src directories
107 * Commit changes and reformat
108 * Help -> Find Action -> "Commit Changes"
109 * Check "Reformat code" & "Optimize imports" and commit
agrieve0c28e4f2016-09-22 01:05:20110
111### Useful Shortcuts
112
wnwend65703a2016-11-24 18:47:31113* `Shift - Shift`: Search to open file or perform IDE action
114* `Ctrl + N`: Jump to class
115* `Ctrl + Shift + T`: Jump to test
116* `Ctrl + Shift + N`: Jump to file
117* `Ctrl + F12`: Jump to method
118* `Ctrl + G`: Jump to line
119* `Shift + F6`: Rename variable
120* `Ctrl + Alt + O`: Organize imports
121* `Alt + Enter`: Quick Fix (use on underlined errors)
wnwenfdea62ce2017-04-05 13:12:54122* `F2`: Find next error
jbudorick900a8d832016-07-18 18:56:05123
agrievec62a52d2016-09-22 01:48:24124### Building from the Command Line
125
wnwen9032b352017-02-09 15:53:33126Gradle builds can be done from the command-line after importing the project
127into Android Studio (importing into the IDE causes the Gradle wrapper to be
128added). This wrapper can also be used to invoke gradle commands.
agrievec62a52d2016-09-22 01:48:24129
130 cd $GRADLE_PROJECT_DIR && bash gradlew
131
132The resulting artifacts are not terribly useful. They are missing assets,
133resources, native libraries, etc.
134
wnwen9032b352017-02-09 15:53:33135* Use a
136 [gradle daemon](https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html)
137 to speed up builds using the gradlew script:
138 * Add the line `org.gradle.daemon=true` to `~/.gradle/gradle.properties`,
139 creating it if necessary.
wnwen660b1312016-10-21 16:46:22140
Peter Wena959a6c2017-11-06 18:24:36141## Status (as of Nov 1, 2017)
agrievec62a52d2016-09-22 01:48:24142
143### What works
144
Peter Wena959a6c2017-11-06 18:24:36145* Android Studio v3.0 and v3.1 canary with `--canary` flag.
Andrew Grieve5b89b232017-11-21 18:41:10146* Java editing and gradle compile (mostly).
wnwene397fec62017-01-19 20:25:21147* Instrumentation tests included as androidTest.
148* Symlinks to existing .so files in jniLibs (doesn't generate them).
Andrew Grieve5b89b232017-11-21 18:41:10149* Editing resource xml files
150* Layout editor (somewhat :P).
estevenson8c9318ff2017-03-10 22:16:35151* Java debugging (see
wnwenfdea62ce2017-04-05 13:12:54152[here](/docs/android_debugging_instructions.md#Android-Studio)).
wnwen03427bc2017-04-19 19:22:24153* Import resolution and refactoring across all modules.
wnwene851ad472017-04-27 16:21:42154* Correct lint and AndroidManifest when only one target is specified.
agrievec62a52d2016-09-22 01:48:24155
156### What doesn't work (yet) ([crbug](https://bugs.chromium.org/p/chromium/issues/detail?id=620034))
157
wnwen03427bc2017-04-19 19:22:24158* Gradle being aware of assets.
Andrew Grieve17b03142017-10-04 14:50:09159* Native code editing.
160* Having the "Make Project" button work correctly.