简体   繁体   中英

How to Add My Android Library's Dependencies in Github Packages?

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 AppDemo s build.gradle file like this:

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

The problem is I get dependency errors, that is, MyLibrary s 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.

  • 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

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
    // ... ... ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM