File: android_studio.md

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (126 lines) | stat: -rw-r--r-- 4,713 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Android Studio

[TOC]

## Usage

Make sure you have followed [android build instructions](android_build_instructions.md) already.

```shell
build/android/gradle/generate_gradle.py
```

This creates a project at `out/Debug/gradle`. To create elsewhere:

```shell
build/android/gradle/generate_gradle.py --output-directory out/My-Out-Dir --project-dir my-project
```

By default, only common targets are generated. To customize the list of targets
to generate projects for:

```shell
build/android/gradle/generate_gradle.py --target //some:target_apk --target //some/other:target_apk
```

For first-time Android Studio users:

* Avoid running the setup wizard.
    * The wizard will force you to download unwanted SDK components to `//third_party/android_tools`.
    * To skip it, select "Cancel" when it comes up.

To import the project:

* Use "Import Project", and select the directory containing the generated project, by default `out-gn/Debug/gradle`.

You need to re-run `generate_gradle.py` whenever `BUILD.gn` files change.

* After regenerating, Android Studio should prompt you to "Sync". If it doesn't, use:
    * Help -> Find Action -> "Sync Project with Gradle Files"


## How it Works

Android Studio integration works by generating `build.gradle` files based on GN
targets. Each `android_apk` and `android_library` target produces a separate
Gradle sub-project.

### Symlinks and .srcjars

Gradle supports source directories but not source files. However, some
`java/src/` directories in Chromium are split amonst multiple GN targets. To
accommodate this, the script detects such targets and creates a `symlinked-java/`
directory to point gradle at. Be warned that creating new files from Android
Studio within these symlink-based projects will cause new files to be created in
the generated `symlinked-java/` rather than the source tree where you want it.

*** note
** TLDR:** Always create new files outside of Android Studio.
***

Most generated .java files in GN are stored as `.srcjars`. Android Studio does
not have support for them, and so the generator script builds and extracts them
all to `extracted-srcjars/` subdirectories for each target that contains them.

*** note
** TLDR:** Always re-generate project files when `.srcjars` change (this
includes `R.java`).
***

## Android Studio Tips

* Configuration instructions can be found [here](http://tools.android.com/tech-docs/configuration). One suggestions:
    * Launch it with more RAM: `STUDIO_VM_OPTIONS=-Xmx2048m /opt/android-studio-stable/bin/studio-launcher.sh`
* If you ever need to reset it: `rm -r ~/.AndroidStudio*/`
* Import Android style settings:
    * Help -> Find Action -> "Code Style" (settings) -> Java -> Manage -> Import
        * Select `third_party/android_platform/development/ide/intellij/codestyles/AndroidStyle.xml`
* Turn on automatic import:
    * Help -> Find Action -> "Auto Import"
        * Tick all the boxes under "Java" and change the dropdown to "All".
* Turn on documentation on mouse hover:
    * Help -> Find Action -> "Show quick documentation on mouse move"
* Turn on line numbers:
    * Help -> Find Action -> "Show line numbers"

### Useful Shortcuts

* `Shift - Shift`: Search to open file or perform IDE action
* `Ctrl + N`: Jump to class
* `Ctrl + Shift + T`: Jump to test
* `Ctrl + Shift + N`: Jump to file
* `Ctrl + F12`: Jump to method
* `Ctrl + G`: Jump to line
* `Shift + F6`: Rename variable
* `Ctrl + Alt + O`: Organize imports
* `Alt + Enter`: Quick Fix (use on underlined errors)

### Building from the Command Line

Gradle 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.

* Use a [gradle daemon](https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html) to speed up builds:
    * Add the line `org.gradle.daemon=true` to `~/.gradle/gradle.properties`, creating it if necessary.

## Status (as of Jan 19, 2017)

### What works

* Tested with Android Studio v2.2.
* Java editing and gradle compile works.
* Instrumentation tests included as androidTest.
* Symlinks to existing .so files in jniLibs (doesn't generate them).

### What doesn't work (yet) ([crbug](https://bugs.chromium.org/p/chromium/issues/detail?id=620034))

* Make gradle aware of resources and assets
* Add a mode in which gradle is responsible for generating `R.java`
* Add support for native code editing
* Make the "Make Project" button work correctly