From a51f20fa2141ac991edc999ef48c3de43dceb8b9 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Wed, 17 Jul 2024 17:39:23 +0200 Subject: [PATCH] Add Android-specific variant of the libraries This avoids the need for adding the permission manually. --- build.gradle.kts | 1 + buildlogic/build.gradle.kts | 1 + buildlogic/settings.gradle.kts | 1 + .../KotlinMultiplatformLibraryPlugin.kt | 38 ++++++++++++++----- ...tlinMultiplatformLibraryPluginExtension.kt | 1 + gradle/libs.versions.toml | 3 +- networktime-singletons/build.gradle.kts | 1 + networktime/build.gradle.kts | 7 ++++ .../src/androidMain/AndroidManifest.xml | 5 +++ .../android/build.gradle.kts | 5 +-- 10 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 networktime/src/androidMain/AndroidManifest.xml diff --git a/build.gradle.kts b/build.gradle.kts index 16415733..f9e4f40b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,6 @@ plugins { alias(libs.plugins.android.application) apply false + alias(libs.plugins.android.library) apply false alias(libs.plugins.buildlogic.kotlin.multiplatform.library) apply false alias(libs.plugins.compose) apply false alias(libs.plugins.kotlin.atomicfu) apply false diff --git a/buildlogic/build.gradle.kts b/buildlogic/build.gradle.kts index 7ce02d64..02ffc2fc 100644 --- a/buildlogic/build.gradle.kts +++ b/buildlogic/build.gradle.kts @@ -4,6 +4,7 @@ plugins { dependencies { compileOnly(gradleApi()) + implementation(libs.plugin.android.build) implementation(libs.plugin.kotlin) implementation(libs.plugin.vanniktech.publish) } diff --git a/buildlogic/settings.gradle.kts b/buildlogic/settings.gradle.kts index 5b00e502..4474df4b 100644 --- a/buildlogic/settings.gradle.kts +++ b/buildlogic/settings.gradle.kts @@ -1,5 +1,6 @@ dependencyResolutionManagement { repositories { + google() gradlePluginPortal() mavenCentral() } diff --git a/buildlogic/src/main/kotlin/buildlogic/KotlinMultiplatformLibraryPlugin.kt b/buildlogic/src/main/kotlin/buildlogic/KotlinMultiplatformLibraryPlugin.kt index a4357fca..c289b61b 100644 --- a/buildlogic/src/main/kotlin/buildlogic/KotlinMultiplatformLibraryPlugin.kt +++ b/buildlogic/src/main/kotlin/buildlogic/KotlinMultiplatformLibraryPlugin.kt @@ -1,5 +1,6 @@ package buildlogic +import com.android.build.api.dsl.LibraryExtension import com.vanniktech.maven.publish.JavadocJar import com.vanniktech.maven.publish.KotlinMultiplatform import com.vanniktech.maven.publish.MavenPublishBaseExtension @@ -8,6 +9,7 @@ import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.artifacts.VersionCatalogsExtension import org.gradle.kotlin.dsl.configure +import org.jetbrains.kotlin.gradle.dsl.JvmTarget import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension @@ -18,6 +20,12 @@ internal class KotlinMultiplatformLibraryPlugin : Plugin { KotlinMultiplatformLibraryPluginExtension::class.java, ) val libs = extensions.getByType(VersionCatalogsExtension::class.java).named("libs") + pluginManager.apply( + libs.findPlugin("android-library") + .get() + .get() + .pluginId, + ) pluginManager.apply( libs.findPlugin("kotlin-multiplatform") .get() @@ -31,6 +39,27 @@ internal class KotlinMultiplatformLibraryPlugin : Plugin { .pluginId, ) group = "com.tidal.networktime" + configure { + applyDefaultHierarchyTemplate() + androidTarget { + compilerOptions { + jvmTarget.set(JvmTarget.JVM_1_8) + } + publishLibraryVariants("release") + } + jvm() + sourceSets.androidMain.get().dependsOn(sourceSets.jvmMain.get()) + } + configure { + sourceSets.all { + languageSettings.optIn("kotlin.experimental.ExperimentalObjCName") + } + } + configure { + compileSdk = 34 + defaultConfig.minSdk = 26 + namespace = "com.tidal.networktime${customExtension.androidNamespaceSuffix.getOrElse("")}" + } configure { pom { name.set(project.name) @@ -62,14 +91,5 @@ internal class KotlinMultiplatformLibraryPlugin : Plugin { signAllPublications() publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL, true) } - configure { - applyDefaultHierarchyTemplate() - jvm() - } - configure { - sourceSets.all { - languageSettings.optIn("kotlin.experimental.ExperimentalObjCName") - } - } } } diff --git a/buildlogic/src/main/kotlin/buildlogic/KotlinMultiplatformLibraryPluginExtension.kt b/buildlogic/src/main/kotlin/buildlogic/KotlinMultiplatformLibraryPluginExtension.kt index 760fb4af..7999b734 100644 --- a/buildlogic/src/main/kotlin/buildlogic/KotlinMultiplatformLibraryPluginExtension.kt +++ b/buildlogic/src/main/kotlin/buildlogic/KotlinMultiplatformLibraryPluginExtension.kt @@ -12,6 +12,7 @@ open class KotlinMultiplatformLibraryPluginExtension @Inject constructor( private val project: Project, objectFactory: ObjectFactory, ) { + val androidNamespaceSuffix: Property = objectFactory.property(String::class.java) val pomDescription: Property = objectFactory.property(String::class.java) fun xCFramework(xCFrameworkName: String) = with(project) { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 4b1b733f..d30a8a97 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -20,16 +20,17 @@ kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-c kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx-datetime" } kotlinx-serialization-json-okio = { module = "com.squareup.okio:okio", version.ref = "okio" } okio = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json-okio", version.ref = "kotlinx-serialization-json-okio" } +plugin-android-build = { module = "com.android.tools.build:gradle", version.ref = "android-build" } plugin-kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" } plugin-vanniktech-publish = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "vanniktech-publish" } [plugins] android-application = { id = "com.android.application", version.ref = "android-build" } +android-library = { id = "com.android.library", version.ref = "android-build" } buildlogic-kotlin-multiplatform-library = { id = "buildlogic.kotlin.multiplatform.library", version = "unspecified" } compose = { id = "org.jetbrains.compose", version.ref = "compose" } kotlin-atomicfu = { id = "org.jetbrains.kotlin.plugin.atomicfu", version.ref = "kotlin" } kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } -kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } vanniktech-publish = { id = "com.vanniktech.maven.publish", version.ref = "vanniktech-publish" } diff --git a/networktime-singletons/build.gradle.kts b/networktime-singletons/build.gradle.kts index 2f85f4ec..e7ef4e6b 100644 --- a/networktime-singletons/build.gradle.kts +++ b/networktime-singletons/build.gradle.kts @@ -4,6 +4,7 @@ plugins { } buildlogicKotlinMultiplatformLibrary { + androidNamespaceSuffix = ".singletons" pomDescription = "Singleton API extension." xCFramework("TidalNetworkTimeSingletons") } diff --git a/networktime/build.gradle.kts b/networktime/build.gradle.kts index e74afb13..1cedaeaa 100644 --- a/networktime/build.gradle.kts +++ b/networktime/build.gradle.kts @@ -1,3 +1,4 @@ +import org.jetbrains.kotlin.gradle.dsl.JvmTarget import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget plugins { @@ -11,6 +12,12 @@ buildlogicKotlinMultiplatformLibrary { } kotlin { + androidTarget { + compilerOptions { + jvmTarget.set(JvmTarget.JVM_1_8) + } + publishLibraryVariants("release") + } targets.filterIsInstance() .forEach { it.compilations.configureEach { cinterops.create("NetworkFrameworkWorkaround") } diff --git a/networktime/src/androidMain/AndroidManifest.xml b/networktime/src/androidMain/AndroidManifest.xml new file mode 100644 index 00000000..4a690b1d --- /dev/null +++ b/networktime/src/androidMain/AndroidManifest.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/samples/multiplatform-kotlin/android/build.gradle.kts b/samples/multiplatform-kotlin/android/build.gradle.kts index 3611c724..37100aa8 100644 --- a/samples/multiplatform-kotlin/android/build.gradle.kts +++ b/samples/multiplatform-kotlin/android/build.gradle.kts @@ -26,9 +26,6 @@ kotlin { android { compileSdk = 34 - defaultConfig { - minSdk = 26 - targetSdk = 34 - } + defaultConfig.minSdk = 26 namespace = "com.tidal.networktime.sample.android" }