[Gradle] Migrate new-mpp-android from Groovy to Kotlin Script

This commit is contained in:
Anton Lakotka
2024-01-31 15:49:22 +07:00
committed by Space Team
parent 20bb6b0ea8
commit b13b27a715
7 changed files with 265 additions and 253 deletions
@@ -161,11 +161,10 @@ class KotlinAndroidMppIT : KGPBaseTest() {
groupDir.deleteRecursively()
// Choose a single variant to publish, check that it's there:
subProject("lib").buildGradle.appendText(
//language=Gradle
subProject("lib").buildGradleKts.appendText(
"""
kotlin.android('androidLib').publishLibraryVariants = ['release']
kotlin.androidTarget("androidLib").publishLibraryVariants("release")
""".trimIndent()
)
build("publish") {
@@ -182,11 +181,10 @@ class KotlinAndroidMppIT : KGPBaseTest() {
groupDir.deleteRecursively()
// Enable publishing for all Android variants:
subProject("lib").buildGradle.appendText(
//language=Gradle
subProject("lib").buildGradleKts.appendText(
"""
kotlin.android('androidLib') { publishAllLibraryVariants() }
kotlin.androidTarget("androidLib") { publishAllLibraryVariants() }
""".trimIndent()
)
build("publish") {
@@ -214,11 +212,10 @@ class KotlinAndroidMppIT : KGPBaseTest() {
groupDir.deleteRecursively()
// Then group the variants by flavor and check that only one publication is created:
subProject("lib").buildGradle.appendText(
//language=Gradle
subProject("lib").buildGradleKts.appendText(
"""
kotlin.android('androidLib').publishLibraryVariantsGroupedByFlavor = true
kotlin.androidTarget("androidLib").publishLibraryVariantsGroupedByFlavor = true
""".trimIndent()
)
build("publish") {
@@ -246,11 +243,21 @@ class KotlinAndroidMppIT : KGPBaseTest() {
groupDir.deleteRecursively()
// Add one flavor dimension with two flavors, check that the flavors produce grouped publications:
subProject("lib").buildGradle.appendText(
//language=Gradle
subProject("lib").buildGradleKts.appendText(
"""
android { flavorDimensions('foo'); productFlavors { fooBar { dimension 'foo' }; fooBaz { dimension 'foo' } } }
android {
flavorDimensions("foo")
productFlavors {
create("fooBar") {
dimension = "foo"
}
create("fooBaz") {
dimension = "foo"
}
}
}
""".trimIndent()
)
build("publish") {
@@ -291,11 +298,10 @@ class KotlinAndroidMppIT : KGPBaseTest() {
groupDir.deleteRecursively()
// Disable the grouping and check that all the variants are published under separate artifactIds:
subProject("lib").buildGradle.appendText(
//language=Gradle
subProject("lib").buildGradleKts.appendText(
"""
kotlin.android('androidLib') { publishLibraryVariantsGroupedByFlavor = false }
kotlin.androidTarget("androidLib") { publishLibraryVariantsGroupedByFlavor = false }
""".trimIndent()
)
build("publish") {
@@ -352,13 +358,12 @@ class KotlinAndroidMppIT : KGPBaseTest() {
buildOptions = defaultBuildOptions.copy(androidVersion = agpVersion),
buildJdk = jdkVersion.location
) {
subProject("lib").buildGradle.appendText(
//language=Gradle
subProject("lib").buildGradleKts.appendText(
"""
kotlin.android('androidLib') {
withSourcesJar(false)
publishLibraryVariants = ['release']
kotlin.androidTarget("androidLib") {
withSourcesJar(publish = false)
publishLibraryVariants("release")
}
""".trimIndent()
)
@@ -393,26 +398,55 @@ class KotlinAndroidMppIT : KGPBaseTest() {
// check that the dependencies in the POMs are correctly rewritten:
val appGroupDir = subProject("app").projectPath.resolve("build/repo/com/example")
subProject("lib").buildGradle.appendText(
//language=Gradle
subProject("lib").buildGradleKts.appendText(
"""
android { flavorDimensions('foo'); productFlavors { fooBar { dimension 'foo' }; fooBaz { dimension 'foo' } } }
android {
flavorDimensions("foo")
productFlavors {
create("fooBar") {
dimension = "foo"
}
create("fooBaz") {
dimension = "foo"
}
}
}
""".trimIndent()
)
subProject("app").buildGradle.modify {
subProject("app").buildGradleKts.modify {
it.replace("com.android.application", "com.android.library")
.replace("applicationId", "//") +
//language=Gradle
.replace("applicationId", "//")
.replace("versionCode", "//")
.replace("versionName", "//")
.replace("plugins {\n", "plugins {\n `maven-publish`\n") +
"""
apply plugin: 'maven-publish'
publishing { repositories { maven { url = uri("${'$'}buildDir/repo") } } }
kotlin.android('androidApp') { publishAllLibraryVariants() }
android { flavorDimensions('foo'); productFlavors { fooBar { dimension 'foo' }; fooBaz { dimension 'foo' } } }
publishing {
repositories {
maven {
url = uri("${'$'}buildDir/repo")
}
}
}
kotlin.androidTarget("androidApp") { publishAllLibraryVariants() }
android {
flavorDimensions("foo")
productFlavors {
create("fooBar") {
dimension = "foo"
}
create("fooBaz") {
dimension = "foo"
}
}
}
""".trimIndent()
}
makeSnapshotTo("/tmp/111")
build("publish") {
listOf("foobar", "foobaz").forEach { flavor ->
listOf("-debug", "").forEach { buildType ->
@@ -431,13 +465,12 @@ class KotlinAndroidMppIT : KGPBaseTest() {
appGroupDir.deleteRecursively()
// Also check that api and runtimeOnly MPP dependencies get correctly published with the appropriate scope, KT-29476:
subProject("app").buildGradle.modify {
it.replace("implementation project(':lib')", "api project(':lib')") +
//language=Gradle
subProject("app").buildGradleKts.modify {
it.replace("implementation(project(\":lib\")", "api(project(\":lib\")") +
"""
kotlin.sourceSets.commonMain.dependencies {
runtimeOnly(kotlin('reflect'))
kotlin.sourceSets.getByName("commonMain").dependencies {
runtimeOnly(kotlin("reflect"))
}
""".trimIndent()
}
@@ -521,24 +554,23 @@ class KotlinAndroidMppIT : KGPBaseTest() {
buildJdk = jdkVersion.location
) {
// Test the fix for KT-29343
subProject("lib").buildGradle.appendText(
//language=Gradle
subProject("lib").buildGradleKts.appendText(
"""
kotlin.sourceSets {
commonMain {
dependencies {
implementation kotlin("stdlib-common")
implementation(kotlin("stdlib-common"))
}
}
androidLibDebug {
val androidLibDebug by creating {
dependencies {
implementation kotlin("reflect")
implementation(kotlin("reflect"))
}
}
androidLibRelease {
val androidLibRelease by creating {
dependencies {
implementation kotlin("test-junit")
implementation(kotlin("test-junit"))
}
}
}
@@ -577,15 +609,14 @@ class KotlinAndroidMppIT : KGPBaseTest() {
buildOptions = defaultBuildOptions.copy(androidVersion = agpVersion),
buildJdk = jdkVersion.location
) {
val libBuildScript = subProject("lib").buildGradle
val appBuildScript = subProject("app").buildGradle
val libBuildScript = subProject("lib").buildGradleKts
val appBuildScript = subProject("app").buildGradleKts
// Enable publishing for all Android variants:
libBuildScript.appendText(
//language=Gradle
"""
kotlin.android('androidLib') { publishAllLibraryVariants() }
kotlin.androidTarget("androidLib") { publishAllLibraryVariants() }
""".trimIndent()
)
@@ -608,25 +639,24 @@ class KotlinAndroidMppIT : KGPBaseTest() {
// Check that the consumer side uses custom attributes specified in the target and compilations:
val appBuildScriptBackup = appBuildScript.readText()
val libBuildScriptBackup = libBuildScript.readText()
libBuildScript.appendText(
//language=Gradle
"""
kotlin.targets.all {
attributes.attribute(
Attribute.of("com.example.target", String),
Attribute.of("com.example.target", String::class.java),
targetName
)
}
""".trimIndent()
)
appBuildScript.appendText(
//language=Gradle
"""
kotlin.targets.androidApp.attributes.attribute(
Attribute.of("com.example.target", String),
kotlin.targets.getByName("androidApp").attributes.attribute(
Attribute.of("com.example.target", String::class.java),
"notAndroidLib"
)
""".trimIndent()
@@ -641,18 +671,14 @@ class KotlinAndroidMppIT : KGPBaseTest() {
}
libBuildScript.writeText(
appBuildScriptBackup +
//language=Gradle
libBuildScriptBackup +
"""
android {
namespace 'app.example.com.lib'
}
kotlin.targets.all {
compilations.all {
attributes.attribute(
Attribute.of("com.example.compilation", String),
targetName + compilationName.capitalize()
Attribute.of("com.example.compilation", String::class.java),
target.name + compilationName.capitalize()
)
}
}
@@ -660,15 +686,11 @@ class KotlinAndroidMppIT : KGPBaseTest() {
)
appBuildScript.writeText(
appBuildScriptBackup +
//language=Gradle
"""
android {
namespace 'app.example.com.app_sample'
}
kotlin.targets.androidApp.compilations.all {
kotlin.targets.getByName("androidApp").compilations.all {
attributes.attribute(
Attribute.of("com.example.compilation", String),
Attribute.of("com.example.compilation", String::class.java),
"notDebug"
)
}
@@ -938,4 +960,5 @@ class KotlinAndroidMppIT : KGPBaseTest() {
build("assemble")
}
}
}
@@ -142,9 +142,9 @@ abstract class AndroidAndJavaConsumeMppLibIT : BaseGradleIT() {
if (useFlavors) {
it + "\n" + """
android {
flavorDimensions "myFlavor"
flavorDimensions("myFlavor")
productFlavors {
flavor1 { dimension "myFlavor" }
create("flavor1") { dimension = "myFlavor" }
}
}
""".trimIndent()
@@ -153,7 +153,7 @@ abstract class AndroidAndJavaConsumeMppLibIT : BaseGradleIT() {
// Simulate the behavior with user-defined consumable configuration added with no proper attributes:
it + "\n" + """
configurations.create("legacyConfiguration") {
def bundlingAttribute = Attribute.of("org.gradle.dependency.bundling", String)
val bundlingAttribute = Attribute.of("org.gradle.dependency.bundling", String::class.java)
attributes.attribute(bundlingAttribute, "external")
}
""".trimIndent()
@@ -33,7 +33,7 @@ fun BuildResult.assertOutputContainsAny(
) {
assert(expectedSubStrings.any { output.contains(it) }) {
printBuildOutput()
"Build output does not contain any of \"$expectedSubStrings\""
"Build output does not contain any of \"${expectedSubStrings.toList()}\""
}
}
@@ -1,82 +0,0 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-multiplatform'
group 'com.example'
version '1.0'
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(8))
}
android {
compileSdkVersion 33
namespace 'app.example.com.app_sample'
defaultConfig {
applicationId "app.example.com.app_sample"
minSdkVersion 26
targetSdkVersion 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'org.jetbrains.kotlin:kotlin-stdlib'
}
kotlin {
sourceSets {
commonMain {
dependencies {
implementation project(':lib')
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
}
}
jvmAppMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib'
}
}
jsAppMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-js'
}
}
}
targets {
fromPreset(presets.android, 'androidApp')
fromPreset(presets.jvm, 'jvmApp')
fromPreset(presets.js, 'jsApp')
}
}
// test diagnostic task, not needed by the build
task printCompilerPluginOptions {
doFirst {
kotlin.sourceSets.each { sourceSet ->
def args = sourceSet.languageSettings.compilerPluginArguments
def cp = sourceSet.languageSettings.compilerPluginClasspath.files
println sourceSet.name + '=args=>' + args
println sourceSet.name + '=cp=>' + cp
}
}
}
@@ -0,0 +1,86 @@
import org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile
import org.jetbrains.kotlin.gradle.tasks.CompilerPluginOptions
plugins {
id("com.android.application")
kotlin("multiplatform")
}
group = "com.example"
version = "1.0"
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(8))
}
kotlin {
androidTarget("androidApp")
jvm("jvmApp")
js("jsApp")
sourceSets {
commonMain {
dependencies {
implementation(project(":lib"))
}
}
}
}
android {
compileSdk = 33
namespace = "app.example.com.app_sample"
defaultConfig {
applicationId = "app.example.com.app_sample"
minSdk = 26
targetSdk = 33
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation("com.android.support:appcompat-v7:27.1.1")
implementation("com.android.support.constraint:constraint-layout:1.1.2")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("com.android.support.test:runner:1.0.2")
androidTestImplementation("com.android.support.test.espresso:espresso-core:3.0.2")
}
// test diagnostic task, not needed by the build
tasks.register("printCompilerPluginOptions") {
doFirst {
kotlin.targets.flatMap { it.compilations }.forEach { compilation ->
val sourceSetName = compilation.defaultSourceSet.name
val compileTask = compilation.compileTaskProvider.get()
val args: List<String>
val cp: Set<File>
when (compileTask) {
is AbstractKotlinCompile<*> -> {
args = compileTask
.pluginOptions
.get()
.fold(CompilerPluginOptions()) { options, option -> options.plus(option) }
.arguments
cp = compileTask.pluginClasspath.files
}
else -> return@forEach
}
println(sourceSetName + "=args=>" + args)
println(sourceSetName + "=cp=>" + cp)
}
}
}
@@ -1,100 +0,0 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-multiplatform'
apply plugin: 'maven-publish'
group 'com.example'
version '1.0'
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(8))
}
android {
compileSdkVersion 31
namespace 'app.example.com.lib'
defaultConfig {
minSdkVersion 26
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
kotlin.targets.all {
compilations.all {
// KT-29964: check that Android compilations can be configured with an `all { ... }` handler:
kotlinOptions {
verbose = true
}
compileKotlinTask.doFirst {
if (!compileKotlinTask.kotlinOptions.verbose) {
throw new AssertionError("kotlinOptions were not configured properly")
}
println "KT-29964 OK"
}
}
}
kotlin {
sourceSets {
jvmLibMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib'
}
}
jsLibMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-js'
}
}
}
targets {
fromPreset(presets.android, 'androidLib') {
attributes {
attribute(Attribute.of("com.example.target", String), "androidLib")
}
compilations.all {
attributes {
attribute(Attribute.of("com.example.compilation", String), compilationName)
}
}
publishAllLibraryVariants()
}
fromPreset(presets.jvm, 'jvmLib')
fromPreset(presets.js, 'jsLib')
}
}
publishing {
repositories {
maven {
url = uri("$buildDir/repo")
}
}
}
@@ -0,0 +1,85 @@
plugins {
id("com.android.library")
kotlin("multiplatform")
`maven-publish`
}
group = "com.example"
version = "1.0"
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(8))
}
android {
compileSdk = 31
namespace = "app.example.com.lib"
defaultConfig {
minSdk = 26
targetSdk = 31
testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation("com.android.support:appcompat-v7:27.1.1")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("com.android.support.test:runner:1.0.2")
androidTestImplementation("com.android.support.test.espresso:espresso-core:3.0.2")
}
kotlin.targets.all {
compilations.all {
// KT-29964: check that Android compilations can be configured with an `all { ... }` handler:
kotlinOptions {
verbose = true
}
compileKotlinTask.doFirst {
if (!compileKotlinTask.kotlinOptions.verbose) {
throw AssertionError("kotlinOptions were not configured properly")
}
println("KT-29964 OK")
}
}
}
kotlin {
androidTarget("androidLib") {
attributes {
attribute(Attribute.of("com.example.target", String::class.java), "androidLib")
}
compilations.all {
attributes {
attribute(Attribute.of("com.example.compilation", String::class.java), compilationName)
}
}
publishAllLibraryVariants()
}
jvm("jvmLib")
js("jsLib")
}
publishing {
repositories {
maven("$buildDir/repo")
}
}