blob: 3ece0443bf9ca384d511964438bd094e48195de5 [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
Peter Wena2d96112018-05-10 20:29:4311build/android/gradle/generate_gradle.py --output-directory out/Debug
jbudorick900a8d832016-07-18 18:56:0512```
13
Peter Wencb519572018-05-16 17:39:1814The above commands create a project dir `gradle` under your output directory.
15Use `--project-dir <project-dir>` to change this.
Anthony Berent8d3ce022018-01-15 12:24:3616
Peter Wenefd17272018-05-23 18:51:4017To import the project:
18* Use "Import Project", and select the directory containing the generated
19 project, e.g. `out/Debug/gradle`.
Anthony Berent8d3ce022018-01-15 12:24:3620
Ken Rockot35028ca2019-05-30 18:50:4521See [android_test_instructions.md](testing/android_test_instructions.md#Using-Emulators)
Peter Wenefd17272018-05-23 18:51:4022for more information about building and running emulators.
agrieve0c28e4f2016-09-22 01:05:2023
Peter Wenfac7ae42023-01-31 02:24:3524Feel free to accept Android Studio's recommended actions. `generate_gradle.py`
25should have already set up a working version of the gradle wrapper and the
26android gradle plugin, as well as a default Android SDK location at
27`~/Android/Sdk`. Since the same script needs to support various versions of
28Android Studio, the defaults may have lower version than the one recommended by
29your version of Android Studio. After you accept Android Studio's update actions
30the `generate_gradle.py` script will try to keep the newer versions when it is
31re-run.
Andrew Grieve17b03142017-10-04 14:50:0932
Peter Wenfac7ae42023-01-31 02:24:3533You'll need to re-run `generate_gradle.py` whenever new directories containing
34source files are added.
Peter Wena2d96112018-05-10 20:29:4335
wnwen9032b352017-02-09 15:53:3336* After regenerating, Android Studio should prompt you to "Sync". If it
Peter Wena2d96112018-05-10 20:29:4337 doesn't, try some of the following options:
38 * File -&gt; "Sync Project with Gradle Files"
wnwen0227e4a52017-02-09 21:14:5139 * Button with two arrows on the right side of the top strip.
wnwend65703a2016-11-24 18:47:3140 * Help -&gt; Find Action -&gt; "Sync Project with Gradle Files"
wnwen0227e4a52017-02-09 21:14:5141 * After `gn clean` you may need to restart Android Studio.
Peter Wena2d96112018-05-10 20:29:4342 * File -&gt; "Invalidate Caches / Restart..."
agrieve0c28e4f2016-09-22 01:05:2043
wnwen03427bc2017-04-19 19:22:2444## How It Works
agrieve0c28e4f2016-09-22 01:05:2045
Peter Wena2d96112018-05-10 20:29:4346By default, only an `_all` module containing all java apk targets is generated.
47If just one apk target is explicitly specified, then a single apk module is
48generated.
wnwene851ad472017-04-27 16:21:4249
Peter Wenfac7ae42023-01-31 02:24:3550If you really prefer a more detailed structure of gn targets, the deprecated
51`--split-projects` flag can be used. This will generate one module for every gn
52target in the dependency graph. This can be very slow and is no longer
53supported.
agrieve0c28e4f2016-09-22 01:05:2054
Peter Wenfac7ae42023-01-31 02:24:3555### Generated files
agrieve0c28e4f2016-09-22 01:05:2056
57Most generated .java files in GN are stored as `.srcjars`. Android Studio does
Peter Wenfac7ae42023-01-31 02:24:3558not support them. Our build will automatically extract them to a
59`generated_java` directory in the output directory during the build. Thus if a
60generated file is missing in Android Studio, build it with ninja first and it
61should show up in Android Studio afterwards.
agrieve0c28e4f2016-09-22 01:05:2062
Peter Wena2d96112018-05-10 20:29:4363### Native Files
64
Peter Wenfac7ae42023-01-31 02:24:3565This option is deprecated and no longer supported since Android Studio is very
66slow when editing in a code base with a large number of C++ files, and Chromium
67has a lot of C++ code. It is recommended to use [VS Code](vscode.md) to edit
68native files and stick to just editing java files in Android Studio.
Peter Wend46a8672023-01-24 20:36:1069
70If you still want to enable editing native C/C++ files with Android Studio, pass
71in any number of `--native-target [target name]` flags in order to use it. The
72target must be the full path and name of a valid gn target (no short-forms).
73This will require you to install `cmake` and `ndk` when prompted. Accept Android
74Studio's prompts for these SDK packages.
Peter Wen8393de82018-05-17 20:58:5475
Peter Wenf8cc36fe2019-01-09 17:52:3176You need to disable a new gradle option in order to edit native files:
77File -&gt; Settings -&gt; Experimental
78-&gt; Gradle and uncheck "Only resolve selected variants".
79
Peter Wen8393de82018-05-17 20:58:5480This is not necessary, but to avoid "This file is not part of the project...",
81you can either add an extra `--native-target` flag or simply copy and paste the
82absolute path to that file into the CMakeLists.txt file alongside the existing
Peter Wenf8cc36fe2019-01-09 17:52:3183file paths. Note that changes to CMakeLists.txt will be overwritten on your next
84invocation of `generate_gradle.py`.
Peter Wen8393de82018-05-17 20:58:5485
86Example:
Peter Wena2d96112018-05-10 20:29:4387
88```shell
Richard Heb08a6942020-08-11 00:36:2689build/android/gradle/generate_gradle.py --native-target //chrome/android:libchrome
Peter Wena2d96112018-05-10 20:29:4390```
91
Peter Wencb519572018-05-16 17:39:1892## Tips
jbudorick900a8d832016-07-18 18:56:0593
Peter Wencb519572018-05-16 17:39:1894* Use environment variables to avoid having to specify `--output-directory`.
95 * Example: Append `export CHROMIUM_OUT_DIR=out; export BUILDTYPE=Debug` to
96 your `~/.bashrc` to always default to `out/Debug`.
Peter Wenfac7ae42023-01-31 02:24:3597* Using the Java debugger is documented
98 [here](android_debugging_instructions.md#android-studio).
wnwen9032b352017-02-09 15:53:3399* Configuration instructions can be found
100 [here](http://tools.android.com/tech-docs/configuration). One suggestions:
101 * Launch it with more RAM:
102 `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-stable/bin/studio-launcher.sh`
Peter Wenfac7ae42023-01-31 02:24:35103* If you ever need to reset it: `rm -r ~/.config/Google/AndroidStudio*/`
Boris Sazonova084e8f92017-09-14 12:29:18104* Import Chromium-specific style and inspections settings:
wnwen9032b352017-02-09 15:53:33105 * Help -&gt; Find Action -&gt; "Code Style" (settings) -&gt; Java -&gt;
Boris Sazonova084e8f92017-09-14 12:29:18106 Scheme -&gt; Import Scheme
107 * Select `tools/android/android_studio/ChromiumStyle.xml` -&gt; OK
108 * Help -&gt; Find Action -&gt; "Inspections" (settings) -&gt;
109 Profile -&gt; Import profile
110 * Select `tools/android/android_studio/ChromiumInspections.xml` -&gt; OK
wnwend65703a2016-11-24 18:47:31111* Turn on automatic import:
112 * Help -&gt; Find Action -&gt; "Auto Import"
113 * Tick all the boxes under "Java" and change the dropdown to "All".
wnwen67b64972016-12-19 19:53:15114* Turn on documentation on mouse hover:
115 * Help -&gt; Find Action -&gt; "Show quick documentation on mouse move"
116* Turn on line numbers:
117 * Help -&gt; Find Action -&gt; "Show line numbers"
Peter Wena2d96112018-05-10 20:29:43118* Turn off indent notification:
119 * Help -&gt; Find Action -&gt; "Show notifications about detected indents"
zpeng38bf00de2017-05-10 16:05:03120* Format changed files (Useful for changes made by running code inspection):
121 * Set up version control
122 * File -&gt; Settings -&gt; Version Control
123 * Add src directories
124 * Commit changes and reformat
125 * Help -&gt; Find Action -&gt; "Commit Changes"
126 * Check "Reformat code" & "Optimize imports" and commit
Peter Wencf4236de2018-05-23 19:26:36127* Change theme from GTK+ to another one to avoid invisible menus.
128 * Help -&gt; Find Action -&gt; "Theme: Settings > Appearance"
agrieve0c28e4f2016-09-22 01:05:20129
130### Useful Shortcuts
131
wnwend65703a2016-11-24 18:47:31132* `Shift - Shift`: Search to open file or perform IDE action
133* `Ctrl + N`: Jump to class
134* `Ctrl + Shift + T`: Jump to test
135* `Ctrl + Shift + N`: Jump to file
136* `Ctrl + F12`: Jump to method
137* `Ctrl + G`: Jump to line
138* `Shift + F6`: Rename variable
139* `Ctrl + Alt + O`: Organize imports
140* `Alt + Enter`: Quick Fix (use on underlined errors)
wnwenfdea62ce2017-04-05 13:12:54141* `F2`: Find next error
jbudorick900a8d832016-07-18 18:56:05142
Peter Wenfac7ae42023-01-31 02:24:35143### Building with Gradle
agrievec62a52d2016-09-22 01:48:24144
Peter Wenfac7ae42023-01-31 02:24:35145Gradle builds are not supported. Only editing is supported in Android Studio.
146Use ninja to build as usual.
wnwen660b1312016-10-21 16:46:22147
Peter Wend46a8672023-01-24 20:36:10148## Status
agrievec62a52d2016-09-22 01:48:24149
150### What works
151
Peter Wenfac7ae42023-01-31 02:24:35152* Android Studio v2021~v2023.
Peter Wena2d96112018-05-10 20:29:43153* Java editing.
Peter Wen9222aa2f2018-06-13 17:50:06154 * Application code in `main` sourceset.
155 * Instrumentation test code in `androidTest` sourceset.
Peter Wend46a8672023-01-24 20:36:10156* Native code editing (deprecated, use [VS Code](vscode.md) instead).
wnwene397fec62017-01-19 20:25:21157* Symlinks to existing .so files in jniLibs (doesn't generate them).
Andrew Grieve5b89b232017-11-21 18:41:10158* Editing resource xml files
Peter Wena2d96112018-05-10 20:29:43159* Layout editor (limited functionality).
estevenson8c9318ff2017-03-10 22:16:35160* Java debugging (see
wnwenfdea62ce2017-04-05 13:12:54161[here](/docs/android_debugging_instructions.md#Android-Studio)).
Peter Wena2d96112018-05-10 20:29:43162* Import resolution and refactoring across java files.
Peter Wen667abd52018-05-17 00:12:25163* Separate Android SDK for Android Studio.
agrievec62a52d2016-09-22 01:48:24164
Peter Wend46a8672023-01-24 20:36:10165### What doesn't work
agrievec62a52d2016-09-22 01:48:24166
Peter Wenfac7ae42023-01-31 02:24:35167* Building with Gradle.
168* The "Make Project" button doesn't work.
Peter Wend46a8672023-01-24 20:36:10169 * Stick to using `autoninja` to build targets and just use Android Studio
170 for editing java source files.
171* No active work is underway or planned to expand Android Studio support.