gpt4 book ai didi

kotlin - 创建 jvm 和 Kotlin 多平台的 Github 库组合

转载 作者:行者123 更新时间:2023-12-02 13:11:36 26 4
gpt4 key购买 nike

我正在尝试构建一个 Android 库,我有以下内容:

  • 图书馆的 Github Repo
  • 库 1:可编译为 Android 和 iOS 的 Kotlin 原生/多平台代码
  • 库 2:Kotlin/JVM 代码依赖于库 1
  • 示例应用程序:使用库 2 和库 1 的应用程序
  • 个人安卓项目
  • 依赖上面的Github库

  • 在我的个人项目中,我只有:
    implementation 'com.github.username:myrepo:0.5'

    问题
  • 私有(private) Android 项目正在加载 android 库(库 2),但无法访问多平台 kotlin 模型和函数(库 1)。

    请注意,该库的示例应用程序运行良好。我怀疑 jitpack.io 没有正确发布工件。关于如何解决这个问题的任何想法?应该对 gradle 文件做些什么来解决问题?
  • 我想单独发布多平台库。我试图从多平台 Kotlin 模块访问它,但我得到“无法解决依赖关系”。这就是我正在尝试的:
    implementation 'com.github.username.myrepo:library1_moduleName:0.1'


  • Gradle for Library 1 多平台:
    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 = "library1"
    freeCompilerArgs.add("-Xobjc-generics")

    }
    }
    }

    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)

    库 2 的 Gradle
    apply plugin: 'com.android.library'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'

    android {

    repositories {
    google()
    jcenter()
    maven { url 'https://jitpack.io' }
    }

    compileSdkVersion 29
    buildToolsVersion "29.0.0"


    defaultConfig {
    minSdkVersion 19
    targetSdkVersion 29

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    consumerProguardFiles 'consumer-rules.pro'
    }

    buildTypes {
    release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
    }

    }

    dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    api project(":library1")
    }

    最佳答案

    我建议你改变你的build.gradle针对 android/的配置将其更改为

    plugins {
    kotlin("multiplatform")
    id("com.android.library")
    id("maven-publish")
    }

    android {
    compileSdkVersion 29
    buildToolsVersion "29.0.0"

    defaultConfig {
    minSdkVersion 19
    targetSdkVersion 29
    }

    buildTypes {
    release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
    }
    }

    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 = "library1"
    freeCompilerArgs.add("-Xobjc-generics")

    }
    }
    }

    // jvm("android")
    android {
    publishLibraryVariants("release")
    }

    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)

    创建 AndroidManifest.xml src/main/AndroidManifest.xml 下 library1 的文件

    <manifest package="library1"/>

    你现在可以将你的项目推送到 github,创建一个标签并等待 jitpack 发挥它的魔力

    当你想在 library2 中使用它时,在 dependencies 下部分,只需添加
    implementation 'com.github.username.myrepo:library1-android:0.1' //for android

    关于kotlin - 创建 jvm 和 Kotlin 多平台的 Github 库组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61176352/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com