https://kotlinlang.org logo
b

Big Chungus

01/28/2021, 11:05 AM
Hi guys, does anyone have an example gradle config of MPP library including android target?
y

Yaniv Sosnovsky

01/28/2021, 11:26 AM
do you mean kotlin multiplatform?
Hopefully this can help you - its a working project using ktor, kodein, sqldelight and dokka
message has been deleted
Copy code
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
    id("com.android.library")
    kotlin("multiplatform")
    kotlin("plugin.serialization")
    id("com.squareup.sqldelight")
    id("org.jetbrains.dokka")
    `maven-publish`
}
group = "com.texel.demosdk"
version = "2.0.10"

repositories {
    gradlePluginPortal()
    google()
    jcenter()
    mavenCentral()
}

kotlin {
    android {
        publishLibraryVariants("release", "debug")
        publishLibraryVariantsGroupedByFlavor = true // publishAndroidPublicationToGithubPackages instead of Debug and Release. this allows for single 'implementation'
    }
    ios {
        binaries {
            framework {
                baseName = "TexelCoreBL"
                linkerOpts.add("-lsqlite3")
            }
        }
    }

    //  workaround for NativeSqliteDriver issue -  <https://stackoverflow.com/a/62916853/1868624>
    val onPhone = System.getenv("SDK_NAME")?.startsWith("iphoneos") ?: false
    if (onPhone) {
        iosArm64("ios")
    } else {
        iosX64("ios")
    }

    sourceSets {
        val ktorVersion = "1.4.1"
        val coroutineVersion = "1.4.2-native-mt" //native-mt is important! it means (multi-threading. see: <https://kotlinlang.org/docs/mobile/concurrency-and-coroutines.html#multithreaded-coroutines>)
        val sqldelightDriverVersion = "1.4.3"
        val kodeinVersion = "7.1.0"
        val commonMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutineVersion")
                implementation("io.ktor:ktor-client-core:$ktorVersion")
                implementation("io.ktor:ktor-client-logging:$ktorVersion")
                implementation("io.ktor:ktor-client-serialization:$ktorVersion")
                implementation("org.kodein.di:kodein-di:$kodeinVersion")
                implementation("com.squareup.sqldelight:coroutines-extensions:$sqldelightDriverVersion")
            }
        }

        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val androidMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutineVersion")
                implementation("io.ktor:ktor-client-android:$ktorVersion")
                implementation("io.ktor:ktor-client-logging-jvm:$ktorVersion")
                implementation("io.ktor:ktor-client-serialization-jvm:$ktorVersion")
                implementation("com.squareup.sqldelight:android-driver:$sqldelightDriverVersion")
            }
        }
        val androidTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.13.1")
            }
        }

        val iosMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.3.7")
                implementation("io.ktor:ktor-client-ios:$ktorVersion")
                implementation("io.ktor:ktor-client-logging-native:1.3.2")
                implementation("io.ktor:ktor-client-serialization-native:1.3.2")
                implementation("com.squareup.sqldelight:native-driver:$sqldelightDriverVersion")
            }
        }
        val iosTest by getting
    }
}

sqldelight {
    database("AppDatabase") {
        packageName = "com.texel.sqldelight"
        schemaOutputDirectory = file("build/dbs")
    }
    linkSqlite = false
}


android {
    compileSdkVersion(29)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(21)
        targetSdkVersion(29)
        versionCode = 1
        versionName = "1.0"
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}
val packForXcode by tasks.creating(Sync::class) {
    group = "build"
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
    val targetName = "ios" + if (sdkName.startsWith("iphoneos")) "Arm64" else "X64"
    val framework =
        kotlin.targets.getByName<KotlinNativeTarget>(targetName).binaries.getFramework(mode)
    inputs.property("mode", mode)
    dependsOn(framework.linkTask)
    val targetDir = File(buildDir, "xcode-frameworks")
    from({ framework.outputDirectory })
    into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcode)

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
b

Big Chungus

01/28/2021, 11:37 AM
Is the repo where this file is hosted open-source?
y

Yaniv Sosnovsky

01/28/2021, 11:41 AM
sorry. the repo is private but I can try to help you
b

Big Chungus

01/28/2021, 11:42 AM
Right, first off, i don;t see a debug variant declared in your config
Is that expected?
Also, do you have to have android studio installed to build android version of the lib?
Basically I'm asking what's required to build it in the pipeline?
y

Yaniv Sosnovsky

01/28/2021, 11:43 AM
we have android studio and we publish the sdk versions to github, then, the consuming application just uses it as a gradle import
Copy code
publishing {
    repositories {
        maven {
            name = "GithubPackages"
            url = uri("your_url")
            credentials {
                username = githubUsername
                password = githubToken
            }
        }
    }
}
We added this to our gradle. When we want to release a version for android we run the command • publishAndroidPublicationToGithubPackagesRepository
b

Big Chungus

01/28/2021, 11:56 AM
Do you not have CI/CD?
y

Yaniv Sosnovsky

01/28/2021, 12:13 PM
not at the moment, no
c

Casey Brooks

01/28/2021, 3:54 PM
You can take a look at this library of mine, which is using Github Actions for CI and publishes separate JVM and Android artifacts https://github.com/copper-leaf/clog