androidandroid-gradle-pluginandroid-libraryandroid-maven-plugin

Using dependencies when developing an android library


I am developing an Android library, i use some dependencies in the project module which is published to a local maven repo, this is how i declare my dependencies in my 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)
}

my publishing section i straight forward:

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")
        }
    }
}

I run ./gradlew assembleRelease, than ./gradlew publish and get an AAR file in a local location.

However, when i try to use this SDK in a sample app, using the line

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

I get errors for java.lang.NoClassDefFoundError: Failed resolution of: on my dependencies, as my sample app dont, nor do i want it to, declare any dependencies in its gradle file.

I have seen solutions for using api instead of implementation however this did not change the error.

I have seen talk of fat-aar but i am not sure that is the correct approach, does anyone know what is the right approach to have dependencies in a library that can be published with it? should they be published with it at all?


Solution

  • Thanks to @CommonsWare i have managed to understand a crusial step in library publication which is the difference between packaging an AAR file and creating a Maven repo.

    By adding a pom block to my publishing script in the gradle,build.kts i added the neccessary metadata for the maven repo to be able to handle my libraries depencdencies without having tham packaged in my AAR file

    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()
        }
    }