---
source: tests/backends_kotlin_android_snapshot_test.rs
assertion_line: 245
expression: "&file.content"
---
// Generated by alef. Do not edit by hand.

import com.vanniktech.maven.publish.AndroidSingleVariantLibrary
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

buildscript {
    dependencies {
        classpath("com.vanniktech:gradle-maven-publish-plugin:0.36.0")
    }
}

plugins {
    id("com.android.library") version "9.2.0"
    id("com.vanniktech.maven.publish") version "0.36.0"
    id("org.jlleitschuh.gradle.ktlint") version "13.1.0"
    id("com.github.ben-manes.versions") version "0.52.0"
}

android {
    namespace = "dev.sample_crate"
    compileSdk = 36

    defaultConfig {
        minSdk = 24
        consumerProguardFiles("consumer-rules.pro")
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    sourceSets {
        getByName("main") {
            jniLibs.srcDirs("src/main/jniLibs")
        }
    }
}

kotlin {
    compilerOptions {
        jvmTarget.set(JvmTarget.JVM_17)
    }
}

ktlint {
    version.set("1.8.0")
    android.set(true)
    ignoreFailures.set(false)
}

dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib")
    // Generated Kotlin facade uses suspend functions and Flow wrappers, both of
    // which require kotlinx-coroutines-android (transitively pulls -core).
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.11.0")
    // Generated sealed-class DTOs use Jackson @JsonDeserialize for polymorphic
    // serde-tagged unions; jackson-module-kotlin is required for Kotlin
    // data-class deserialization (handles nullable, default values, etc.).
    // jackson-datatype-jdk8 is required because the generated DefaultClient.kt
    // registers Jdk8Module for Optional<T> / java.util.Optional support.
    implementation("com.fasterxml.jackson.core:jackson-databind:2.21.3")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.21.3")
    implementation("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.21.3")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.3.0")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.7.0")
}

// Build host JNI library for JVM unit tests (macOS/Linux/Windows).
// The generated Kotlin Bridge object calls System.loadLibrary("demo_jni") for JVM
// unit tests running on developer machines. This task builds the host-platform binary
// and stages it into src/test/resources/host-jni/<platform>/ for the test loader.
// Set alef.skipHostJni=true to disable this (e.g., in publish-only builds).
tasks.register("buildHostJni", Exec::class) {
    if (project.properties["alef.skipHostJni"] != "true") {
        val jniCargoPath = "../../crates/demo-jni/Cargo.toml"
        description = "Build host-platform JNI library from ../../crates/demo-jni"
        commandLine("cargo", "build", "--release", "--manifest-path", jniCargoPath)
        errorOutput = System.err
    } else {
        description = "Build host JNI (disabled via alef.skipHostJni=true)"
        commandLine("true")
    }
}

tasks.register("copyHostJni", Copy::class) {
    if (project.properties["alef.skipHostJni"] != "true") {
        description = "Copy host JNI library to test resources"
        dependsOn("buildHostJni")

        val hostPlatform = when {
            System.getProperty("os.name").lowercase().contains("mac") -> "darwin"
            System.getProperty("os.name").lowercase().contains("win") -> "windows"
            else -> "linux"
        }
        val jniCratePath = file("../../crates/demo-jni")
        val buildDir = jniCratePath.resolve("target/release")

        // Map host platform to library filename
        val libName = when (hostPlatform) {
            "darwin" -> "libdemo_jni.dylib"
            "windows" -> "demo_jni.dll"
            else -> "libdemo_jni.so"  // linux
        }

        from(buildDir) {
            include(libName)
        }
        into(layout.projectDirectory.dir("src/test/resources/host-jni/$hostPlatform"))
    }
}

tasks.withType<Test> {
    if (project.properties["alef.skipHostJni"] != "true") {
        val hostPlatform = when {
            System.getProperty("os.name").lowercase().contains("mac") -> "darwin"
            System.getProperty("os.name").lowercase().contains("win") -> "windows"
            else -> "linux"
        }
        systemProperty(
            "java.library.path",
            project.layout.projectDirectory.dir("src/test/resources/host-jni/$hostPlatform").asFile.absolutePath
        )
        dependsOn("copyHostJni")
    }
}

// `processDebugUnitTestJavaRes` and `processReleaseUnitTestJavaRes` package the
// `src/test/resources` tree into the unit-test runtime classpath. They consume
// the dylib emitted by `copyHostJni`, so AGP 8.10+ requires an explicit
// dependency declaration to satisfy Gradle's task-output validation.
tasks.matching { it.name.startsWith("processDebug") || it.name.startsWith("processRelease") }.configureEach {
    if (project.properties["alef.skipHostJni"] != "true" && name.contains("UnitTestJavaRes")) {
        dependsOn("copyHostJni")
    }
}

// Guard: fail the build if assembleRelease runs without jniLibs staged.
// This prevents accidental publication of jni-less AARs when gradle rebuilds
// during the publish phase. The publish workflow must extract jniLibs from
// pre-built AARs and stage them into src/main/jniLibs before invoking gradle.
// This check catches the bug where jniLibs are lost during publish-time rebuild.
tasks.register("validateJniLibsForRelease") {
    doFirst {
        if (gradle.taskGraph.hasTask("assembleRelease") || gradle.taskGraph.hasTask("publishAndReleaseToMavenCentral")) {
            val jniLibsDir = file("src/main/jniLibs")
            if (!jniLibsDir.exists() || jniLibsDir.listFiles()?.isEmpty() != false) {
                throw GradleException(
                    "FATAL: jniLibs directory is empty or missing. " +
                    "The Android AAR must include native .so libraries for ARM64 and x86_64. " +
                    "Ensure the publish workflow stages jniLibs from pre-built AARs " +
                    "into src/main/jniLibs/{arm64-v8a,x86_64}/ before invoking assembleRelease. " +
                    "Aborting to prevent shipping a jni-less AAR to Maven Central."
                )
            }
        }
    }
}

// Make assemble and publish tasks depend on the validation.
tasks.named("preBuild") {
    dependsOn("validateJniLibsForRelease")
}

mavenPublishing {
    configure(AndroidSingleVariantLibrary(
        variant = "release",
        sourcesJar = com.vanniktech.maven.publish.SourcesJar.Sources(),
        javadocJar = com.vanniktech.maven.publish.JavadocJar.Empty(),
    ))

    publishToMavenCentral()
    signAllPublications()

    coordinates(
        groupId = "dev.sample_crate",
        artifactId = "demo-android",
        version = "0.0.0",
    )

    pom {
        name.set("demo-android")
        description.set("Bindings for demo")
        url.set("https://github.com/example/demo")
        licenses {
            license {
                name.set("MIT")
                url.set("https://opensource.org/licenses/MIT")
            }
        }

        scm {
            url.set("https://github.com/example/demo")
            connection.set("scm:git:git://github.com/example/demo.git")
            developerConnection.set("scm:git:ssh://git@github.com:example/demo.git")
        }
    }
}
