3

我正在构建一个 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 包,因此我可以将它添加到s文件中的另一个应用程序(例如AppDemo),如下所示:AppDemobuild.gradle

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

问题是我得到依赖错误,即缺少MyLibrarys 依赖项(在此示例中pinentryedittextplay-services-auth-api-phone如上面的库文件所示)。build.gradle我已经搜索了这个问题并尝试了一些解决方案,例如Mobbeel fataar gradle 插件和其他一些类似的插件,但我无法让它们工作。谁能帮我解决这个问题或给我一个工作样本?任何帮助都将是可观的。

4

2 回答 2

1
  • 本教程中显示的库文件 (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

于 2019-12-30T17:11:17.033 回答
0

万一有人需要,经过大量谷歌搜索,并尝试了一些 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
    // ... ... ...
}
于 2019-11-25T05:02:52.897 回答