Make sure you have followed android build instructions already.
build/android/gradle/generate_gradle.py --output-directory out/Debug
This creates a project at out/Debug/gradle
. To create elsewhere:
build/android/gradle/generate_gradle.py --output-directory out/Debug --project-dir my-project
If you are planning to use Android emulators use the --sdk=AndroidStudioDefault or the --sdk-path option, since adding emulator images to the project sdk will modify the project sdk, hence causing problems when you next run gclient sync.
See android_test_instructions.md for more information about building and running emulators.
For first-time Android Studio users:
To import the project:
out/Debug/gradle
.If you‘re asked to use Studio’s Android SDK:
If you‘re asked to use Studio’s Gradle wrapper:
You need to re-run generate_gradle.py
whenever BUILD.gn
files change.
Pass --canary
or --beta
to avoid the “A newer version of gradle is available” notification.
gn clean
you may need to restart Android Studio.By default, only an _all
module containing all java apk targets is generated. If just one apk target is explicitly specified, then a single apk module is generated.
To see more detailed structure of gn targets, the --split-projects
flag can be used. This will generate one module for every gn target in the dependency graph. This can be very slow when used with --all
by default.
Gradle supports source directories but not source files. However, files in Chromium are used amongst multiple targets. To accommodate this, the script detects such targets and creates exclude patterns to exclude files not in the current target. The editor does not respect these exclude patterns, so the _all
pseudo module is added which includes directories from all targets. This allows imports and refactoring to be across all targets.
Most generated .java files in GN are stored as .srcjars
. Android Studio does not support them. It is very slow to build all these generated files and they rarely change. The generator script does not do anything with them by default. If --full
is passed then the generator script builds and extracts them all to extracted-srcjars/
subdirectories for each target that contains them. This is the reason that the _all
pseudo module may contain multiple copies of generated files.
--full
when generated files change ( includes R.java
) and to remove some red underlines in java files.A new experimental option is now available to enable editing native C/C++ files with Android Studio. Pass in any number of --native-target [target name]
flags in order to try it out. This will require cmake
and ndk
packages for your android SDK. This crbug tracks adding those to our android_tools
repository. For the interim accepting Android Studio's prompts should work. Example below.
build/android/gradle/generate_gradle.py --native-target //chrome/android:monochrome
STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-stable/bin/studio-launcher.sh
rm -r ~/.AndroidStudio*/
tools/android/android_studio/ChromiumStyle.xml
-> OKtools/android/android_studio/ChromiumInspections.xml
-> OKShift - Shift
: Search to open file or perform IDE actionCtrl + N
: Jump to classCtrl + Shift + T
: Jump to testCtrl + Shift + N
: Jump to fileCtrl + F12
: Jump to methodCtrl + G
: Jump to lineShift + F6
: Rename variableCtrl + Alt + O
: Organize importsAlt + Enter
: Quick Fix (use on underlined errors)F2
: Find next errorGradle builds can be done from the command-line after importing the project into Android Studio (importing into the IDE causes the Gradle wrapper to be added). This wrapper can also be used to invoke gradle commands.
cd $GRADLE_PROJECT_DIR && bash gradlew
The resulting artifacts are not terribly useful. They are missing assets, resources, native libraries, etc.
org.gradle.daemon=true
to ~/.gradle/gradle.properties
, creating it if necessary.