README.md
1## SystemUI in Android Studio (internal documentation)
2
3```sh
4$ ./packages/apps/ManagedProvisioning/studio-dev/studiow
5```
6
7### Setup
8- Run this command from the root of the initialized repo directory to start android studio
9```sh
10$ ./packages/apps/ManagedProvisioning/studio-dev/studiow
11```
12- Make changes and run `SystemUI` run configuration from Android Studio (or `SystemUITitan` when working with tablets). This configuration for system UI injects custom shell function to remount and replace the apk instead of using pm install, using a different run configuration will not work.
13
14##### -studio branch (useful for mac)
15You can alternatively checkout the sysui-studio repo branch `xxx-sysui-studio-dev` (e.g.: [master-sysui-studio-dev](https://goto.google.com/repo-init/master-sysui-studio-dev). This tracks the same git branches as the corresponding `xxx` branch but with minimal dependencies.
16Command line `make` will not work on this branch as it only tracks a small number of projects to keep the checkout as small as possible (as of late 2022: 55G with [partial clones](https://goto.google.com/git-reaper) of all repositories).
17
18### First run
19- Make sure to have the rooted device connected. The script pulls the dex files from the device and decompiles them to create an SDK with hidden APIs.
20- If import settings dialog shows up in Android Studio, select do-not import.
21- If sdk wizard shows up, cancel and select never run in future.
22- If the project do not open (happens when sdk wizard shows up), close Android Studio and run `studiow` again.
23- First time you install systemUI, you might need to
24 - run 'adb disable-verity' (which requires a reboot)
25 - reboot your device for dexopt to kick in
26
27### Running tests
28You should be able to run instrumented tests in AndroidStudio using `SystemUI` configuration.
29
30### Updating SDK
31If after a sync, you are unable to compile, it's probably because some API in framework changed and
32you need to update your SDK. Depending on how you checked out your tree, there are two ways to
33update the SDK. In either case, you should pass the `--update-sdk` flag.
34
35For a minimal studiow-dev checkout, the SDK must be pulled from the device. Flash your device with
36latest image (corresponding to the tree you are working on) from [Flashstation](http://go/flash) and
37update the sdk using the `--update-sdk` flag:
38
39```sh
40$ ./packages/apps/ManagedProvisioning/studio-dev/studiow --update-sdk
41```
42
43For a platform checkout, you have the option to use the built SDK from your tree's out/ directory.
44This SDK exists if you've previously built with `m`. The script will prefer to use the built SDK if
45it exists, otherwise it will attempt to pull the SDK from the attached device. You can pass
46`--pull-sdk` to override this behavior and _always_ pull the SDK from the attached device, whether
47or not the built SDK exists.
48
49For example:
50 - If you are using a sysui-studio checkout, it will always pull the SDK from the attached device.
51 - If you are using a platform checkout which you've never built, it will pull the SDK from the
52 attached device.
53 - If you are using a platform checkout which you've built with `m`, it will use the SDK from the
54 out/ directory. However, in this scenario, if you wanted to use the SDK from the attached device
55 instead you can pass `--pull-sdk`.
56
57### Adding dependencies
58
59When you add new dependencies to SystemUI (Android.bp files), they will also need to be added
60in the sysui-studio gradle files in SysUIGradleProject/ before they are available in your project.
61These dependencies should stay in sync - don't add dependencies to Gradle that aren't in Soong.
62
63## FAQ / Helpful info
64
65This project is using android studio + gradle as a build system, and as such it can be tricky to bootstrap for a first time set up or after a sync. This section is for some common problem/solution pairs we run into and also some general things to try.
66
67#### We have both flavors, country _and_ western
68
69Remember that this project can be run from a full platform checkout **or** a minimal studio-dev checkout.
70
71#### Make sure the sdk is updated after a sync
72
73 `./studiow --update-sdk` is your friend.
74
75 We pull the framework.jar (&friends) from the device and do some `<magic>` to compile against hidden framework APIs. Therefore, you have to build and flash the device to be current, and then pull the resulting jars from the device using the above command.
76
77 > NOTE: if you're using the studio-dev branch (minimal checkout), then you want to ensure that the device image is as close in time to your local checkout as possible. You'll want to flash the device to the most recent possible build when you do a sync. Platform builds will always be in lock-step so long as you build after syncing.
78
79#### Android sdk choice
80
81Android Studio shouldn't ask you to choose which Android SDK to use and will instead select the
82right SDK automatically. But, [if it does ask](https://screenshot.googleplex.com/AtA62tTRyKWiSWg),
83choose the **project** SDK (likely in the `.../<branchname>/prebuilts/fullsdk-linux` directory),
84**not** the Android SDK (likely in the `.../<username>/Android/Sdk` directory).
85
86#### Javac `@IntDef`s compiler error
87
88You will find `@IntDef` clauses all over the platform. It sometimes works in ASwB-studio but breaks the build at sysui-studio-dev. The reason is `Java 8 javac` has some issue to deal with this but not `Java 9 javac`. The workaround solution would be to avoid the static imports.
89
90Build errors (cannot find symbol @IntDef) in Java 8 javac:
91```
92import static com.example.myapplication.MainActivity.LockTypes.PASSWORD;
93import static com.example.myapplication.MainActivity.LockTypes.PIN;
94import static com.example.myapplication.MainActivity.LockTypes.PATTERN;
95
96@IntDef({
97 PASSWORD,
98 PIN,
99 PATTERN
100})
101@interface LockTypes {
102 int PASSWORD = 0;
103 int PIN = 1;
104 int PATTERN = 2;
105}
106```
107
108Workaround to avoid the static imports:
109```
110@IntDef({
111 LockTypes.PASSWORD,
112 LockTypes.PIN,
113 LockTypes.PATTERN
114})
115@interface LockTypes {
116 int PASSWORD = 0;
117 int PIN = 1;
118 int PATTERN = 2;
119}
120```
121You could find more details discussion
122[here](https://buganizer.corp.google.com/issues/67418397).
123
124#### Some other things to think about:
125
1261. Build > clean project
1272. File > Invalidate caches & restart
128
129#### Android Studio is not launching
130
131If Android Studio fails to start when running studio wrapper you can try to launch the binary directly to see more logs.
132
133After running studio wrapper once you can find the binary in `~/.AndroidStudioSystemUI` directory. For example, on macOS it may look like this:
134
135```
136/Users/{{USERNAME}}/.AndroidStudioSystemUI/bin/android-studio-ide-201.6953283-mac/Android Studio.app/Contents/MacOS/studio
137```
138
139One of the issues that you may encounter is merging studio.vmoptions from different Android Studio installations that result into conflicting options.
140
141You may see something similar to this in the logs:
142
143```
144...
1452021-06-02 13:58:37.069 studio[20732:221422] Processing VMOptions file at /Users/{{USERNAME}}/.AndroidStudioSystemUI/bin/android-studio-ide-201.6953283-mac/Android Studio.app/Contents/bin/studio.vmoptions
1462021-06-02 13:58:37.070 studio[20732:221422] Done
1472021-06-02 13:58:37.070 studio[20732:221422] Processing VMOptions file at /Users/{{USERNAME}}/Library/Application Support/Google/AndroidStudio4.1/studio.vmoptions
1482021-06-02 13:58:37.070 studio[20732:221422] Done
1492021-06-02 13:58:37.070 studio[20732:221422] Processing VMOptions file at
1502021-06-02 13:58:37.072 studio[20732:221422] No content found
151Conflicting collector combinations in option list; please refer to the release notes for the combinations allowed
1522021-06-02 13:58:37.079 studio[20732:221422] JNI_CreateJavaVM (/Users/{{USERNAME}}/.AndroidStudioSystemUI/bin/android-studio-ide-201.6953283-mac/Android Studio.app/Contents/jre/jdk) failed: 4294967295
153```
154
155To resolve this you can remove all other Android Studio installations (including `Application Support/Google/AndroidStudio*` directories) and re-launch the wrapper.
156
157If the logs show an error similar to:
158
159```
160Error opening zip file or JAR manifest missing : ../plugins/g3plugins/bin/FileProfilingAgent.jar
161Error occurred during initialization of VM
162agent library failed to init: instrument
163```
164
165you can edit the file
166
167```
168/Users/{{USERNAME}}/Library/Application\ Support/Google/AndroidStudio2021.2/studio.vmoptions
169```
170
171and comment out the line
172
173```
174# -javaagent:../plugins/g3plugins/bin/FileProfilingAgent.jar
175```
176
177and try launching as normal.
178
179
180#### Enable hidden projects
181
182Some projects are hidden in Android Studio (the support app, platform code, and the one search plugin). To enable these projects for debugging and building, for example, the One Search Plugin, you need to uncomment it in `packages/apps/ManagedProvisioning/studio-dev/SysUIGradleProject/settings.gradle` for these lines:
183
184```
185// Uncomment this for OneSearch Plugin app
186// include ':OneSearch'
187// project(':OneSearch').projectDir = new File(rootDir,'../../../NexusLauncher/plugins/OneSearchPlugin')
188```
189
190After syncing gradle, the project should show up in Configurations in Android Studio. You might also need to update the configuration for some projects.
191
192#### Kotlin compiler errors
193
194Sometimes Android Studio encounters a version error when resolving the Kotlin compiler, due to version mismatches.
195To fix this, add `-Xskip-prerelease-check` to the ["Additional command line arguments"](https://screenshot.googleplex.com/5h3FUEx5vjuazD9)
196in the "Kotlin compiler" section of Settings.
197
198#### KVM enabled for CRD
199
200If you are using Chrome remote desktop and see an error like:
201
202```
203Setting up SDK from scratch
204Found ADB at /path/to/adb
205Updating framework.aidl file at: /path/to/framework.aidl
206Updating private apis sdk
207restarting adbd as root
208adb: error: connect failed: closed
209
210```
211
212you might need to enable kvm with the following command:
213
214```
215sudo setfacl -m u:${USER}:rw /dev/kvm
216```
217
218