在Android库中包含使用的第三方库

3
我创建了一个 Android Library,并将其上传到 Azure Artifact,然后从那里拉到我的项目中。 在库中使用 Retrofit 进行服务调用。我使用 MoshiKotlinx-Serialization 作为转换器,但如果我不将这些库添加到使用该库的项目中,我会遇到 ClassNotFoundException 等错误。添加后,这些错误消失了,但服务模型中的变量没有保存数据,仍为空。 我认为这是由于第三方库(Moshi、KotlinX-Serialization 等)造成的。 我找到了一种方法,可以将我在库中使用的第三方库包含在 .aar 包中。
task copyLibs(type: Copy) {
    from configurations.implementation
    into 'libs'
}
但是当我运行任务时,出现了这个错误。
Resolving dependency configuration 'implementation' is not allowed as it is defined as 'canBeResolved=false'.
Instead, a resolvable ('canBeResolved=true') dependency configuration that extends 'implementation' should be resolved.
我的库的.aar文件路径为 ->
Projects/MySDK-Android/MySDK/build/outputs/aar/MySDK-release.aar

我认为如果我将用于序列化的第三方库(如Moshi、KotlinX-Serialization、Gson等)包含在我的库中,问题就会得到解决。

请问有什么帮助可以提供?

build.gradle

plugins {
    id 'com.android.library'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'kotlin-parcelize'
    id "kotlinx-serialization"
}

android {
    compileSdk 31

    defaultConfig {
        minSdk 23
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

    kotlin {
        // for strict mode
        explicitApi()

        // for warning mode
        explicitApiWarning()
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0"

    // Retrofit
    implementation "com.squareup.retrofit2:retrofit:2.9.0"
    implementation "com.squareup.okhttp3:logging-interceptor:4.9.1"

    // Logger
    implementation 'com.orhanobut:logger:2.2.0'

    // Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2"

    // KotlinX Serialization
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2"
    implementation "com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0"

}


task copyLibs(type: Copy) {
    from configurations.customConfig
    into "$project.rootDir/build/outputs/"
}

apply plugin: 'maven-publish'
apply plugin: "kotlinx-serialization"

publishing {
    publications {
        maven(MavenPublication) {
            groupId 'com.my.mysdk-android'
            artifactId 'mysdk-android'
            version '1.0.0'
            artifact("$projectDir/build/outputs/aar/MySDK-release.aar")
        }
    }

    repositories {
        maven {
            url 'https://pkgs.dev.azure.com/......'
            credentials {
                username "MySDK.Android"
                password accessToken
            }
        }
    }
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}
1个回答

0
经过几个小时的研究,我终于解决了这个问题。问题出在你的实现上。当你发布库时,第三方实现没有打包进去。你需要告诉Maven这个库使用了一些第三方库。如果你使用的是build.gradle.kts,请将以下代码添加到你的发布配置中。
publishing {
    publications {
        register<MavenPublication>("maven") {
            groupId = "yourGroupId"
            artifactId = "yourArtifactId"
            version = "versionOfTheLibrary"

            // You don't have to change this line. artifact("$projectDir/build/outputs/aar/MySDK-release.aar") is fine
            afterEvaluate { artifact(tasks.getByName("bundleReleaseAar")) }

            // Here is the magic happens
            pom.withXml {
                val dependencies = asNode().appendNode("dependencies")

                val addNode = { groupId: String, artifactId: String, version: String ->
                    val dependency = dependencies.appendNode("dependency")
                    dependency.appendNode("groupId", groupId)
                    dependency.appendNode("artifactId", artifactId)
                    dependency.appendNode("version", version)
                }

                addNode("com.example", "dependency name", "version")

                // In your case, add below lines
                addNode("com.squareup.retrofit2", "retrofit2", "2.9.0")
                addNode("com.squareup.okhttp3", "logging-interceptor", "4.9.1")
                addNode("com.orhanobut", "logger", "2.2.0")
                addNode("org.jetbrains.kotlinx", "kotlinx-coroutines-android", "1.5.2")
                addNode("org.jetbrains.kotlinx", "kotlinx-coroutines-core", "1.5.2")
                addNode("org.jetbrains.kotlinx", "kotlinx-serialization-json", "1.3.2")
                addNode("com.jakewharton.retrofit", "retrofit2-kotlinx-serialization-converter", "0.8.0")
               // Add other dependencies that you use in the code
            }
        }
    }

    repositories {
        maven {
            url = uri(localProperties.getProperty("ABTOOL_MAVEN_WRITE_URL"))
            credentials {
                username = localProperties.getProperty("ABTOOL_MAVEN_USERNAME")
                password = localProperties.getProperty("ABTOOL_MAVEN_WRITE_PASSWORD")
            }
        }
    }
}
如果你使用的是Groovy而不是gradle.kts,你可以像这样添加
publishing {
    publications {
        maven(MavenPublication) {
            groupId 'com.my.mysdk-android'
            artifactId 'mysdk-android'
            version '1.0.0'
            artifact("$projectDir/build/outputs/aar/MySDK-release.aar")

            pom.withXml {
                def dependencies = asNode().appendNode("dependencies")

                def addDependency = { groupId, artifactId, version ->
                    def dependency = dependencies.appendNode("dependency")
                    dependency.appendNode("groupId", groupId)
                    dependency.appendNode("artifactId", artifactId)
                    dependency.appendNode("version", version)
                }

                addDependency("com.example", "dependency name", "version")

                // In your case, add below lines
                addDependency("com.squareup.retrofit2", "retrofit2", "2.9.0")
                addDependency("com.squareup.okhttp3", "logging-interceptor", "4.9.1")
                addDependency("com.orhanobut", "logger", "2.2.0")
                addDependency("org.jetbrains.kotlinx", "kotlinx-coroutines-android", "1.5.2")
                addDependency("org.jetbrains.kotlinx", "kotlinx-coroutines-core", "1.5.2")
                addDependency("org.jetbrains.kotlinx", "kotlinx-serialization-json", "1.3.2")
                addDependency("com.jakewharton.retrofit", "retrofit2-kotlinx-serialization-converter", "0.8.0")
                // Add other dependencies that you use in the code
            }
        }
    }

    repositories {
        maven {
            url 'https://pkgs.dev.azure.com/......'
            credentials {
                username "MySDK.Android"
                password accessToken
            }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,