Maven-publish logic inside Gradle Plugin - android

I've got a few libraries that I want to publish to a local Maven. I want to use a Plugin for this to maximum reuse.
The basic code setup is this:
apply plugin: 'com.android.library'
apply plugin: 'LibraryPlugin'
android {
defaultConfig {
versionCode 1
versionName "1.0q"
}
}
def groupId = "x.y.z"
def artifactId = "LibZ"
project.afterEvaluate {
publishing {
publications {
parseDebugLibraryResources(MavenPublication) {
setGroupId groupId
setArtifactId artifactId
version = android.defaultConfig.versionName
}
}
}
}
After a ./gradlew publishToMavenLocal it is correctly placed there.
I want to move that project.afterEvaluate > publishing > publications stuff into the Plugin so I can reuse it in multiple projects.
Here's the Plugin code:
class LibraryPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.plugins.apply("kotlin-android")
project.plugins.apply("maven-publish")
project.dependencies.add("implementation", BuildPlugins.kotlinStandardLibrary)
val extension = project.extensions.getByType<LibraryExtension>()
extension.configureLibrary()
project.afterEvaluate {
// publishing {
// }
}
}
private fun LibraryExtension.configureLibrary() {
setCompileSdkVersion(AndroidSdk.compile)
defaultConfig.apply {
setMinSdkVersion(AndroidSdk.min)
setTargetSdkVersion(AndroidSdk.target)
testInstrumentationRunner = TestLibraries.UI.instrumentationRunner
}
}
}
Now it doesn't know the "publishing" node in Project.afterEvaluate. How can I move that code in to the Plugin?

I needed to get the publishing node like this:
val publishing = project.properties["publishing"] as PublishingExtension
Full example:
class LibraryPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.plugins.apply("kotlin-android")
project.plugins.apply("maven-publish")
project.dependencies.add("implementation", BuildPlugins.kotlinStandardLibrary)
val extension = project.extensions.getByType<LibraryExtension>()
extension.configureLibrary()
project.afterEvaluate {
val publishing = project.properties["publishing"] as PublishingExtension
publishing {
}
}
}
private fun LibraryExtension.configureLibrary() {
setCompileSdkVersion(AndroidSdk.compile)
defaultConfig.apply {
setMinSdkVersion(AndroidSdk.min)
setTargetSdkVersion(AndroidSdk.target)
testInstrumentationRunner = TestLibraries.UI.instrumentationRunner
}
}
}

Related

iOS cocoaods framework not generating in Kotlin Multiplatform project

I am creating KMM library. I want to create a cocoapods framework, but when I tried to build my project, the framework is not generating. I tried clean project and build again. My cocoapods is not generating the at all in build folder. I didn't get why this is not working. I am adding my build file.
build.gradle.kts
plugins {
kotlin("multiplatform") version "1.6.21"
kotlin("native.cocoapods") version "1.6.21"
id("com.android.library")
id("org.jetbrains.dokka") version "1.6.10"
id("org.jetbrains.kotlin.plugin.serialization") version "1.6.21"
id("maven-publish")
}
val libraryVersion = "0.0.1"
var libraryGroup = "com.vivek"
var libraryArtifactId = "kmm-module"
var libraryUri = uri("https://maven.pkg.github.com/vivek/KotlinMultiplatformMobile")
repositories {
google()
mavenCentral()
}
kotlin {
android {
publishLibraryVariants("release", "debug")
}
cocoapods {
version = libraryVersion
summary = "Some description for a Kotlin/Native module"
homepage = ""
ios.deploymentTarget = "13.0"
name = "VivekKmmPod"
framework {
baseName = "VivekFramework"
isStatic = false
}
}
iosX64()
iosArm64()
iosSimulatorArm64()
sourceSets {
val ktorVersion = "2.0.0"
val commonMain by getting {
dependencies {
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-logging:$ktorVersion")
implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
implementation("io.ktor:ktor-client-auth:$ktorVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.3.2")
implementation("io.insert-koin:koin-core:3.2.0")
}
}
val androidMain by getting {
dependencies {
implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
implementation("io.ktor:ktor-client-logging-jvm:$ktorVersion")
}
}
val iosX64Main by getting
val iosArm64Main by getting
val iosSimulatorArm64Main by getting
val iosMain by creating {
dependsOn(commonMain)
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
iosSimulatorArm64Main.dependsOn(this)
dependencies {
implementation("io.ktor:ktor-client-darwin:$ktorVersion")
}
}
}
}
android {
compileSdk = 21
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdk = 21
targetSdk = 31
}
}
tasks {
create<Jar>("javadocJar") {
val dokkaHtml by getting(org.jetbrains.dokka.gradle.DokkaTask::class)
dependsOn(dokkaHtml)
archiveClassifier.set("javadoc")
from(dokkaHtml.outputDirectory)
}
}
publishing {
publications {
group = libraryGroup
publications.withType<MavenPublication> {
artifactId.toLowerCase()
groupId = libraryGroup
artifactId = libraryArtifactId
version = libraryVersion
artifact(tasks["javadocJar"])
}
repositories {
maven {
url = libraryUri
credentials {
username = (System.getenv("GITHUB_USER") ?: project.properties["GITHUB_USER"]).toString()
password = (System.getenv("GITHUB_PERSONAL_ACCESS_TOKEN") ?: project.properties["GITHUB_PERSONAL_ACCESS_TOKEN"]).toString()
}
}
}
}
}
As you can see no framework generated in build folder.
just sync the project ... the framework file will be created .. or try ./gradlew build

