开发 Android 库时使用依赖项

问题描述 投票:0回答:1

我正在开发一个Android库,我在项目模块中使用一些依赖项,这些依赖项发布到本地maven仓库,这就是我在我的

gradle.build.kts
中声明我的依赖项的方式:

    dependencies {

    // Retrofit
    implementation(libs.retrofit)
    implementation(libs.retrofit.gson)
    implementation(libs.okhttp)
    implementation(libs.okhttp.logging)

    // room
    implementation(libs.room.runtime)
    implementation(libs.room.ktx)
    ksp(libs.room.compiler)
    testImplementation(libs.room.testing)

    // Work Manager
    implementation(libs.work.runtime.ktx)

    // Play services
    implementation(libs.play.services.location)

    // DI
    implementation(libs.koin.android)
}

我的出版部分直接说:

publishing {
    publications {
        create<MavenPublication>("release") {
            artifact("$buildDir/outputs/aar/${project.name}-release.aar")
            groupId = "com.mylibrary.android"
            artifactId = "core"
            version = project.extra["baseVersionName"] as String
        }
    }
    repositories {
        maven {
            url = uri("${project.buildDir}/repo")
        }
    }
}

我运行

./gradlew assembleRelease
,而不是
./gradlew publish
并在本地位置获取 AAR 文件。

但是,当我尝试在示例应用程序中使用此 SDK 时,使用以下行

implementation("com.mylibrary.android:core:0.3.1")

我在依赖项上遇到

java.lang.NoClassDefFoundError: Failed resolution of:
错误,因为我的示例应用程序没有,我也不希望它在其 gradle 文件中声明任何依赖项。

我已经看到使用

api
而不是
implementation
的解决方案,但这并没有改变错误。

我看过有关 fat-aar 的讨论,但我不确定这是正确的方法,有谁知道在可以用它发布的库中拥有依赖项的正确方法是什么?它们是否应该与它一起发布?

android android-gradle-plugin android-library android-maven-plugin
1个回答
0
投票

感谢@CommonsWare,我成功地理解了库发布中的一个关键步骤,即打包 AAR 文件和创建 Maven 存储库之间的区别。

通过向

pom
中的
publishing
脚本添加
gradle,build.kts
块,我添加了 Maven 存储库所需的元数据,以便能够处理我的库依赖项,而无需将其打包到我的 AAR 文件中

publishing {
    publications {
        create<MavenPublication>("release") {
            artifact("$buildDir/outputs/aar/${project.name}-release.aar")
            groupId = "com.library.android"
            artifactId = "core"
            version = project.extra["baseVersionName"] as String

            pom {
                name.set("SDK name")
                description.set("Description of library")
                url.set("http://www.url.co")

                withXml {
                    val dependenciesNode = asNode().appendNode("dependencies")
                    configurations["api"].allDependencies.forEach {
                        val dependencyNode = dependenciesNode.appendNode("dependency")
                        dependencyNode.appendNode("groupId", it.group)
                        dependencyNode.appendNode("artifactId", it.name)
                        dependencyNode.appendNode("version", it.version)
                        dependencyNode.appendNode("scope", "compile") // "compile" for API, "runtime" for implementation
                    }
                }
            }
        }
    }

    repositories {
        mavenLocal()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.