blob: 7708a51781cb266e8cf72ca72e27a988255ee372 [file] [log] [blame] [view]
AndroidX Core Team2e416b22020-12-03 22:58:07 +00001# Getting started
2
3[TOC]
4
5This page describes how to set up your workstation to check out source code,
6make simple changes in Android Studio, and upload commits to Gerrit for review.
7
8This page does **not** cover best practices for the content of changes. Please
9see [Life of a Jetpack Feature](loaf.md) for details on developing and releasing
10a library, [API Guidelines](api_guidelines.md) for best practices regarding
AndroidX Core Team9e6c2362021-08-30 12:22:59 -070011public APIs and an overview of the constraints placed on changes.
AndroidX Core Team2e416b22020-12-03 22:58:07 +000012
13## Workstation setup {#setup}
14
AndroidX Core Team0db91f02021-05-06 22:45:18 +000015You will need to install the
16[`repo`](https://source.android.com/setup/develop#repo) tool, which is used for
17Git branch and commit management. If you want to learn more about `repo`, see
18the [Repo Command Reference](https://source.android.com/setup/develop/repo).
AndroidX Core Team2e416b22020-12-03 22:58:07 +000019
20### Linux and MacOS {#setup-linux-mac}
21
22First, download `repo` using `curl`.
23
24```shell
25test -d ~/bin || mkdir ~/bin
26curl https://storage.googleapis.com/git-repo-downloads/repo \
27 > ~/bin/repo && chmod 700 ~/bin/repo
28```
29
AndroidX Core Team685fbcd2022-01-10 14:18:55 -080030Then, modify `~/.zshrc` (or `~/.bash_profile` if using `bash`) to ensure you can
AndroidX Core Team21ccf652022-04-01 14:53:07 +000031find local binaries from the command line. We assume you're using `zsh`, but the
AndroidX Core Team685fbcd2022-01-10 14:18:55 -080032following should work with `bash` as well.
AndroidX Core Team2e416b22020-12-03 22:58:07 +000033
34```shell
35export PATH=~/bin:$PATH
36```
37
AndroidX Core Team68274512022-04-28 13:10:15 -070038Next, add the following lines to `~/.zshrc` (or `~/.bash_profile` if using
39`bash`) aliasing the `repo` command to run with `python3`:
AndroidX Core Team2e416b22020-12-03 22:58:07 +000040
41```shell
42# Force repo to run with Python3
43function repo() {
AndroidX Core Team21ccf652022-04-01 14:53:07 +000044 command python3 ~/bin/repo $@
AndroidX Core Team2e416b22020-12-03 22:58:07 +000045}
46```
47
AndroidX Core Team68274512022-04-28 13:10:15 -070048Finally, you will need to either start a new terminal session or run `source
49~/.zshrc` (or `source ~/.bash_profile` if using `bash`) to enable the changes.
50
51> NOTE: If you encounter the following warning about Python 2 being no longer
52> supported, you will need to install Python 3 from the
53> [official website](https://www.python.org).
54>
55> ```shell {.bad}
56> repo: warning: Python 2 is no longer supported; Please upgrade to Python 3.6+.
57> ```
58
59> NOTE: If you encounter an SSL `CERTIFICATE_VERIFY_FAILED` error:
60>
61> ```shell {.bad}
62> Downloading Repo source from https://gerrit.googlesource.com/git-repo
63> fatal: Cannot get https://gerrit.googlesource.com/git-repo/clone.bundle
64> fatal: error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (\_ssl.c:997)
65> ```
66>
67> Run the `Install Certificates.command` in the Python folder of Application.
68> For more information about SSL/TLS certificate validation, you can read the
69> "Important Information" displayed during Python installation.
AndroidX Core Team21ccf652022-04-01 14:53:07 +000070
AndroidX Core Team2e416b22020-12-03 22:58:07 +000071### Windows {#setup-win}
72
73Sorry, Windows is not a supported platform for AndroidX development.
74
75## Set up access control {#access}
76
77### Authenticate to AOSP Gerrit {#access-gerrit}
78
79Before you can upload changes, you will need to associate your Google
80credentials with the AOSP Gerrit code review system by signing in to
81[android-review.googlesource.com](https://android-review.googlesource.com) at
82least once using the account you will use to submit patches.
83
84Next, you will need to
85[set up authentication](https://android-review.googlesource.com/new-password).
86This will give you a shell command to update your local Git cookies, which will
87allow you to upload changes.
88
89Finally, you will need to accept the
90[CLA for new contributors](https://android-review.googlesource.com/settings/new-agreement).
91
92## Check out the source {#source}
93
94Like ChromeOS, Chromium, and the Android build system, we develop in the open as
95much as possible. All feature development occurs in the public
AndroidX Core Team408c27b2020-12-15 15:57:00 +000096[androidx-main](https://android.googlesource.com/platform/frameworks/support/+/androidx-main)
AndroidX Core Team2e416b22020-12-03 22:58:07 +000097branch of the Android Open Source Project.
98
99As of 2020/03/20, you will need about 38 GB for a fully-built checkout.
100
101### Synchronize the branch {#source-checkout}
102
AndroidX Core Team4e1909a2021-10-20 15:04:04 +0000103Use the following commands to check out your branch.
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000104
AndroidX Core Teamf5f77ab2021-01-05 10:56:15 -0500105#### Public main development branch {#androidx-main}
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000106
107All development should occur in this branch unless otherwise specified by the
108AndroidX Core team.
109
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000110The following command will check out the public main development branch:
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000111
112```shell
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000113mkdir androidx-main && cd androidx-main
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000114repo init -u https://android.googlesource.com/platform/manifest \
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000115 -b androidx-main --partial-clone --clone-filter=blob:limit=10M
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000116repo sync -c -j8
117```
118
119NOTE On MacOS, if you receive an SSL error like `SSL: CERTIFICATE_VERIFY_FAILED`
120you may need to install Python3 and boot strap the SSL certificates in the
121included version of pip. You can execute `Install Certificates.command` under
122`/Applications/Python 3.6/` to do so.
123
AndroidX Core Teamf74ae232022-04-25 11:17:51 -0400124NOTE On MacOS, if you receive a Repo or GPG error like `repo: error: "gpg"
125failed with exit status -6` with cause `md_enable: algorithm 10 not available`
126you may need to install a build of `gpg` that supports SHA512, such as the
127latest version available from [Homebrew](https://brew.sh/) using `brew install
128gpg`.
129
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000130### Increase Git rename limit {#source-config}
131
132To ensure `git` can detect diffs and renames across significant changes (namely,
133the `androidx.*` package rename), we recommend that you set the following `git
134config` properties:
135
136```shell
137git config --global merge.renameLimit 999999
138git config --global diff.renameLimit 999999
139```
140
AndroidX Core Teamc2e3ad52021-08-17 13:40:01 -0400141### To check out older source, use the superproject
142
143The
144[git superproject](https://android.googlesource.com/platform/superproject/+/androidx-main)
145contains a history of the matching exact commits of each git repository over
146time, and it can be
147[checked out directly via git](https://stackoverflow.com/questions/3796927/how-to-git-clone-including-submodules)
148
alanve9101e42022-01-28 12:05:11 -0800149### Troubleshooting
150
151> NOTE: If the repo manifest changes -- for example when we update the version
152> of `platform-tools` by pointing it to a different git project -- you may see
153> the following error during`repo sync`:
154>
155> ```shell
156> error.GitError: Cannot fetch --force-sync not enabled; cannot overwrite a local work tree.
157> ...
158> error: Unable to fully sync the tree.
159> error: Downloading network changes failed.
160> ```
161>
162> This indicates that Studio or some other process has made changes in the git
163> project that has been replaced or removed. You can force `repo sync` to
164> discard these changes and check out the correct git project by adding the
165> `--force-sync` argument:
166>
167> ```shell
168> repo sync -j32 --force-sync
169> ```
170
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000171## Explore source code from a browser {#code-search}
172
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000173`androidx-main` has a publicly-accessible
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000174[code search](https://cs.android.com/androidx/platform/frameworks/support) that
175allows you to explore all of the source code in the repository. Links to this
AndroidX Core Team37584142021-02-25 17:58:46 +0000176URL may be shared on the public issue tracked and other external sites.
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000177
178We recommend setting up a custom search engine in Chrome as a faster (and
179publicly-accessible) alternative to `cs/`.
180
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000181### Custom search engine for `androidx-main` {#custom-search-engine}
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000182
1831. Open `chrome://settings/searchEngines`
1841. Click the `Add` button
1851. Enter a name for your search engine, ex. "AndroidX Code Search"
1861. Enter a keyword, ex. "csa"
1871. Enter the following URL:
188 `https://cs.android.com/search?q=%s&ss=androidx%2Fplatform%2Fframeworks%2Fsupport`
1891. Click the `Add` button
190
191Now you can select the Chrome omnibox, type in `csa` and press tab, then enter a
192query to search for, e.g. `AppCompatButton file:appcompat`, and press the
193`Enter` key to get to the search result page.
194
195## Develop in Android Studio {#studio}
196
197Library development uses a curated version of Android Studio to ensure
198compatibility between various components of the development workflow.
199
AndroidX Core Teamee1457a2021-02-25 16:13:10 +0000200From the `frameworks/support` directory, you can use `./studiow m` (short for
201`ANDROIDX_PROJECTS=main ./gradlew studio`) to automatically download and run the
AndroidX Core Team23c50442021-05-18 13:03:40 -0400202correct version of Studio to work on the `main` set of androidx projects
203(non-Compose Jetpack libraries).
AndroidX Core Teamee1457a2021-02-25 16:13:10 +0000204[studiow](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:studiow)
205also supports several other arguments like `all` for other subsets of the
AndroidX Core Team23c50442021-05-18 13:03:40 -0400206projects (run `./studiow` for help).
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000207
208Next, open the `framework/support` project root from your checkout. If Studio
209asks you which SDK you would like to use, select `Use project SDK`. Importing
210projects may take a while, but once that finishes you can use Studio as you
211normally would for application or library development -- right-click on a test
212or sample to run or debug it, search through classes, and so on.
213
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000214If you get a “Unregistered VCS root detected” message, click “Add root” to
215enable the Git/VCS integration for Android Studio.
216
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000217If you see any errors (red underlines), click Gradle's elephant button in the
218toolbar ("Sync Project with Gradle Files") and they should resolve once the
219build completes.
220
221> NOTE: You should choose "Use project SDK" when prompted by Studio. If you
222> picked "Android Studio SDK" by mistake, don't panic! You can fix this by
223> opening `File > Project Structure > Platform Settings > SDKs` and manually
224> setting the Android SDK home path to
225> `<project-root>/prebuilts/fullsdk-<platform>`.
226
227> NOTE: If Android Studio's UI looks scaled up, ex. twice the size it should be,
228> you may need to add the following line to your `studio64.vmoptions` file using
229> `Help -> Edit Custom VM Options`:
230>
231> ```
232> -Dsun.java2d.uiScale.enabled=false
233> ```
234
AndroidX Core Teamee1457a2021-02-25 16:13:10 +0000235If in the future you encounter unexpected errors in Studio and you want to check
236for the possibility it is due to some incorrect settings or other generated
237files, you can run `./studiow --clean main <project subset>` or `./studiow
238--reinstall <project subset>` to clean generated files or reinstall Studio.
239
AndroidX Core Teame80aab72021-09-29 08:44:33 -0700240> Tip: If you don't see a specific Gradle task listed in Studio's Gradle pane,
241> check the following:
242>
243> * Studio might be running a different project subset than the one intended.
244> For example, `./studiow main` only loads the `main` set of androidx
245> projects; run `./studiow compose` to load the tasks specific to Compose.
246>
247> * Gradle tasks aren't being loaded. Under Studio's settings => Experimental,
248> make sure that "Do not build Gradle task list during Gradle sync" is
249> unchecked. (Note that unchecking this can reduce Studio's performance)
250
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000251## Making changes {#changes}
252
AndroidX Core Team5c914c42021-02-08 17:22:57 +0000253Similar to Android framework development, library development should occur in
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000254CL-specific working branches. Use `repo` to create, upload, and abandon local
255branches. Use `git` to manage changes within a local branch.
256
257```shell
258cd path/to/checkout/frameworks/support/
259repo start my_branch_name .
260# make necessary code changes
261# use git to commit changes
262repo upload --cbr -t .
263```
264
265The `--cbr` switch automatically picks the current repo branch for upload. The
AndroidX Core Team0db91f02021-05-06 22:45:18 +0000266`-t` switch sets the Gerrit topic to the branch name, e.g. `my-branch-name`. You
267can refer to the
268[Android documentation](https://source.android.com/setup/create/coding-tasks#workflow)
269for a high level overview of this basic workflow.
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000270
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000271If you see the following prompt, choose `always`:
272
273```
274Run hook scripts from https://android.googlesource.com/platform/manifest (yes/always/NO)?
275```
276
277If the upload succeeds, you'll see an output like:
278
279```
280remote:
281remote: New Changes:
282remote: https://android-review.googlesource.com/c/platform/frameworks/support/+/720062 Further README updates
283remote:
284```
285
286To edit your change, use `git commit --amend`, and re-upload.
287
AndroidX Core Teamee1457a2021-02-25 16:13:10 +0000288NOTE If you encounter issues with `repo upload`, consider running upload with
289trace enabled, e.g. `GIT_DAPPER_TRACE=1 repo --trace upload . --cbr -y`. These
290logs can be helpful for reporting issues to the team that manages our git
291servers.
292
AndroidX Core Team03b4da32021-03-10 23:20:41 +0000293NOTE If `repo upload` or any `git` command hangs and causes your CPU usage to
294skyrocket (e.g. your laptop fan sounds like a jet engine), then you may be
295hitting a rare issue with Git-on-Borg and HTTP/2. You can force `git` and `repo`
296to use HTTP/1.1 with `git config --global http.version HTTP/1.1`.
297
AndroidX Core Teamdeda2cf2021-08-06 15:14:40 -0700298### Fixing Kotlin code style errors
299
300`repo upload` automatically runs `ktlint`, which will cause the upload to fail
301if your code has style errors, which it reports on the command line like so:
302
303```
304[FAILED] ktlint_hook
305 [path]/MessageListAdapter.kt:36:69: Missing newline before ")"
306```
307
308To find and fix these errors, you can run ktlint locally, either in a console
309window or in the Terminal tool in Android Studio. Running in the Terminal tool
310is preferable because it will surface links to your source files/lines so you
311can easily navigate to the code to fix any problems.
312
313First, to run the tool and see all of the errors, run:
314
315`./gradlew module:submodule:ktlint`
316
317where module/submodule are the names used to refer to the module you want to
318check, such as `navigation:navigation-common`. You can also run ktlint on the
319entire project, but that takes longer as it is checking all active modules in
320your project.
321
322Many of the errors that ktlint finds can be automatically fixed by running
323ktlintFormat:
324
325`./gradlew module:submodule:ktlintFormat`
326
327ktlintFormat will report any remaining errors, but you can also run `ktlint`
328again at any time to see an updated list of the remaining errors.
329
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000330## Building {#building}
331
332### Modules and Maven artifacts {#modules-and-maven-artifacts}
333
334To build a specific module, use the module's `assemble` Gradle task. For
335example, if you are working on `core` module use:
336
337```shell
338./gradlew core:core:assemble
339```
340
AndroidX Core Team03b4da32021-03-10 23:20:41 +0000341To make warnings fail your build (same as presubmit), use the `--strict` flag,
342which our gradlew expands into a few correctness-related flags including
343`-Pandroidx.allWarningsAsErrors`:
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000344
345```shell
AndroidX Core Team03b4da32021-03-10 23:20:41 +0000346./gradlew core:core:assemble --strict
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000347```
348
349To build every module, run the Lint verifier, verify the public API surface, and
350generate the local Maven repository artifact, use the `createArchive` Gradle
351task:
352
353```shell
354./gradlew createArchive
355```
356
AndroidX Core Team03b4da32021-03-10 23:20:41 +0000357To run the complete build task that our build servers use, use the corresponding
358shell script:
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000359
360```shell
AndroidX Core Team03b4da32021-03-10 23:20:41 +0000361./busytown/androidx.sh
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000362```
363
364### Attaching a debugger to the build
365
366Gradle tasks, including building a module, may be run or debugged from Android
367Studio's `Gradle` pane by finding the task to be debugged -- for example,
368`androidx > androidx > appcompat > appcompat > build > assemble` --
369right-clicking on it, and then selecting `Debug...`.
370
371Note that debugging will not be available until Gradle sync has completed.
372
AndroidX Core Teamee9c1aa2021-04-06 17:29:05 +0000373#### From the command line
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000374
375Tasks may also be debugged from the command line, which may be useful if
AndroidX Core Teamee1457a2021-02-25 16:13:10 +0000376`./studiow` cannot run due to a Gradle task configuration issue.
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000377
3781. From the configurations dropdown in Studio, select "Edit Configurations".
3791. Click the plus in the top left to create a new "Remote" configuration. Give
380 it a name and hit "Ok".
3811. Set breakpoints.
3821. Run your task with added flags: `./gradlew <your_task_here>
383 -Dorg.gradle.debug=true --no-daemon`
3841. Hit the "Debug" button to the right of the configuration dropdown to attach
385 to the process.
386
387#### Troubleshooting the debugger
388
389If you get a "Connection refused" error, it's likely because a gradle daemon is
390still running on the port specified in the config, and you can fix this by
391killing the running gradle daemons:
392
393```shell
394./gradlew --stop
395```
396
AndroidX Core Teamee9c1aa2021-04-06 17:29:05 +0000397NOTE This is described in more detail in this
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000398[Medium article](https://medium.com/grandcentrix/how-to-debug-gradle-plugins-with-intellij-eef2ef681a7b).
399
400#### Attaching to an annotation processor
401
402Annotation processors run as part of the build, to debug them is similar to
403debugging the build.
404
405For a Java project:
406
407```shell
408./gradlew <your_project>:compileDebugJava --no-daemon --rerun-tasks -Dorg.gradle.debug=true
409```
410
411For a Kotlin project:
412
413```shell
414./gradlew <your_project>:compileDebugKotlin --no-daemon --rerun-tasks -Dorg.gradle.debug=true -Dkotlin.compiler.execution.strategy="in-process" -Dkotlin.daemon.jvm.options="-Xdebug,-Xrunjdwp:transport=dt_socket\,address=5005\,server=y\,suspend=n"
415```
416
417### Optional: Enabling internal menu in IntelliJ/Studio
418
419To enable tools such as `PSI tree` inside of IntelliJ/Studio to help debug
420Android Lint checks and Metalava, you can enable the
421[internal menu](https://www.jetbrains.org/intellij/sdk/docs/reference_guide/internal_actions/enabling_internal.html)
422which is typically used for plugin and IDE development.
423
424### Reference documentation {#docs}
425
426Our reference docs (Javadocs and KotlinDocs) are published to
427https://developer.android.com/reference/androidx/packages and may be built
428locally.
429
430NOTE `./gradlew tasks` always has the canonical task information! When in doubt,
431run `./gradlew tasks`
432
433#### Javadocs
434
435To build API reference docs for tip-of-tree Java source code, run the Gradle
436task:
437
438```
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000439./gradlew doclavaDocs
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000440```
441
AndroidX Core Team8e522ba2021-09-14 07:07:45 -0700442Places the documentation in
443`{androidx-main}/out/androidx/docs-tip-of-tree/build/javadoc`
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000444
445#### KotlinDocs
446
447To build API reference docs for tip-of-tree Kotlin source code, run the Gradle
448task:
449
450```
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000451./gradlew dokkaKotlinDocs
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000452```
453
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000454Places the documentation in
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000455`{androidx-main}/out/androidx/docs-tip-of-tree/build/dokkaKotlinDocs`
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000456
AndroidX Core Teame1288a72021-09-03 12:30:13 -0700457#### Dackka docs
458
459To build API reference docs for both Java and Kotlin source code using Dackka,
460run the Gradle task:
461
462```
463./gradlew dackkaDocs
464```
465
466Location of generated refdocs:
467
468* docs-public (what is published to DAC):
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000469 `{androidx-main}/out/androidx/docs-public/build/dackkaDocs`
AndroidX Core Teame1288a72021-09-03 12:30:13 -0700470* docs-tip-of-tree:
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000471 `{androidx-main}/out/androidx/docs-tip-of-tree/build/dackkaDocs`
AndroidX Core Teame1288a72021-09-03 12:30:13 -0700472
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000473#### Release docs
474
475To build API reference docs for published artifacts formatted for use on
476[d.android.com](http://d.android.com), run the Gradle command:
477
478```
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000479./gradlew zipDoclavaDocs
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000480```
481
482This will create the artifact
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000483`{androidx-main}/out/dist/doclava-public-docs-0.zip`. This command builds docs
484based on the version specified in
AndroidX Core Team4e1909a2021-10-20 15:04:04 +0000485`{androidx-main-checkout}/frameworks/support/docs-public/build.gradle` and uses
486the prebuilt checked into
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000487`{androidx-main-checkout}/prebuilts/androidx/internal/androidx/`. We
AndroidX Core Team4e1909a2021-10-20 15:04:04 +0000488colloquially refer to this two step process of (1) updating `docs-public` and
489(2) checking in a prebuilt artifact into the prebuilts directory as
490[The Prebuilts Dance](releasing_detailed.md#the-prebuilts-dance™). So, to build
491javadocs that will be published to
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000492https://developer.android.com/reference/androidx/packages, both of these steps
493need to be completed.
494
495Once you done the above steps, Kotlin docs will also be generated, with the only
496difference being that we use the Gradle command:
497
498```
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000499./gradlew zipDokkaDocs
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000500```
501
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000502This will create the artifact `{androidx-main}/out/dist/dokka-public-docs-0.zip`
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000503
AndroidX Core Teame1288a72021-09-03 12:30:13 -0700504To generate a zip artifact for both Java and Kotlin source code using Dackka:
505
506```
507./gradlew zipDackkaDocs
508```
509
510This will create the artifact
511`{androidx-main}/out/dist/dackka-public-docs-0.zip`
512
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000513### Updating public APIs {#updating-public-apis}
514
515Public API tasks -- including tracking, linting, and verifying compatibility --
516are run under the following conditions based on the `androidx` configuration
517block, evaluated in order:
518
519* `runApiTasks=Yes` => yes
520* `runApiTasks=No` => no
521* `toolingProject=true` => no
522* `mavenVersion` or group version not set => no
523* Has an existing `api/` directory => yes
524* `publish=SNAPSHOT_AND_RELEASE` => yes
525* Otherwise, no
526
527If you make changes to tracked public APIs, you will need to acknowledge the
528changes by updating the `<component>/api/current.txt` and associated API files.
529This is handled automatically by the `updateApi` Gradle task:
530
531```shell
532# Run updateApi for all modules.
533./gradlew updateApi
534
535# Run updateApi for a single module, ex. appcompat-resources in group appcompat.
536./gradlew :appcompat:appcompat-resources:updateApi
537```
538
539If you change the public APIs without updating the API file, your module will
540still build **but** your CL will fail Treehugger presubmit checks.
541
alanva5fd21b2021-08-20 10:26:46 -0700542#### What are all these files in `api/`? {#updating-public-apis-glossary}
543
544Historical API surfaces are tracked for compatibility and docs generation
545purposes. For each version -- including `current` to represent the tip-of-tree
546version -- we record three different types of API surfaces.
547
548* `<version>.txt`: Public API surface, tracked for compatibility
549* `restricted_<version>.txt`: `@RestrictTo` API surface, tracked for
550 compatibility where necessary (see
551 [Restricted APIs](api_guidelines.md#restricted-api))
552* `public_plus_experimental_<version>.txt`: Public API surface plus
553 `@RequiresOptIn` experimental API surfaces used for documentation (see
554 [Experimental APIs](api_guidelines.md#experimental-api)) and API review
555
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000556### Release notes & the `Relnote:` tag {#relnote}
557
558Prior to releasing, release notes are pre-populated using a script and placed
559into a Google Doc. The Google Doc is manually double checked by library owners
560before the release goes live. To auto-populate your release notes, you can use
561the semi-optional commit tag `Relnote:` in your commit, which will automatically
562include that message the commit in the pre-populated release notes.
563
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000564The presence of a `Relnote:` tag is required for API changes in `androidx-main`.
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000565
566#### How to use it?
567
568One-line release note:
569
570``` {.good}
571Relnote: Fixed a critical bug
572```
573
574``` {.good}
575Relnote: "Fixed a critical bug"
576```
577
578``` {.good}
579Relnote: Added the following string function: `myFoo(\"bar\")`
580```
581
582Multi-line release note:
583
584Note: If the following lines do not contain an indent, you may hit b/165570183.
585
586``` {.good}
587Relnote: "We're launching this awesome new feature! It solves a whole list of
588 problems that require a lot of explaining! "
589```
590
591``` {.good}
592Relnote: """Added the following string function: `myFoo("bar")`
593 It will fix cases where you have to call `myFoo("baz").myBar("bar")`
594 """
595```
596
597Opt out of the Relnote tag:
598
599``` {.good}
600Relnote: N/A
601```
602
603``` {.good}
604Relnote: NA
605```
606
607NOT VALID:
608
609``` {.bad}
610Relnote: This is an INVALID multi-line release note. Our current scripts won't
611include anything beyond the first line. The script has no way of knowing when
612the release note actually stops.
613```
614
615``` {.bad}
616Relnote: This is an INVALID multi-line release note. "Quotes" need to be
617 escaped in order for them to be parsed properly.
618```
619
620### Common build errors
621
622#### Diagnosing build failures
623
624If you've encountered a build failure and you're not sure what is triggering it,
625then please run
626`./development/diagnose-build-failure/diagnose-build-failure.sh`.
627
628This script can categorize your build failure into one of the following
629categories:
630
631* The Gradle Daemon is saving state in memory and triggering a failure
632* Your source files have been changed and/or incompatible git commits have
633 been checked out
634* Some file in the out/ dir is triggering an error
635 * If this happens, diagnose-build-failure.sh should also identify which
636 file(s) specifically
637* The build is nondeterministic and/or affected by timestamps
638* The build via gradlew actually passes and this build failure is specific to
639 Android Studio
640
641Some more-specific build failures are listed below in this page.
642
643#### Out-of-date platform prebuilts
644
645Like a normal Android library developed in Android Studio, libraries within
646`androidx` are built against prebuilts of the platform SDK. These are checked in
647to the `prebuilts/fullsdk-darwin/platforms/<android-version>` directory.
648
649If you are developing against pre-release platform APIs in the internal
650`androidx-platform-dev` branch, you may need to update these prebuilts to obtain
651the latest API changes.
652
AndroidX Core Teamee9c1aa2021-04-06 17:29:05 +0000653#### Missing external dependency
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000654
655If Gradle cannot resolve a dependency listed in your `build.gradle`, you may
656need to import the corresponding artifact into `prebuilts/androidx/external`.
657Our workflow does not automatically download artifacts from the internet to
658facilitate reproducible builds even if remote artifacts are changed.
659
660You can download a dependency by running:
661
662```shell
663cd frameworks/support && ./development/importMaven/import_maven_artifacts.py -n 'someGroupId:someArtifactId:someVersion'
664```
665
666This will create a change within the `prebuilts/androidx/external` directory.
667Make sure to upload this change before or concurrently (ex. in the same Gerrit
668topic) with the dependent library code.
669
670Libraries typically reference dependencies using constants defined in
AndroidX Core Teama20829b2021-12-09 10:25:57 -0800671[`libs.versions.toml`](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:gradle/libs.versions.toml),
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000672so please update this file to include a constant for the version of the library
673that you have checked in. You will reference this constant in your library's
674`build.gradle` dependencies.
675
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000676#### Dependency verification
677
678If the new dependency you are importing is unsigned, or is signed with a new,
679unrecognized key, then you will need to add new dependency verification metadata
680to indicate to Gradle that this new dependency is trusted. Instructions for how
681to do this are currently in the
682[README](https://android.googlesource.com/platform/frameworks/support/+/androidx-main/gradle/README.md)
683in the development subfolder
684
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000685#### Updating an existing dependency
686
687If an older version of a dependency prebuilt was already checked in, please
688manually remove it within the same CL that adds the new prebuilt. You will also
689need to update `Dependencies.kt` to reflect the version change.
690
691#### My gradle build fails with "Cannot invoke method getURLs() on null object"
692
693You're using Java 9's javac, possibly because you ran envsetup.sh from the
694platform build or specified Java 9 as the global default Java compiler. For the
695former, you can simply open a new shell and avoid running envsetup.sh. For the
696latter, we recommend you set Java 8 as the default compiler using sudo
697update-java-alternatives; however, if you must use Java 9 as the default then
698you may alternatively set JAVA_HOME to the location of the Java 8 SDK.
699
700#### My gradle build fails with "error: cannot find symbol" after making framework-dependent changes.
701
702You probably need to update the prebuilt SDK used by the gradle build. If you
703are referencing new framework APIs, you will need to wait for the framework
704changes to land in an SDK build (or build it yourself) and then land in both
705prebuilts/fullsdk and prebuilts/sdk. See
706[Updating SDK prebuilts](playbook.md#prebuilts-fullsdk) for more information.
707
708#### How do I handle refactoring a framework API referenced from a library?
709
710Because AndroidX must compile against both the current framework and the latest
711SDK prebuilt, and because compiling the SDK prebuilt depends on AndroidX, you
712will need to refactor in stages: Remove references to the target APIs from
713AndroidX Perform the refactoring in the framework Update the framework prebuilt
714SDK to incorporate changes in (2) Add references to the refactored APIs in
715AndroidX Update AndroidX prebuilts to incorporate changes in (4)
716
717## Testing {#testing}
718
719AndroidX libraries are expected to include unit or integration test coverage for
720100% of their public API surface. Additionally, all CLs must include a `Test:`
721stanza indicating which tests were used to verify correctness. Any CLs
722implementing bug fixes are expected to include new regression tests specific to
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000723the issue being fixed.
724
725### Running Tests
726
727#### Single Test Class or Method
728
7291. Open the desired test file in Android Studio.
7302. Right-click on a test class or @Test method name and select `Run FooBarTest`
731
732#### Full Test Package
733
7341. In the project side panel open the desired module.
7352. Find the directory with the tests
7363. Right-click on the directory and select `Run androidx.foobar`
737
738### Running Sample Apps
739
740The AndroidX repository has a set of Android applications that exercise AndroidX
741code. These applications can be useful when you want to debug a real running
742application, or reproduce a problem interactively, before writing test code.
743
744These applications are named either `<libraryname>-integration-tests-testapp`,
745or `support-\*-demos` (e.g. `support-v4-demos` or `support-leanback-demos`). You
746can run them by clicking `Run > Run ...` and choosing the desired application.
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000747
748See the [Testing](testing.md) page for more resources on writing, running, and
749monitoring tests.
750
751### AVD Manager
752
AndroidX Core Teamee1457a2021-02-25 16:13:10 +0000753The Android Studio instance started by `./studiow` uses a custom SDK directory,
754which means any virtual devices created by a "standard" non-AndroidX instance of
AndroidX Core Teame1288a72021-09-03 12:30:13 -0700755Android Studio will be *visible* from the `./studiow` instance but will be
AndroidX Core Teamee1457a2021-02-25 16:13:10 +0000756unable to locate the SDK artifacts -- they will display a `Download` button.
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000757
758You can either use the `Download` button to download an extra copy of the SDK
AndroidX Core Teame1288a72021-09-03 12:30:13 -0700759artifacts *or* you can set up a symlink to your "standard" non-AndroidX SDK
AndroidX Core Teamee1457a2021-02-25 16:13:10 +0000760directory to expose your existing artifacts to the `./studiow` instance:
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000761
762```shell
763# Using the default MacOS Android SDK directory...
764ln -s /Users/$(whoami)/Library/Android/sdk/system-images \
765 ../../prebuilts/fullsdk-darwin/system-images
766```
767
768### Benchmarking {#testing-benchmarking}
769
770Libraries are encouraged to write and monitor performance benchmarks. See the
AndroidX Core Team5a4c72f2022-01-12 09:34:38 -0800771[Benchmarking](benchmarking.md) and [Macrobenchmarking](macrobenchmarking.md)
772pages for more details, and the
773[Benchmarking section of d.android.com](http://d.android.com/benchmark) for more
774info on these tools.
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000775
776## Library snapshots {#snapshots}
777
AndroidX Core Teamee9c1aa2021-04-06 17:29:05 +0000778### Quick how-to
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000779
780Add the following snippet to your build.gradle file, replacing `buildId` with a
AndroidX Core Teamee9c1aa2021-04-06 17:29:05 +0000781snapshot build ID.
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000782
783```groovy {highlight=context:[buildId]}
784allprojects {
785 repositories {
786 google()
787 jcenter()
788 maven { url 'https://androidx.dev/snapshots/builds/[buildId]/artifacts/repository' }
789 }
790}
791```
792
AndroidX Core Teamee9c1aa2021-04-06 17:29:05 +0000793You must define dependencies on artifacts using the `SNAPSHOT` version suffix,
794for example:
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000795
796```groovy {highlight=context:SNAPSHOT}
797dependencies {
798 implementation "androidx.core:core:1.2.0-SNAPSHOT"
799}
800```
801
802### Where to find snapshots
803
804If you want to use unreleased `SNAPSHOT` versions of `androidx` artifacts, you
805can find them on either our public-facing build server:
806
807`https://ci.android.com/builds/submitted/<build_id>/androidx_snapshot/latest`
808
809or on our slightly-more-convenient [androidx.dev](https://androidx.dev) site:
810
811`https://androidx.dev/snapshots/builds/<build-id>/artifacts/repository` for a
812specific build ID
813
814`https://androidx.dev/snapshots/builds/latest/artifacts/repository` for
815tip-of-tree snapshots
816
817### Obtaining a build ID
818
819To browse build IDs, you can visit either
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000820[androidx-main](https://ci.android.com/builds/branches/aosp-androidx-main/grid?)
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000821on ci.android.com or [Snapshots](https://androidx.dev/snapshots/builds) on the
822androidx.dev site.
823
824Note that if you are using androidx.dev, you may substitute `latest` for a build
825ID to use the last known good build.
826
827To manually find the last known good `build-id`, you have several options.
828
829#### Snapshots on androidx.dev
830
831[Snapshots](https://androidx.dev/snapshots/builds) on androidx.dev only lists
832usable builds.
833
834#### Programmatically via `jq`
835
836Install `jq`:
837
838```shell
839sudo apt-get install jq
840```
841
842```shell
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000843ID=`curl -s "https://ci.android.com/builds/branches/aosp-androidx-main/status.json" | jq ".targets[] | select(.ID==\"aosp-androidx-main.androidx_snapshot\") | .last_known_good_build"` \
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000844 && echo https://ci.android.com/builds/submitted/"${ID:1:-1}"/androidx_snapshot/latest/raw/repository/
845```
846
847#### Android build server
848
849Go to
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000850[androidx-main](https://ci.android.com/builds/branches/aosp-androidx-main/grid?)
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000851on ci.android.com.
852
853For `androidx-snapshot` target, wait for the green "last known good build"
854button to load and then click it to follow it to the build artifact URL.
855
856### Using in a Gradle build
857
858To make these artifacts visible to Gradle, you need to add it as a respository:
859
860```groovy
861allprojects {
862 repositories {
863 google()
864 maven {
865 // For all Jetpack libraries (including Compose)
866 url 'https://androidx.dev/snapshots/builds/<build-id>/artifacts/repository'
867 }
868 }
869}
870```
871
872Note that the above requires you to know the `build-id` of the snapshots you
873want.
874
875#### Specifying dependencies
876
877All artifacts in the snapshot repository are versioned as `x.y.z-SNAPSHOT`. So
878to use a snapshot artifact, the version in your `build.gradle` will need to be
879updated to `androidx.<groupId>:<artifactId>:X.Y.Z-SNAPSHOT`
880
881For example, to use the `core:core:1.2.0-SHAPSHOT` snapshot, you would add the
882following to your `build.gradle`:
883
884```
885dependencies {
886 ...
887 implementation("androidx.core:core:1.2.0-SNAPSHOT")
888 ...
889}
890```
891
892## FAQ {#faq}
893
894### How do I test my change in a separate Android Studio project? {#faq-test-change-studio}
895
896If you're working on a new feature or bug fix in AndroidX, you may want to test
897your changes against another project to verify that the change makes sense in a
898real-world context or that a bug's specific repro case has been fixed.
899
900If you need to be absolutely sure that your test will exactly emulate the
901developer's experience, you can repeatedly build the AndroidX archive and
902rebuild your application. In this case, you will need to create a local build of
903AndroidX's local Maven repository artifact and install it in your Android SDK
904path.
905
906First, use the `createArchive` Gradle task to generate the local Maven
907repository artifact:
908
909```shell
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000910# Creates <path-to-checkout>/out/androidx/build/support_repo/
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000911./gradlew createArchive
912```
913
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000914Using for your alternate (non-AndroidX) version of Android Studio open the
915project's 'build.gradle' and add the following within 'repositories' to make
916Android Gradle Plugin look for binaries in newly built repository:
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000917
918```groovy
919allprojects {
920 repositories {
921 ...
922 maven {
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000923 url "<path-to-sdk>/out/androidx/build/support_repo/"
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000924 }
925 }
926}
927```
928
929NOTE Gradle resolves dependencies in the order that the repositories are defined
930(if 2 repositories can resolve the same dependency, the first listed will do so
931and the second will not). Therefore, if the library you are testing has the same
932group, artifact, and version as one already published, you will want to list
933your custom maven repo first.
934
935Finally, in the dependencies section of your standalone project's `build.gradle`
936file, add or update the `implementation` entries to reflect the AndroidX modules
937that you would like to test. Example:
938
939```
940dependencies {
941 ...
942 implementation "androidx.appcompat:appcompat::1.0.0-alpha02"
943}
944```
945
946If you are testing your changes in the Android Platform code, you can replace
947the module you are testing
948`YOUR_ANDROID_PATH/prebuilts/sdk/current/androidx/m2repository` with your own
949module. We recommend only replacing the module you are modifying instead of the
950full m2repository to avoid version issues of other modules. You can either take
951the unzipped directory from
952`<path-to-checkout>/out/dist/top-of-tree-m2repository-##.zip`, or from
953`<path-to-checkout>/out/androidx/build/support_repo/` after buiding `androidx`.
954Here is an example of replacing the RecyclerView module:
955
956```shell
957$TARGET=YOUR_ANDROID_PATH/prebuilts/sdk/current/androidx/m2repository/androidx/recyclerview/recyclerview/1.1.0-alpha07;
958rm -rf $TARGET;
959cp -a <path-to-sdk>/extras/m2repository/androidx/recyclerview/recyclerview/1.1.0-alpha07 $TARGET
960```
961
962Make sure the library versions are the same before and after replacement. Then
963you can build the Android platform code with the new `androidx` code.
AndroidX Core Team4cc85fa2021-11-23 15:58:34 +0000964
965### How do I measure library size? {#library-size}
966
967Method count and bytecode size are tracked in CI
968[alongside benchmarks](benchmarking.md#monitoring) to detect regressions.
969
970For local measurements, use the `:reportLibraryMetrics` task. For example:
971
972```shell
973./gradlew benchmark:benchmark-macro:reportLibraryMetrics
974cat ../../out/dist/librarymetrics/androidx.benchmark_benchmark-macro.json
975```
976
977Will output something like: `{"method_count":1256,"bytecode_size":178822}`
978
979Note: this only counts the weight of your library's jar/aar, including
980resources. It does not count library dependencies. It does not account for a
981minification step (e.g. with R8), as that is dynamic, and done at app build time
982(and depend on which entrypoints the app uses).
AndroidX Core Teamf74ae232022-04-25 11:17:51 -0400983
984### How do I add content to a library's Overview reference doc page?
985
986Put content in a markdown file that ends with `-documentation.md` in the
987directory that corresponds to the Overview page that you'd like to document.
988
989For example, the `androidx.compose.runtime`
990[Overview page](https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary)
991includes content from
992[compose-runtime-documentation.md](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/compose-runtime-documentation.md).