5

I am building an Android library (say, MyLibrary) which will be added to other apps of my company. The library has some dependencies in the build.gradle file like this:

dependencies{
    implementation 'com.alimuzaffar.lib:pinentryedittext:2.0.6'
    implementation "com.google.android.gms:play-services-auth-api-phone:17.3.0"
// ... other dependencies
}

After making the library, I created a Github package, so I can add it to another app (say AppDemo) in AppDemos build.gradle file like this:

dependencies{
    implementation 'com.mycompany:mylibrary:1.2.3'
    // other dependencies
}

The problem is I get dependency errors, that is, MyLibrarys dependencies (in this example, pinentryedittext , play-services-auth-api-phone as shown in the library's build.gradle file above) are missing. I have googled the problem and tried out some solutions, such as, Mobbeel fataar gradle plugin,and some other similar plugins, but I could not make them work. Could anyone help my with this or give me a working sample? Any help will be appreciable.

Qazi Fahim Farhan
  • 2,066
  • 1
  • 14
  • 26
  • You'll need to change `implementation` to `api` in your library gradle file for which third party library you want to allow access to your end users. In your case use this and then republish your library: (I.e. `api 'com.alimuzaffar.lib:pinentryedittext:2.0.6'`) – Jeel Vankhede Nov 20 '19 at 07:26
  • Sorry. I have just tried it, but it did not work. DO I have to only change `implementation` to `api` or is there anything else to do? – Qazi Fahim Farhan Nov 20 '19 at 07:50
  • Are you adding your library as **aar file**? – Jeel Vankhede Nov 20 '19 at 08:34
  • yes, I am using aar file. I followed this [tutorial](https://proandroiddev.com/publishing-android-libraries-to-the-github-package-registry-part-1-7997be54ea5a) to make my github package. – Qazi Fahim Farhan Nov 20 '19 at 09:03
  • Oh, in that case my solution won't work because it is not possible for aar files. You'll need to create fat aar, I thought you're publishing it to maven or jitpack. – Jeel Vankhede Nov 20 '19 at 09:06

2 Answers2

2
  • The library file (aar) shown in the tutorial will not include the transitive dependencies.
  • For Maven repos, Gradle will download the dependencies using the pom file which will contain the dependencies list.
  • In the project shown in the tutorial the pom file does not generate the nested dependencies list. Either you have to specify the dependencies in your project or you’ll have to modify the code to generate a pom file with dependencies included.
  • Use the below code and update your build.gradle file inside the Android library module to generate the .pom file with information about dependencies included.

publications {
    bar(MavenPublication) {
        groupId getGroupId()
        artifactId getArtificatId()
        version getVersionName()
        artifact("$buildDir/outputs/aar/${getArtificatId()}-release.aar")
        pom.withXml {
            final dependenciesNode = asNode().appendNode('dependencies')
            ext.addDependency = { Dependency dep, String scope ->
                if (dep.group == null || dep.version == null || dep.name == null || dep.name == "unspecified")
                    return // ignore invalid dependencies
                final dependencyNode = dependenciesNode.appendNode('dependency')
                dependencyNode.appendNode('groupId', dep.group)
                dependencyNode.appendNode('artifactId', dep.name)
                dependencyNode.appendNode('version', dep.version)
                dependencyNode.appendNode('scope', scope)
                if (!dep.transitive) {
                    final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
                    exclusionNode.appendNode('groupId', '*')
                    exclusionNode.appendNode('artifactId', '*')
                } else if (!dep.properties.excludeRules.empty) {
                    final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
                    dep.properties.excludeRules.each { ExcludeRule rule ->
                        exclusionNode.appendNode('groupId', rule.group ?: '*')
                        exclusionNode.appendNode('artifactId', rule.module ?: '*')
                    }
                }
            }
            configurations.compile.getDependencies().each { dep -> addDependency(dep, "compile") }

            configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }

            configurations.implementation.getDependencies().each { dep -> addDependency(dep, "runtime") }
        }
    }
}

the above code is referred from Publish an Android library to Maven with aar and source jar

0

In case anyone needs, After a lot of Googling around, and trying some gradle plugins (mobbeel, kezong etc), I finally came to the conclusion that adding dependencies in github packages is a bad idea, and so the end user should include those dependencies. That is, the end user should add gradle dependencies in this way:

dependencies{
    implementation 'com.mycompany:mylibrary:1.2.3' // <-- the actual lirbary
    // library dependencies
    implementation 'com.alimuzaffar.lib:pinentryedittext:2.0.6' 
    implementation "com.google.android.gms:play-services-auth-api-phone:17.3.0"
    // other dependencies of end user's app
    // ... ... ...
}
Qazi Fahim Farhan
  • 2,066
  • 1
  • 14
  • 26