如何在Github软件包中添加我的Android库依赖项?

我正在构建一个Android库(例如Mylibrary),该库将添加到公司的其他应用中。该库在build.gradle文件中具有某些依赖性,如下所示:

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

制作完库后,我创建了一个Github包,因此可以将其添加到AppDemo的{​​{1}}文件中的另一个应用程序中(例如AppDemo):

build.gradle

问题是我遇到依赖性错误,即dependencies{ implementation 'com.mycompany:mylibrary:1.2.3' // other dependencies } 的依赖性(在此示例中,如以上库的Mylibrary文件中所示,pinentryedittextplay-services-auth-api-phone)缺失。 我已经搜索了这个问题,并尝试了一些解决方案,例如Mobbeel fataar gradle plugin和其他一些类似的插件,但是我无法使它们正常工作。 谁能帮我这个忙,或者给我一个工作样本?任何帮助将是可观的。

cutmiss3 回答:如何在Github软件包中添加我的Android库依赖项?

万一有人需要,经过大量的Google搜索,并尝试了一些gradle插件(mobbeel,kezong等),我终于得出结论,在github包中添加依赖项是一个坏主意,因此最终用户应该包括那些依赖项。也就是说,最终用户应通过以下方式添加gradle依赖项:

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
    // ... ... ...
}
,
  • 本教程中显示的库文件(aar)将不包含传递依赖项。
  • 对于Maven仓库,Gradle将使用包含依赖项列表的pom文件下载依赖项。
  • 在教程所示的项目中,pom文件不会生成嵌套的依赖项列表。您必须在项目中指定依赖项,或者必须修改代码以生成包含依赖项的pom文件。
  • 使用以下代码并在Android库模块中更新build.gradle文件,以生成包含相关依赖项信息的.pom文件。

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

上面的代码来自Publish an Android library to Maven with aar and source jar

本文链接:https://www.f2er.com/3068090.html

大家都在问