Include used 3rd party libraries in Android Library

Viewed 411

I made an Android Library. I upload it to Azure Artifact and pull it to my project from there.

I am making a service call with retrofit in the library. I'm using Moshi or Kotlinx-Serialization for Converter, but if I don't add these libraries to the Project where I use the library, I get errors like ClassNotFoundException. After adding, these errors go away, but the variables in the service models do not hold the data and remain null.

I think this is due to the 3rd party library (Moshi, KotlinX-Serialization, ...).

I found a way to include the 3rd party libraries I use in the library in the .aar package.

task copyLibs(type: Copy) {
    from configurations.implementation
    into 'libs'
}

But when I run the task I got this error.

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.

The .aar file path for my library consists of ->

Projects/MySDK-Android/MySDK/build/outputs/aar/MySDK-release.aar

I think if I include the 3rd party library I use for serialization (Moshi, KotlinX-Serilalization, Gson, ...) into my library, the problem will be solved.

Any help on how I can do it?

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
}
0 Answers
Related