Can only add Kotlin Multiplatform Mobile library from maven by using releaseImplementation and debugImplementation

Every time I publish a Kotlin Multiplatform Mobile library to maven central the only I can seem to add/use the Android dependency in an Android app is by adding both the releaseImplementation and debugImplementation
Example
releaseImplementation 'io.github.tyczj.lumberjack:Lumberjack-android:1.0.0#aar'
debugImplementation 'io.github.tyczj.lumberjack:Lumberjack-android-debug:1.0.0#aar'
Instead of the "normal" way where you just have a single implementation
implementation 'io.github.tyczj.lumberjack:Lumberjack-android:1.0.0'
Here is my build.gradle file
plugins {
kotlin("multiplatform") version "1.4.32"
id("com.android.library")
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
id("maven-publish")
id("signing")
}
group = "io.github.tyczj.lumberjack"
version = "1.0.2"
ext["signing.keyId"] = ""
ext["signing.password"] = ""
ext["signing.secretKeyRingFile"] = ""
repositories {
google()
mavenCentral()
maven {
setUrl("https://plugins.gradle.org/m2/")
}
}
val javadocJar by tasks.registering(Jar::class) {
archiveClassifier.set("javadoc")
}
val emptyJar by tasks.registering(Jar::class) {
archiveAppendix.set("empty")
}
kotlin {
android{
publishLibraryVariants("release", "debug")
}
iosX64("ios") {
binaries {
framework {
baseName = "lumberjack"
}
}
}
sourceSets {
val commonMain by getting
val commonTest by getting
val androidMain by getting
val androidTest by getting
val iosMain by getting
val iosTest by getting
}
}
android {
compileSdkVersion(29)
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(24)
targetSdkVersion(29)
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
}
}
afterEvaluate {
publishing {
repositories {
maven {
name = "sonatype"
url = uri("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/")
credentials {
username = rootProject.ext["ossrhUsername"]?.toString()
password = rootProject.ext["ossrhPassword"]?.toString()
}
}
}
publications.withType<MavenPublication> {
artifact(javadocJar.get())
pom{
name.set("Lumberjack")
description.set("Logging library for Kotlin Multiplatform Mobile applications")
url.set("https://github.com/tyczj/Lumberjack")
licenses {
license {
name.set("MIT")
url.set("https://opensource.org/licenses/MIT")
}
}
developers {
developer {
id.set("tyczj")
name.set("Jeff Tycz")
email.set("tyczj359#gmail.com")
}
}
scm {
url.set("https://github.com/tyczj/Lumberjack")
}
}
}
}
}
ext["signing.keyId"] = rootProject.ext["signing.keyId"]?.toString()
ext["signing.password"] = rootProject.ext["signing.password"]?.toString()
ext["signing.secretKeyRingFile"] = rootProject.ext["signing.secretKeyRingFile"]?.toString()
signing {
sign(publishing.publications)
}
apply(from = "${rootDir}/scripts/publish-root.gradle")
The full source for this library can be found here
What is wrong with how I am building/publishing KMM libraries where I have to specify the release and debug implementations?
You should not specify -android postfix, just use implementation("io.github.tyczj.lumberjack:Lumberjack:1.0.0").
This works because dependency variant resolution is based on Gradle Module Metadata. This metadata is just another file published with your library (it has .module extension) and it contains description of all variants. As you are publishing the library as a whole, the top-level artifact io.github.tyczj.lumberjack:Lumberjack contains the metadata for the whole library, allowing gradle to choose the right variant.
Another option would be to make sure your -android artifact contains proper module metadata with both release and debug variants. I believe publishLibraryVariantsGroupedByFlavor is the way to tell the publisher plugin to make it this way, but I have not tried it.

