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

tjjdgumg  于 2023-11-18  发布在  Android
关注(0)|答案(1)|浏览(196)

我做了一个Android Library。我把它上传到Azure服务器,然后把它拉到我的项目中。
我在库中使用reflect进行服务调用。我使用莫希Kotlinx-Serializationfor Converter,但如果我不将这些库添加到使用库的Project中,我会收到类似ClassNotFoundException的错误。添加后,这些错误消失,但服务模型中的变量不保存数据并保持为null。
我认为这是由于第三方库(莫希,KotlinX序列化,...)。
我找到了一种方法,将我在库中使用的第三方库包含在.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


我想如果我将用于序列化的第三方库(莫希,KotlinX-Serilalization,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
}

ws51t4hk

ws51t4hk1#

经过几个小时的研究,我终于解决了这个问题。所以问题是你的实现。当你发布库时,第三方实现没有打包。你必须告诉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
            }
        }
    }
}

相关问题