【问题标题】:How to Add My Android Library's Dependencies in Github Packages?如何在 Github 包中添加我的 Android 库的依赖项?
【发布时间】:2020-03-15 19:56:36
【问题描述】:

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

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

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

【问题讨论】:

  • 您需要在您希望允许最终用户访问的第三方库的库 gradle 文件中将 implementation 更改为 api。在您的情况下使用它,然后重新发布您的库:(即api 'com.alimuzaffar.lib:pinentryedittext:2.0.6'
  • 对不起。我刚刚尝试过,但没有奏效。我只需要将implementation 更改为api 还是还有其他事情要做?
  • 您是否将库添加为 aar 文件
  • 是的,我正在使用 aar 文件。我按照tutorial 制作了我的 github 包。
  • 哦,在这种情况下,我的解决方案将不起作用,因为它不适用于 aar 文件。您需要创建 fat aar,我以为您将其发布到 maven 或 jitpack。

标签: java android gradle github github-package-registry


【解决方案1】:

如果有人需要,经过大量谷歌搜索,并尝试了一些 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
    // ... ... ...
}

【讨论】:

    【解决方案2】:
    • 本教程中显示的库文件 (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

    【讨论】:

    • 这是公认的解决方案!非常感谢!这正是我想要的! :D
    猜你喜欢
    • 2014-03-14
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 2022-01-14
    • 2018-05-27
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多