Maven publication with documentation

I am trying to include the documentation into maven publication. The publication itself works fine. I can also publish with the setup bellow sources and javadoc to gitlab packages. I can download and open the javadoc.jar and sources.jar which contains those comments/documentation of classes. However when I include that library as a dependency to my android app as a gradle dependency, I can not see the comments on my interfaces/classes with F1 or when I open that class.
Any help would be appreciated.
Using kotlin DSL
id("maven-publish")
id("org.jetbrains.dokka")
tasks {
dokka {
outputFormat = "javadoc"
outputDirectory = "$buildDir/javadoc"
moduleName = rootProject.name
}
}
val dokkaJar by tasks.creating(Jar::class) {
group = JavaBasePlugin.DOCUMENTATION_GROUP
description = "Assembles Kotlin docs with Dokka"
archiveClassifier.set("javadoc")
from(tasks.dokka)
dependsOn(tasks.dokka)
}
val sourcesJar by tasks.registering(Jar::class) {
archiveClassifier.set("sources")
from(android.sourceSets.getByName("main").java.srcDirs)
}
artifacts {
archives(sourcesJar)
archives(dokkaJar)
}
afterEvaluate {
publishing {
publications {
create<MavenPublication>("snapshot_aar") {
groupId = libGroupId
artifactId = libArticactId
version = getVersionNameForSnapshot()
artifact(tasks.getByName("bundleDebugAar"))
artifact(dokkaJar)
artifact(sourcesJar)
pom.withXml {
fun groovy.util.Node.addDependency(dependency: Dependency) {
appendNode("dependency").apply {
appendNode("groupId", dependency.group)
appendNode("artifactId", dependency.name)
appendNode("version", dependency.version)
}
}
asNode().appendNode("dependencies").let { dependencies ->
configurations.api.get().allDependencies.forEach {
dependencies.addDependency(it)
}
configurations.implementation.get().allDependencies.forEach {
dependencies.addDependency(it)
}
}
}
}
...

Kotlin Multiplatform build failure due to android target

I am creating a Kotlin Multiplatform library with Android support.
When I try to build with gradle I get an error (./gradlew build --warning-mode all).
Either this:
Could not determine the dependencies of task ':lib:compileDebugAidl'.
> Failed to find target with hash string '29' in: /home/user/Android/Sdk
Or that:
Could not determine the dependencies of task ':lib:verifyReleaseResources'.
> Failed to find target with hash string '29' in: /home/user/Android/Sdk
The first right after indexing and the second when I do ./gradlew clean
However, there is this dependency in any case since I have already created Android apps based on that.
I have also tried Target 28 and an RC version of 30 (since 30 has not yet been officially released, but I have still created apps)
My structure of the gradle files is like this:
root
|-build.gradle.kts
|-settings.gradle.kts
|-gradlew
|-gradlew.bat
|-gradle
|-wrapper
|-gradle-wrapper.jar
|-gradle-wrapper.properties
|-lib
|-build.gradle.kts
settings.gradle.kts:
rootProject.name = "Library"
include(":lib")
root build.gradle.kts:
buildscript {
repositories {
addRepos()
maven(uri("https://dl.bintray.com/kotlin/kotlin-eap")) {
metadataSources {
gradleMetadata()
mavenPom()
}
}
}
dependencies {
classpath("com.android.tools.build:gradle:4.1.0-beta04")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72")
}
}
allprojects {
repositories {
addRepos()
maven(uri("https://dl.bintray.com/kotlin/kotlin-eap")) {
metadataSources {
gradleMetadata()
mavenPom()
}
}
}
}
lib build.gradle.kts:
plugins {
id("com.android.library")
kotlin("multiplatform")
id("maven-publish")
}
group = "de.datlag"
version = "1.0"
android {
compileSdkVersion = 29.toString()
buildToolsVersion = "29.0.3"
defaultConfig {
targetSdkVersion(29)
versionCode = 1
versionName = "1.0"
}
buildTypes {
val debug by getting {
isMinifyEnabled = false
isDebuggable = true
isShrinkResources = false
}
val release by getting {
isMinifyEnabled = false
isDebuggable = false
isShrinkResources = false
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
repositories {
addRepos()
}
kotlin {
jvm {
compilations.all {
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}
js {
browser()
nodejs()
}
android {
publishAllLibraryVariants()
}
val hostOs = System.getProperty("os.name")
val isMingwX64 = hostOs.startsWith("Windows")
val nativeTarget = when {
hostOs == "Mac OS X" -> macosX64("native")
hostOs == "Linux" -> linuxX64("native")
isMingwX64 -> mingwX64("native")
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
sourceSets {
val commonMain by getting {
dependencies {
implementation(kotlin("stdlib-common"))
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val jvmMain by getting {
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
}
val jvmTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-junit"))
}
}
val androidMain by getting {
dependsOn(jvmMain)
}
val androidTest by getting {
dependsOn(jvmTest)
}
val jsMain by getting {
dependencies {
implementation(kotlin("stdlib-js"))
}
}
val jsTest by getting {
dependencies {
implementation(kotlin("test-js"))
}
}
val nativeMain by getting { }
val nativeTest by getting { }
}
}
addRepos method:
fun RepositoryHandler.addRepos() {
mavenLocal {
metadataSources {
gradleMetadata()
mavenPom()
}
}
mavenCentral {
metadataSources {
gradleMetadata()
mavenPom()
}
}
jcenter {
metadataSources {
gradleMetadata()
mavenPom()
}
}
google {
metadataSources {
gradleMetadata()
mavenPom()
}
}
gradlePluginPortal()
}
In lib.build.gradle.kts you should use the compileSdkVersion(29) syntax rather than setting a string. You can see in the source for these extensions that the behavior is different if you call the string version.
/** #see #getCompileSdkVersion() */
public void compileSdkVersion(String version) {
checkWritability();
this.target = version;
}
/** #see #getCompileSdkVersion() */
public void compileSdkVersion(int apiLevel) {
compileSdkVersion("android-" + apiLevel);
}
The when passing an int it will append "android-" for you. So you could do compileSdkVersion = "android-29" if you wanted, but just passing the int is easier

Kotlin Multiplatform Android Imports won't resolve

I have created a very simple KMP project, with the following structure:
-Root
--app
--gradle
--SharedCode
--src\commonMain\kotlin\actual.kt
--src\iosMain\kotlin\actual.kt
--scr\androidMain\kotlin\actual.kt
--build.gradle.kts
--native
--KotlinIOS
--iOS project (xcodeproj, etc)
Everything works, and the basic project work on both Android and iOS platforms.
But when I try to use an android-specific import statement in my androidMain directory, the import statement won't resolve:
import android.os.build // Android Studio can't find this
actual fun platformName(): String {
return "Android"
}
It is weird, since the iOS package is using iOS-specific imports successfully:
import platform.UIKit.UIDevice // This import works
actual fun platformName(): String {
return UIDevice.currentDevice.systemName() +
" " + UIDevice.currentDevice.systemVersion
}
Any suggestions about what I may need to configure to get my Android import to work?
This is my build.gradle.kts file for completeness:
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
}
kotlin {
//select iOS target platform depending on the Xcode environment variables
val iOSTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
::iosArm64
else
::iosX64
iOSTarget("ios") {
binaries {
framework {
baseName = "SharedCode"
}
}
}
jvm("android")
sourceSets["commonMain"].dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
}
sourceSets["androidMain"].dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib")
}
}
val packForXcode by tasks.creating(Sync::class) {
val targetDir = File(buildDir, "xcode-frameworks")
/// selecting the right configuration for the iOS
/// framework depending on the environment
/// variables set by Xcode build
val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
val framework = kotlin.targets
.getByName<KotlinNativeTarget>("ios")
.binaries.getFramework(mode)
inputs.property("mode", mode)
dependsOn(framework.linkTask)
from({ framework.outputDirectory })
into(targetDir)
/// generate a helpful ./gradlew wrapper with embedded Java path
doLast {
val gradlew = File(targetDir, "gradlew")
gradlew.writeText("#!/bin/bash\n"
+ "export 'JAVA_HOME=${System.getProperty("java.home")}'\n"
+ "cd '${rootProject.rootDir}'\n"
+ "./gradlew \$#\n")
gradlew.setExecutable(true)
}
}
tasks.getByName("build").dependsOn(packForXcode)
You do not have the android required definitions in your gradle build script. Here's a basic gradle script that works for me:
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.tasks.FatFrameworkTask
buildscript {
repositories {
mavenLocal()
maven("https://kotlin.bintray.com/kotlinx")
maven("https://dl.bintray.com/jetbrains/kotlin-native-dependencies")
maven("https://dl.bintray.com/kotlin/kotlin-eap")
maven("https://plugins.gradle.org/m2/")
google()
jcenter()
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50")
classpath("com.android.tools.build:gradle:3.5.0")
classpath("co.touchlab:kotlinxcodesync:0.1.5")
}
}
plugins {
id ("com.android.library") version "1.3.50"
id ("org.jetbrains.kotlin.multiplatform") version "1.3.50"
}
allprojects {
repositories {
mavenLocal()
google()
jcenter()
mavenCentral()
maven("https://dl.bintray.com/kotlin/kotlin-eap")
maven("https://kotlin.bintray.com/ktor")
maven("https://kotlin.bintray.com/kotlinx")
}
}
group = "com.example.mykmp"
version = "0.0.1"
apply(plugin = "maven-publish")
apply(plugin = "com.android.library")
apply(plugin = "kotlin-android-extensions")
android {
compileSdkVersion(29)
defaultConfig {
minSdkVersion(21)
targetSdkVersion(29)
}
}
kotlin {
android()
val frameworkName = "mykmp"
val ios = iosX64("ios") {
binaries.framework {
baseName = frameworkName
}
}
sourceSets {
commonMain {
dependencies {
implementation(kotlin("stdlib-common"))
}
}
commonTest {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting {
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-junit"))
}
}
val iosMain by getting {
dependencies {
implementation(kotlin("stdlib"))
}
}
val iosTest by getting {
dependencies {
implementation(kotlin("stdlib"))
}
}
}
tasks.create("framework", FatFrameworkTask::class) {
baseName = frameworkName
from(
ios.binaries.getFramework("DEBUG")
)
destinationDir = buildDir.resolve("xcode-frameworks")
group = "iOS frameworks"
description = "Builds a simulator only framework to build from xcode directly"
doLast {
val file = File(destinationDir, "gradlew")
file.writeText(text = "#!/bin/bash\nexport JAVA_HOME=${System.getProperty("java.home")}\ncd ${rootProject.rootDir}\n./gradlew \$#\n")
file.setExecutable(true)
}
}
}
tasks.getByName("build").dependsOn(packForXcode)
Also, in the settings.gradle.kts, I have this:
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.id == "kotlin-multiplatform") {
useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}")
}
if (requested.id.id == "com.android.library") {
useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}")
}
if (requested.id.id == "kotlinx-serialization") {
useModule("org.jetbrains.kotlin:kotlin-serialization:${requested.version}")
}
}
}
}
Otherwise, it might complain that com.android.library could not be found.
I would also recommend Invalidate Cache/Restart on Android Studio or IntelliJ after that.
Hope that helps.

Categories

Resources