AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame^] | 1 | # Getting started |
| 2 | |
| 3 | [TOC] |
| 4 | |
| 5 | This page describes how to set up your workstation to check out source code, |
| 6 | make simple changes in Android Studio, and upload commits to Gerrit for review. |
| 7 | |
| 8 | This page does **not** cover best practices for the content of changes. Please |
| 9 | see [Life of a Jetpack Feature](loaf.md) for details on developing and releasing |
| 10 | a library, [API Guidelines](api_guidelines.md) for best practices regarding |
| 11 | public APIs, or [Policies and Processes](policies.md) for an overview of the |
| 12 | constraints placed on changes. |
| 13 | |
| 14 | ## Workstation setup {#setup} |
| 15 | |
| 16 | You will need to install the `repo` tool, which is used for Git branch and |
| 17 | commit management. If you want to learn more about `repo`, see the |
| 18 | [Repo Command Reference](https://source.android.com/setup/develop/repo). |
| 19 | |
| 20 | ### Linux and MacOS {#setup-linux-mac} |
| 21 | |
| 22 | First, download `repo` using `curl`. |
| 23 | |
| 24 | ```shell |
| 25 | test -d ~/bin || mkdir ~/bin |
| 26 | curl https://storage.googleapis.com/git-repo-downloads/repo \ |
| 27 | > ~/bin/repo && chmod 700 ~/bin/repo |
| 28 | ``` |
| 29 | |
| 30 | Then, modify `~/.bash_profile` (if using `bash`) to ensure you can find local |
| 31 | binaries from the command line. |
| 32 | |
| 33 | ```shell |
| 34 | export PATH=~/bin:$PATH |
| 35 | ``` |
| 36 | |
| 37 | You will need to either start a new terminal session or run `source |
| 38 | ~/.bash_profile` to pick up the new path. |
| 39 | |
| 40 | If you encounter an SSL `CERTIFICATE_VERIFY_FAILED` error or warning about |
| 41 | Python 2 being no longer supported, you will need to install Python 3 and alias |
| 42 | your `repo` command to run with `python3`. |
| 43 | |
| 44 | ```shell {.bad} |
| 45 | repo: warning: Python 2 is no longer supported; Please upgrade to Python 3.6+. |
| 46 | ``` |
| 47 | |
| 48 | ```shell {.bad} |
| 49 | Downloading Repo source from https://gerrit.googlesource.com/git-repo |
| 50 | fatal: Cannot get https://gerrit.googlesource.com/git-repo/clone.bundle |
| 51 | fatal: error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777) |
| 52 | ``` |
| 53 | |
| 54 | First, install Python 3 from the [official website](https://www.python.org). |
| 55 | Please read the "Important Information" displayed during installation for |
| 56 | information about SSL/TLS certificate validation and the running the "Install |
| 57 | Certificates.command". |
| 58 | |
| 59 | Next, open your `~/.bash_profile` and add the following lines to wrap the `repo` |
| 60 | command: |
| 61 | |
| 62 | ```shell |
| 63 | # Force repo to run with Python3 |
| 64 | function repo() { |
| 65 | command python3 "$(which repo)" $@ |
| 66 | } |
| 67 | ``` |
| 68 | |
| 69 | ### Windows {#setup-win} |
| 70 | |
| 71 | Sorry, Windows is not a supported platform for AndroidX development. |
| 72 | |
| 73 | ## Set up access control {#access} |
| 74 | |
| 75 | ### Authenticate to AOSP Gerrit {#access-gerrit} |
| 76 | |
| 77 | Before you can upload changes, you will need to associate your Google |
| 78 | credentials with the AOSP Gerrit code review system by signing in to |
| 79 | [android-review.googlesource.com](https://android-review.googlesource.com) at |
| 80 | least once using the account you will use to submit patches. |
| 81 | |
| 82 | Next, you will need to |
| 83 | [set up authentication](https://android-review.googlesource.com/new-password). |
| 84 | This will give you a shell command to update your local Git cookies, which will |
| 85 | allow you to upload changes. |
| 86 | |
| 87 | Finally, you will need to accept the |
| 88 | [CLA for new contributors](https://android-review.googlesource.com/settings/new-agreement). |
| 89 | |
| 90 | ## Check out the source {#source} |
| 91 | |
| 92 | Like ChromeOS, Chromium, and the Android build system, we develop in the open as |
| 93 | much as possible. All feature development occurs in the public |
| 94 | [androidx-master-dev](https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev) |
| 95 | branch of the Android Open Source Project. |
| 96 | |
| 97 | As of 2020/03/20, you will need about 38 GB for a fully-built checkout. |
| 98 | |
| 99 | ### Synchronize the branch {#source-checkout} |
| 100 | |
| 101 | Use the following `repo` commands to check out your branch. |
| 102 | |
| 103 | #### Public master development branch {#source-checkout-master} |
| 104 | |
| 105 | All development should occur in this branch unless otherwise specified by the |
| 106 | AndroidX Core team. |
| 107 | |
| 108 | The following command will check out the public master development branch: |
| 109 | |
| 110 | ```shell |
| 111 | mkdir androidx-master-dev && cd androidx-master-dev |
| 112 | repo init -u https://android.googlesource.com/platform/manifest \ |
| 113 | -b androidx-master-dev --partial-clone --clone-filter=blob:limit=10M |
| 114 | repo sync -c -j8 |
| 115 | ``` |
| 116 | |
| 117 | NOTE On MacOS, if you receive an SSL error like `SSL: CERTIFICATE_VERIFY_FAILED` |
| 118 | you may need to install Python3 and boot strap the SSL certificates in the |
| 119 | included version of pip. You can execute `Install Certificates.command` under |
| 120 | `/Applications/Python 3.6/` to do so. |
| 121 | |
| 122 | ### Increase Git rename limit {#source-config} |
| 123 | |
| 124 | To ensure `git` can detect diffs and renames across significant changes (namely, |
| 125 | the `androidx.*` package rename), we recommend that you set the following `git |
| 126 | config` properties: |
| 127 | |
| 128 | ```shell |
| 129 | git config --global merge.renameLimit 999999 |
| 130 | git config --global diff.renameLimit 999999 |
| 131 | ``` |
| 132 | |
| 133 | ## Explore source code from a browser {#code-search} |
| 134 | |
| 135 | `androidx-master-dev` has a publicly-accessible |
| 136 | [code search](https://cs.android.com/androidx/platform/frameworks/support) that |
| 137 | allows you to explore all of the source code in the repository. Links to this |
| 138 | URL may be shared on public Buganizer and other external sites. |
| 139 | |
| 140 | We recommend setting up a custom search engine in Chrome as a faster (and |
| 141 | publicly-accessible) alternative to `cs/`. |
| 142 | |
| 143 | ### Custom search engine for `androidx-master-dev` {#custom-search-engine} |
| 144 | |
| 145 | 1. Open `chrome://settings/searchEngines` |
| 146 | 1. Click the `Add` button |
| 147 | 1. Enter a name for your search engine, ex. "AndroidX Code Search" |
| 148 | 1. Enter a keyword, ex. "csa" |
| 149 | 1. Enter the following URL: |
| 150 | `https://cs.android.com/search?q=%s&ss=androidx%2Fplatform%2Fframeworks%2Fsupport` |
| 151 | 1. Click the `Add` button |
| 152 | |
| 153 | Now you can select the Chrome omnibox, type in `csa` and press tab, then enter a |
| 154 | query to search for, e.g. `AppCompatButton file:appcompat`, and press the |
| 155 | `Enter` key to get to the search result page. |
| 156 | |
| 157 | ## Develop in Android Studio {#studio} |
| 158 | |
| 159 | Library development uses a curated version of Android Studio to ensure |
| 160 | compatibility between various components of the development workflow. |
| 161 | |
| 162 | From the `frameworks/support` directory, you can use `ANDROIDX_PROJECTS=MAIN |
| 163 | ./gradlew studio` to automatically download and run the correct version of |
| 164 | Studio to work on main set of androidx projects. `ANDROIDX_PROJECTS` has several |
| 165 | other options like `ANDROIDX_PROJECTS=ALL` to open other subsets of the |
| 166 | projects. |
| 167 | [settings.gradle](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:settings.gradle) |
| 168 | file in the repository has these options listed. |
| 169 | |
| 170 | ```shell |
| 171 | ANDROIDX_PROJECTS=MAIN ./gradlew studio |
| 172 | ``` |
| 173 | |
| 174 | Next, open the `framework/support` project root from your checkout. If Studio |
| 175 | asks you which SDK you would like to use, select `Use project SDK`. Importing |
| 176 | projects may take a while, but once that finishes you can use Studio as you |
| 177 | normally would for application or library development -- right-click on a test |
| 178 | or sample to run or debug it, search through classes, and so on. |
| 179 | |
| 180 | If you see any errors (red underlines), click Gradle's elephant button in the |
| 181 | toolbar ("Sync Project with Gradle Files") and they should resolve once the |
| 182 | build completes. |
| 183 | |
| 184 | > NOTE: You should choose "Use project SDK" when prompted by Studio. If you |
| 185 | > picked "Android Studio SDK" by mistake, don't panic! You can fix this by |
| 186 | > opening `File > Project Structure > Platform Settings > SDKs` and manually |
| 187 | > setting the Android SDK home path to |
| 188 | > `<project-root>/prebuilts/fullsdk-<platform>`. |
| 189 | |
| 190 | > NOTE: If Android Studio's UI looks scaled up, ex. twice the size it should be, |
| 191 | > you may need to add the following line to your `studio64.vmoptions` file using |
| 192 | > `Help -> Edit Custom VM Options`: |
| 193 | > |
| 194 | > ``` |
| 195 | > -Dsun.java2d.uiScale.enabled=false |
| 196 | > ``` |
| 197 | |
| 198 | ## Making changes {#changes} |
| 199 | |
| 200 | Similar to Android framework development, library developmnent should occur in |
| 201 | CL-specific working branches. Use `repo` to create, upload, and abandon local |
| 202 | branches. Use `git` to manage changes within a local branch. |
| 203 | |
| 204 | ```shell |
| 205 | cd path/to/checkout/frameworks/support/ |
| 206 | repo start my_branch_name . |
| 207 | # make necessary code changes |
| 208 | # use git to commit changes |
| 209 | repo upload --cbr -t . |
| 210 | ``` |
| 211 | |
| 212 | The `--cbr` switch automatically picks the current repo branch for upload. The |
| 213 | `-t` switch sets the Gerrit topic to the branch name, e.g. `my-branch-name`. |
| 214 | |
| 215 | ## Building {#building} |
| 216 | |
| 217 | ### Modules and Maven artifacts {#modules-and-maven-artifacts} |
| 218 | |
| 219 | To build a specific module, use the module's `assemble` Gradle task. For |
| 220 | example, if you are working on `core` module use: |
| 221 | |
| 222 | ```shell |
| 223 | ./gradlew core:core:assemble |
| 224 | ``` |
| 225 | |
| 226 | Use the `-Pandroidx.allWarningsAsErrors` to make warnings fail your build (same |
| 227 | as presubmits): |
| 228 | |
| 229 | ```shell |
| 230 | ./gradlew core:core:assemble -Pandroidx.allWarningsAsErrors |
| 231 | ``` |
| 232 | |
| 233 | To build every module, run the Lint verifier, verify the public API surface, and |
| 234 | generate the local Maven repository artifact, use the `createArchive` Gradle |
| 235 | task: |
| 236 | |
| 237 | ```shell |
| 238 | ./gradlew createArchive |
| 239 | ``` |
| 240 | |
| 241 | To run the complete build task that our build servers use, use the |
| 242 | `buildOnServer` Gradle task: |
| 243 | |
| 244 | ```shell |
| 245 | ./gradlew buildOnServer |
| 246 | ``` |
| 247 | |
| 248 | ### Attaching a debugger to the build |
| 249 | |
| 250 | Gradle tasks, including building a module, may be run or debugged from Android |
| 251 | Studio's `Gradle` pane by finding the task to be debugged -- for example, |
| 252 | `androidx > androidx > appcompat > appcompat > build > assemble` -- |
| 253 | right-clicking on it, and then selecting `Debug...`. |
| 254 | |
| 255 | Note that debugging will not be available until Gradle sync has completed. |
| 256 | |
| 257 | ## From the command line |
| 258 | |
| 259 | Tasks may also be debugged from the command line, which may be useful if |
| 260 | `./gradlew studio` cannot run due to a Gradle task configuration issue. |
| 261 | |
| 262 | 1. From the configurations dropdown in Studio, select "Edit Configurations". |
| 263 | 1. Click the plus in the top left to create a new "Remote" configuration. Give |
| 264 | it a name and hit "Ok". |
| 265 | 1. Set breakpoints. |
| 266 | 1. Run your task with added flags: `./gradlew <your_task_here> |
| 267 | -Dorg.gradle.debug=true --no-daemon` |
| 268 | 1. Hit the "Debug" button to the right of the configuration dropdown to attach |
| 269 | to the process. |
| 270 | |
| 271 | #### Troubleshooting the debugger |
| 272 | |
| 273 | If you get a "Connection refused" error, it's likely because a gradle daemon is |
| 274 | still running on the port specified in the config, and you can fix this by |
| 275 | killing the running gradle daemons: |
| 276 | |
| 277 | ```shell |
| 278 | ./gradlew --stop |
| 279 | ``` |
| 280 | |
| 281 | Note: This is described in more detail in this |
| 282 | [Medium article](https://medium.com/grandcentrix/how-to-debug-gradle-plugins-with-intellij-eef2ef681a7b). |
| 283 | |
| 284 | #### Attaching to an annotation processor |
| 285 | |
| 286 | Annotation processors run as part of the build, to debug them is similar to |
| 287 | debugging the build. |
| 288 | |
| 289 | For a Java project: |
| 290 | |
| 291 | ```shell |
| 292 | ./gradlew <your_project>:compileDebugJava --no-daemon --rerun-tasks -Dorg.gradle.debug=true |
| 293 | ``` |
| 294 | |
| 295 | For a Kotlin project: |
| 296 | |
| 297 | ```shell |
| 298 | ./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" |
| 299 | ``` |
| 300 | |
| 301 | ### Optional: Enabling internal menu in IntelliJ/Studio |
| 302 | |
| 303 | To enable tools such as `PSI tree` inside of IntelliJ/Studio to help debug |
| 304 | Android Lint checks and Metalava, you can enable the |
| 305 | [internal menu](https://www.jetbrains.org/intellij/sdk/docs/reference_guide/internal_actions/enabling_internal.html) |
| 306 | which is typically used for plugin and IDE development. |
| 307 | |
| 308 | ### Reference documentation {#docs} |
| 309 | |
| 310 | Our reference docs (Javadocs and KotlinDocs) are published to |
| 311 | https://developer.android.com/reference/androidx/packages and may be built |
| 312 | locally. |
| 313 | |
| 314 | NOTE `./gradlew tasks` always has the canonical task information! When in doubt, |
| 315 | run `./gradlew tasks` |
| 316 | |
| 317 | #### Javadocs |
| 318 | |
| 319 | To build API reference docs for tip-of-tree Java source code, run the Gradle |
| 320 | task: |
| 321 | |
| 322 | ``` |
| 323 | ./gradlew disttipOfTreeDocs |
| 324 | ``` |
| 325 | |
| 326 | This will output docs in the zip file: |
| 327 | `{androidx-master-dev}/out/dist/android-support-tipOfTree-docs-0.zip`, as well |
| 328 | as in local html files that you can check from your browser: |
| 329 | `{androidx-master-dev}/out/androidx/build/javadoc/tipOfTree/offline/reference/packages.html` |
| 330 | |
| 331 | #### KotlinDocs |
| 332 | |
| 333 | To build API reference docs for tip-of-tree Kotlin source code, run the Gradle |
| 334 | task: |
| 335 | |
| 336 | ``` |
| 337 | ./gradlew distTipOfTreeDokkaDocs |
| 338 | ``` |
| 339 | |
| 340 | This will output docs in the zip file: |
| 341 | `{androidx-master-dev}/out/dist/dokkaTipOfTreeDocs-0.zip` |
| 342 | |
| 343 | #### Release docs |
| 344 | |
| 345 | To build API reference docs for published artifacts formatted for use on |
| 346 | [d.android.com](http://d.android.com), run the Gradle command: |
| 347 | |
| 348 | ``` |
| 349 | ./gradlew distpublicDocs |
| 350 | ``` |
| 351 | |
| 352 | This will create the artifact |
| 353 | `{androidx-master-dev}/out/dist/android-support-public-docs-0.zip`. This command |
| 354 | builds docs based on the version specified in |
| 355 | `{androidx-master-dev-checkout}/frameworks/support/buildSrc/src/main/kotlin/androidx/build/PublishDocsRules.kt` |
| 356 | and uses the prebuilt checked into |
| 357 | `{androidx-master-dev-checkout}/prebuilts/androidx/internal/androidx/`. We |
| 358 | colloquially refer to this two step process of (1) updating PublishDocsRules.kt |
| 359 | and (2) checking in a prebuilt artifact into the prebuilts directory as |
| 360 | [The Prebuilts Dance](releasing.md#the-prebuilts-danceâ„¢). So, to build javadocs |
| 361 | that will be published to |
| 362 | https://developer.android.com/reference/androidx/packages, both of these steps |
| 363 | need to be completed. |
| 364 | |
| 365 | Once you done the above steps, Kotlin docs will also be generated, with the only |
| 366 | difference being that we use the Gradle command: |
| 367 | |
| 368 | ``` |
| 369 | ./gradlew distPublicDokkaDocs |
| 370 | ``` |
| 371 | |
| 372 | which generates the kotlin docs artifact |
| 373 | `{androidx-master-dev}/out/dist/dokkaPublicDocs-0.zip` |
| 374 | |
| 375 | ### Updating public APIs {#updating-public-apis} |
| 376 | |
| 377 | Public API tasks -- including tracking, linting, and verifying compatibility -- |
| 378 | are run under the following conditions based on the `androidx` configuration |
| 379 | block, evaluated in order: |
| 380 | |
| 381 | * `runApiTasks=Yes` => yes |
| 382 | * `runApiTasks=No` => no |
| 383 | * `toolingProject=true` => no |
| 384 | * `mavenVersion` or group version not set => no |
| 385 | * Has an existing `api/` directory => yes |
| 386 | * `publish=SNAPSHOT_AND_RELEASE` => yes |
| 387 | * Otherwise, no |
| 388 | |
| 389 | If you make changes to tracked public APIs, you will need to acknowledge the |
| 390 | changes by updating the `<component>/api/current.txt` and associated API files. |
| 391 | This is handled automatically by the `updateApi` Gradle task: |
| 392 | |
| 393 | ```shell |
| 394 | # Run updateApi for all modules. |
| 395 | ./gradlew updateApi |
| 396 | |
| 397 | # Run updateApi for a single module, ex. appcompat-resources in group appcompat. |
| 398 | ./gradlew :appcompat:appcompat-resources:updateApi |
| 399 | ``` |
| 400 | |
| 401 | If you change the public APIs without updating the API file, your module will |
| 402 | still build **but** your CL will fail Treehugger presubmit checks. |
| 403 | |
| 404 | ### Release notes & the `Relnote:` tag {#relnote} |
| 405 | |
| 406 | Prior to releasing, release notes are pre-populated using a script and placed |
| 407 | into a Google Doc. The Google Doc is manually double checked by library owners |
| 408 | before the release goes live. To auto-populate your release notes, you can use |
| 409 | the semi-optional commit tag `Relnote:` in your commit, which will automatically |
| 410 | include that message the commit in the pre-populated release notes. |
| 411 | |
| 412 | The presence of a `Relnote:` tag is required for API changes in |
| 413 | `androidx-master-dev`. |
| 414 | |
| 415 | #### How to use it? |
| 416 | |
| 417 | One-line release note: |
| 418 | |
| 419 | ``` {.good} |
| 420 | Relnote: Fixed a critical bug |
| 421 | ``` |
| 422 | |
| 423 | ``` {.good} |
| 424 | Relnote: "Fixed a critical bug" |
| 425 | ``` |
| 426 | |
| 427 | ``` {.good} |
| 428 | Relnote: Added the following string function: `myFoo(\"bar\")` |
| 429 | ``` |
| 430 | |
| 431 | Multi-line release note: |
| 432 | |
| 433 | Note: If the following lines do not contain an indent, you may hit b/165570183. |
| 434 | |
| 435 | ``` {.good} |
| 436 | Relnote: "We're launching this awesome new feature! It solves a whole list of |
| 437 | problems that require a lot of explaining! " |
| 438 | ``` |
| 439 | |
| 440 | ``` {.good} |
| 441 | Relnote: """Added the following string function: `myFoo("bar")` |
| 442 | It will fix cases where you have to call `myFoo("baz").myBar("bar")` |
| 443 | """ |
| 444 | ``` |
| 445 | |
| 446 | Opt out of the Relnote tag: |
| 447 | |
| 448 | ``` {.good} |
| 449 | Relnote: N/A |
| 450 | ``` |
| 451 | |
| 452 | ``` {.good} |
| 453 | Relnote: NA |
| 454 | ``` |
| 455 | |
| 456 | NOT VALID: |
| 457 | |
| 458 | ``` {.bad} |
| 459 | Relnote: This is an INVALID multi-line release note. Our current scripts won't |
| 460 | include anything beyond the first line. The script has no way of knowing when |
| 461 | the release note actually stops. |
| 462 | ``` |
| 463 | |
| 464 | ``` {.bad} |
| 465 | Relnote: This is an INVALID multi-line release note. "Quotes" need to be |
| 466 | escaped in order for them to be parsed properly. |
| 467 | ``` |
| 468 | |
| 469 | ### Common build errors |
| 470 | |
| 471 | #### Diagnosing build failures |
| 472 | |
| 473 | If you've encountered a build failure and you're not sure what is triggering it, |
| 474 | then please run |
| 475 | `./development/diagnose-build-failure/diagnose-build-failure.sh`. |
| 476 | |
| 477 | This script can categorize your build failure into one of the following |
| 478 | categories: |
| 479 | |
| 480 | * The Gradle Daemon is saving state in memory and triggering a failure |
| 481 | * Your source files have been changed and/or incompatible git commits have |
| 482 | been checked out |
| 483 | * Some file in the out/ dir is triggering an error |
| 484 | * If this happens, diagnose-build-failure.sh should also identify which |
| 485 | file(s) specifically |
| 486 | * The build is nondeterministic and/or affected by timestamps |
| 487 | * The build via gradlew actually passes and this build failure is specific to |
| 488 | Android Studio |
| 489 | |
| 490 | Some more-specific build failures are listed below in this page. |
| 491 | |
| 492 | #### Out-of-date platform prebuilts |
| 493 | |
| 494 | Like a normal Android library developed in Android Studio, libraries within |
| 495 | `androidx` are built against prebuilts of the platform SDK. These are checked in |
| 496 | to the `prebuilts/fullsdk-darwin/platforms/<android-version>` directory. |
| 497 | |
| 498 | If you are developing against pre-release platform APIs in the internal |
| 499 | `androidx-platform-dev` branch, you may need to update these prebuilts to obtain |
| 500 | the latest API changes. |
| 501 | |
| 502 | ### Missing external dependency |
| 503 | |
| 504 | If Gradle cannot resolve a dependency listed in your `build.gradle`, you may |
| 505 | need to import the corresponding artifact into `prebuilts/androidx/external`. |
| 506 | Our workflow does not automatically download artifacts from the internet to |
| 507 | facilitate reproducible builds even if remote artifacts are changed. |
| 508 | |
| 509 | You can download a dependency by running: |
| 510 | |
| 511 | ```shell |
| 512 | cd frameworks/support && ./development/importMaven/import_maven_artifacts.py -n 'someGroupId:someArtifactId:someVersion' |
| 513 | ``` |
| 514 | |
| 515 | This will create a change within the `prebuilts/androidx/external` directory. |
| 516 | Make sure to upload this change before or concurrently (ex. in the same Gerrit |
| 517 | topic) with the dependent library code. |
| 518 | |
| 519 | Libraries typically reference dependencies using constants defined in |
| 520 | [`Dependencies.kt`](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:buildSrc/src/main/kotlin/androidx/build/dependencies/Dependencies.kt), |
| 521 | so please update this file to include a constant for the version of the library |
| 522 | that you have checked in. You will reference this constant in your library's |
| 523 | `build.gradle` dependencies. |
| 524 | |
| 525 | #### Updating an existing dependency |
| 526 | |
| 527 | If an older version of a dependency prebuilt was already checked in, please |
| 528 | manually remove it within the same CL that adds the new prebuilt. You will also |
| 529 | need to update `Dependencies.kt` to reflect the version change. |
| 530 | |
| 531 | #### My gradle build fails with "Cannot invoke method getURLs() on null object" |
| 532 | |
| 533 | You're using Java 9's javac, possibly because you ran envsetup.sh from the |
| 534 | platform build or specified Java 9 as the global default Java compiler. For the |
| 535 | former, you can simply open a new shell and avoid running envsetup.sh. For the |
| 536 | latter, we recommend you set Java 8 as the default compiler using sudo |
| 537 | update-java-alternatives; however, if you must use Java 9 as the default then |
| 538 | you may alternatively set JAVA_HOME to the location of the Java 8 SDK. |
| 539 | |
| 540 | #### My gradle build fails with "error: cannot find symbol" after making framework-dependent changes. |
| 541 | |
| 542 | You probably need to update the prebuilt SDK used by the gradle build. If you |
| 543 | are referencing new framework APIs, you will need to wait for the framework |
| 544 | changes to land in an SDK build (or build it yourself) and then land in both |
| 545 | prebuilts/fullsdk and prebuilts/sdk. See |
| 546 | [Updating SDK prebuilts](playbook.md#prebuilts-fullsdk) for more information. |
| 547 | |
| 548 | #### How do I handle refactoring a framework API referenced from a library? |
| 549 | |
| 550 | Because AndroidX must compile against both the current framework and the latest |
| 551 | SDK prebuilt, and because compiling the SDK prebuilt depends on AndroidX, you |
| 552 | will need to refactor in stages: Remove references to the target APIs from |
| 553 | AndroidX Perform the refactoring in the framework Update the framework prebuilt |
| 554 | SDK to incorporate changes in (2) Add references to the refactored APIs in |
| 555 | AndroidX Update AndroidX prebuilts to incorporate changes in (4) |
| 556 | |
| 557 | ## Testing {#testing} |
| 558 | |
| 559 | AndroidX libraries are expected to include unit or integration test coverage for |
| 560 | 100% of their public API surface. Additionally, all CLs must include a `Test:` |
| 561 | stanza indicating which tests were used to verify correctness. Any CLs |
| 562 | implementing bug fixes are expected to include new regression tests specific to |
| 563 | the issue being fixed |
| 564 | |
| 565 | See the [Testing](testing.md) page for more resources on writing, running, and |
| 566 | monitoring tests. |
| 567 | |
| 568 | ### AVD Manager |
| 569 | |
| 570 | The Android Studio instance started by `./gradlew studio` uses a custom SDK |
| 571 | directory, which means any virtual devices created by a "standard" non-AndroidX |
| 572 | instance of Android Studio will be _visible_ from the `./gradlew studio` |
| 573 | instance but will be unable to locate the SDK artifacts -- they will display a |
| 574 | `Download` button. |
| 575 | |
| 576 | You can either use the `Download` button to download an extra copy of the SDK |
| 577 | artifacts _or_ you can set up a symlink to your "standard" non-AndroidX SDK |
| 578 | directory to expose your existing artifacts to the `./gradlew studio` instance: |
| 579 | |
| 580 | ```shell |
| 581 | # Using the default MacOS Android SDK directory... |
| 582 | ln -s /Users/$(whoami)/Library/Android/sdk/system-images \ |
| 583 | ../../prebuilts/fullsdk-darwin/system-images |
| 584 | ``` |
| 585 | |
| 586 | ### Benchmarking {#testing-benchmarking} |
| 587 | |
| 588 | Libraries are encouraged to write and monitor performance benchmarks. See the |
| 589 | [Benchmarking](benchmarking.md) page for more details. |
| 590 | |
| 591 | ## Library snapshots {#snapshots} |
| 592 | |
| 593 | ### Quick how to |
| 594 | |
| 595 | Add the following snippet to your build.gradle file, replacing `buildId` with a |
| 596 | snapshot build Id. |
| 597 | |
| 598 | ```groovy {highlight=context:[buildId]} |
| 599 | allprojects { |
| 600 | repositories { |
| 601 | google() |
| 602 | jcenter() |
| 603 | maven { url 'https://androidx.dev/snapshots/builds/[buildId]/artifacts/repository' } |
| 604 | } |
| 605 | } |
| 606 | ``` |
| 607 | |
| 608 | You must define dependencies on artifacts using the SNAPSHOT version suffix, for |
| 609 | example: |
| 610 | |
| 611 | ```groovy {highlight=context:SNAPSHOT} |
| 612 | dependencies { |
| 613 | implementation "androidx.core:core:1.2.0-SNAPSHOT" |
| 614 | } |
| 615 | ``` |
| 616 | |
| 617 | ### Where to find snapshots |
| 618 | |
| 619 | If you want to use unreleased `SNAPSHOT` versions of `androidx` artifacts, you |
| 620 | can find them on either our public-facing build server: |
| 621 | |
| 622 | `https://ci.android.com/builds/submitted/<build_id>/androidx_snapshot/latest` |
| 623 | |
| 624 | or on our slightly-more-convenient [androidx.dev](https://androidx.dev) site: |
| 625 | |
| 626 | `https://androidx.dev/snapshots/builds/<build-id>/artifacts/repository` for a |
| 627 | specific build ID |
| 628 | |
| 629 | `https://androidx.dev/snapshots/builds/latest/artifacts/repository` for |
| 630 | tip-of-tree snapshots |
| 631 | |
| 632 | ### Obtaining a build ID |
| 633 | |
| 634 | To browse build IDs, you can visit either |
| 635 | [androidx-master-dev](https://ci.android.com/builds/branches/aosp-androidx-master-dev/grid?) |
| 636 | on ci.android.com or [Snapshots](https://androidx.dev/snapshots/builds) on the |
| 637 | androidx.dev site. |
| 638 | |
| 639 | Note that if you are using androidx.dev, you may substitute `latest` for a build |
| 640 | ID to use the last known good build. |
| 641 | |
| 642 | To manually find the last known good `build-id`, you have several options. |
| 643 | |
| 644 | #### Snapshots on androidx.dev |
| 645 | |
| 646 | [Snapshots](https://androidx.dev/snapshots/builds) on androidx.dev only lists |
| 647 | usable builds. |
| 648 | |
| 649 | #### Programmatically via `jq` |
| 650 | |
| 651 | Install `jq`: |
| 652 | |
| 653 | ```shell |
| 654 | sudo apt-get install jq |
| 655 | ``` |
| 656 | |
| 657 | ```shell |
| 658 | ID=`curl -s "https://ci.android.com/builds/branches/aosp-androidx-master-dev/status.json" | jq ".targets[] | select(.ID==\"aosp-androidx-master-dev.androidx_snapshot\") | .last_known_good_build"` \ |
| 659 | && echo https://ci.android.com/builds/submitted/"${ID:1:-1}"/androidx_snapshot/latest/raw/repository/ |
| 660 | ``` |
| 661 | |
| 662 | #### Android build server |
| 663 | |
| 664 | Go to |
| 665 | [androidx-master-dev](https://ci.android.com/builds/branches/aosp-androidx-master-dev/grid?) |
| 666 | on ci.android.com. |
| 667 | |
| 668 | For `androidx-snapshot` target, wait for the green "last known good build" |
| 669 | button to load and then click it to follow it to the build artifact URL. |
| 670 | |
| 671 | ### Using in a Gradle build |
| 672 | |
| 673 | To make these artifacts visible to Gradle, you need to add it as a respository: |
| 674 | |
| 675 | ```groovy |
| 676 | allprojects { |
| 677 | repositories { |
| 678 | google() |
| 679 | maven { |
| 680 | // For all Jetpack libraries (including Compose) |
| 681 | url 'https://androidx.dev/snapshots/builds/<build-id>/artifacts/repository' |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | ``` |
| 686 | |
| 687 | Note that the above requires you to know the `build-id` of the snapshots you |
| 688 | want. |
| 689 | |
| 690 | #### Specifying dependencies |
| 691 | |
| 692 | All artifacts in the snapshot repository are versioned as `x.y.z-SNAPSHOT`. So |
| 693 | to use a snapshot artifact, the version in your `build.gradle` will need to be |
| 694 | updated to `androidx.<groupId>:<artifactId>:X.Y.Z-SNAPSHOT` |
| 695 | |
| 696 | For example, to use the `core:core:1.2.0-SHAPSHOT` snapshot, you would add the |
| 697 | following to your `build.gradle`: |
| 698 | |
| 699 | ``` |
| 700 | dependencies { |
| 701 | ... |
| 702 | implementation("androidx.core:core:1.2.0-SNAPSHOT") |
| 703 | ... |
| 704 | } |
| 705 | ``` |
| 706 | |
| 707 | ## FAQ {#faq} |
| 708 | |
| 709 | ### How do I test my change in a separate Android Studio project? {#faq-test-change-studio} |
| 710 | |
| 711 | If you're working on a new feature or bug fix in AndroidX, you may want to test |
| 712 | your changes against another project to verify that the change makes sense in a |
| 713 | real-world context or that a bug's specific repro case has been fixed. |
| 714 | |
| 715 | If you need to be absolutely sure that your test will exactly emulate the |
| 716 | developer's experience, you can repeatedly build the AndroidX archive and |
| 717 | rebuild your application. In this case, you will need to create a local build of |
| 718 | AndroidX's local Maven repository artifact and install it in your Android SDK |
| 719 | path. |
| 720 | |
| 721 | First, use the `createArchive` Gradle task to generate the local Maven |
| 722 | repository artifact: |
| 723 | |
| 724 | ```shell |
| 725 | # Creates <path-to-checkout>/out/dist/sdk-repo-linux-m2repository-##.zip |
| 726 | ./gradlew createArchive |
| 727 | ``` |
| 728 | |
| 729 | Next, take the ZIP output from this task and extract the contents to the Android |
| 730 | SDK path that you are using for your alternate (non-AndroidX) version of Android |
| 731 | Studio. For example, you may be using `~/Android/SDK/extras` if you are using |
| 732 | the default Android Studio SDK for app development or |
| 733 | `prebuilts/fullsdk-linux/extras` if you are using fullsdk for platform |
| 734 | development. |
| 735 | |
| 736 | ```shell |
| 737 | # Creates or overwrites android/m2repository |
| 738 | cd <path-to-sdk>/extras |
| 739 | unzip <path-to-checkout>/out/dist/top-of-tree-m2repository-##.zip |
| 740 | ``` |
| 741 | |
| 742 | In the project's 'build.gradle' within 'repositories' notify studio of the |
| 743 | location of m2repository: |
| 744 | |
| 745 | ```groovy |
| 746 | allprojects { |
| 747 | repositories { |
| 748 | ... |
| 749 | maven { |
| 750 | url "<path-to-sdk>/extras/m2repository" |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | ``` |
| 755 | |
| 756 | NOTE Gradle resolves dependencies in the order that the repositories are defined |
| 757 | (if 2 repositories can resolve the same dependency, the first listed will do so |
| 758 | and the second will not). Therefore, if the library you are testing has the same |
| 759 | group, artifact, and version as one already published, you will want to list |
| 760 | your custom maven repo first. |
| 761 | |
| 762 | Finally, in the dependencies section of your standalone project's `build.gradle` |
| 763 | file, add or update the `implementation` entries to reflect the AndroidX modules |
| 764 | that you would like to test. Example: |
| 765 | |
| 766 | ``` |
| 767 | dependencies { |
| 768 | ... |
| 769 | implementation "androidx.appcompat:appcompat::1.0.0-alpha02" |
| 770 | } |
| 771 | ``` |
| 772 | |
| 773 | If you are testing your changes in the Android Platform code, you can replace |
| 774 | the module you are testing |
| 775 | `YOUR_ANDROID_PATH/prebuilts/sdk/current/androidx/m2repository` with your own |
| 776 | module. We recommend only replacing the module you are modifying instead of the |
| 777 | full m2repository to avoid version issues of other modules. You can either take |
| 778 | the unzipped directory from |
| 779 | `<path-to-checkout>/out/dist/top-of-tree-m2repository-##.zip`, or from |
| 780 | `<path-to-checkout>/out/androidx/build/support_repo/` after buiding `androidx`. |
| 781 | Here is an example of replacing the RecyclerView module: |
| 782 | |
| 783 | ```shell |
| 784 | $TARGET=YOUR_ANDROID_PATH/prebuilts/sdk/current/androidx/m2repository/androidx/recyclerview/recyclerview/1.1.0-alpha07; |
| 785 | rm -rf $TARGET; |
| 786 | cp -a <path-to-sdk>/extras/m2repository/androidx/recyclerview/recyclerview/1.1.0-alpha07 $TARGET |
| 787 | ``` |
| 788 | |
| 789 | Make sure the library versions are the same before and after replacement. Then |
| 790 | you can build the Android platform code with the new `androidx` code. |