Android 源代码集布局

The new Android source set layout was introduced in Kotlin 1.8.0 and became the default in 1.9.0. Follow this guide to understand the key differences between the deprecated and the new layouts, as well as how to migrate your projects.

You don’t need to implement all the suggestions, only those that are applicable to your particular projects.

Android 源代码集布局 - 图1

Check the compatibility

The new layout requires Android Gradle plugin 7.0 or later and is supported in Android Studio 2022.3 and later. Check your version of the Android Gradle plugin and upgrade if necessary.

Rename Kotlin source sets

If applicable, rename the source sets in your project, following this pattern:

Previous source set layoutNew source set layout
targetName + AndroidSourceSet.nametargetName + AndroidVariantType

{AndroidSourceSet.name} maps to {KotlinSourceSet.name} as follows:

Previous source set layoutNew source set layout
mainandroidMainandroidMain
testandroidTestandroidUnitTest
androidTestandroidAndroidTestandroidInstrumentedTest

Move source files

If applicable, move your source files to the new directories, following this pattern:

Previous source set layoutNew source set layout
The layout had additional /kotlin SourceDirectoriessrc/{KotlinSourceSet.name}/kotlin

{AndroidSourceSet.name} maps to {SourceDirectories included} as follows:

Previous source set layoutNew source set layout
mainsrc/androidMain/kotlin
src/main/kotlin
src/main/java
src/androidMain/kotlin
src/main/kotlin
src/main/java
testsrc/androidTest/kotlin
src/test/kotlin
src/test/java
src/androidUnitTest/kotlin
src/test/kotlin
src/test/java
androidTestsrc/androidAndroidTest/kotlin
src/androidTest/java
src/androidInstrumentedTest/kotlin
src/androidTest/java, src/androidTest/kotlin

Move the AndroidManifest.xml file

If you have the AndroidManifest.xml file in your project, move it to the new directory, following this pattern:

Previous source set layoutNew source set layout
src/{AndroidSourceSet.name}/AndroidManifest.xmlsrc/{KotlinSourceSet.name}/AndroidManifest.xml

{AndroidSourceSet.name} maps to {AndroidManifest.xml location} as follows:

Previous source set layoutNew source set layout
mainsrc/main/AndroidManifest.xmlsrc/androidMain/AndroidManifest.xml
debugsrc/debug/AndroidManifest.xmlsrc/androidDebug/AndroidManifest.xml

Check the relationship between Android and common tests

The new Android source set layout changes the relationship between Android-instrumented tests (renamed to androidInstrumentedTest in the new layout) and common tests.

Previously, the dependsOn relationship between androidAndroidTest and commonTest was the default. It meant the following:

  • The code in commonTest was available in androidAndroidTest.
  • expect declarations in commonTest had to have corresponding actual implementations in androidAndroidTest.
  • Tests declared in commonTest were also running as Android instrumented tests.

In the new Android source set layout, the dependsOn relationship is not added by default. If you prefer the previous behavior, manually declare the following relationship in your build.gradle.kts file:

  1. kotlin {
  2. // ...
  3. sourceSets {
  4. val commonTest by getting
  5. val androidInstrumentedTest by getting {
  6. dependsOn(commonTest)
  7. }
  8. }
  9. }

Adjust the implementation of Android flavors

Previously, the Kotlin Gradle plugin eagerly created source sets that corresponded to Android source sets containing debug and release build types or custom flavors like demo and full. It made the source sets accessible by using expressions like val androidDebug by getting { ... }.

The new Android source set layout makes use of Android’s onVariants) to create source sets. It makes such expressions invalid, leading to errors like org.gradle.api.UnknownDomainObjectException: KotlinSourceSet with name 'androidDebug' not found.

To work around that, use the new invokeWhenCreated() API in your build.gradle.kts file:

  1. kotlin {
  2. // ...
  3. @OptIn(ExperimentalKotlinGradlePluginApi::class)
  4. sourceSets.invokeWhenCreated("androidFreeDebug") {
  5. // ...
  6. }
  7. }