Android gradle依赖项

时间:2017-03-20 10:11:59

标签: android android-studio gradle android-gradle

如何根据特定配置文件动态添加gradle依赖项 告诉android studio将哪些库添加到项目中

提前致谢

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你需要类似的东西:

  • build.gradle包含"标准"配置,共同和所有团队共享
  • 一些特殊配置(但对于同一个项目)可能因团队而异

并且你想要避免,你必须一直检查或评论一些依赖关系,因为最后一次签入来自团队B,而团队A需要一些其他依赖项(或相同依赖项的不同版本)。

到目前为止我是对的吗?

这可以通过gradle中的新标志来完成,称为apply-from。 或多或少,这是一个include命令,它允许您将单独文件的内容导入到当前的build.gradle配置中。

我在我的项目中使用它来包含签名配置(它仅对我的所有应用程序存在一次,并且只是"包含"在构建中,而不是一遍又一遍地复制)。 对于某些标准依赖项也是如此,例如支持库,浓缩咖啡和类似的东西。 这会使build.gradle文件变小。

从我的构建文件中查看此示例,我认为您可以根据自己的需要进行调整:

apply plugin: 'com.android.application'
apply from: '../../stdcfg/app/default.build.gradle'
apply from: '../../stdcfg/app/include.libs.gradle'
apply from: '../../stdcfg/app/signingconfig.gradle'

android {
    // ... normal contents of your build file
}

apply-from包含此build.gradle文件的任何文件。作为一个例子,这里是我的default.build.gradle文件的(部分)内容,你看,这甚至可以添加依赖项:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', 
    {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'

    compile 'com.android.support:cardview-v7:25.2.0'
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support:design:25.2.0'
    compile 'com.android.support:support-v4:25.2.0'
    //  compile 'com.google.android.gms:play-services-ads:10.2.0'
    //  compile 'com.google.android.gms:play-services-gcm:10.2.0'
}

那么,你如何适应它?

比如说,A队和B队各得到一个特定文件,我们将文件命名为teamconfig.gradle。 在A队的分支中,该文件包含A队的配置,B队的B队分支。 两个团队都为他们的应用程序使用相同的build.gradle文件,其中包含以下行:

apply from: '../branches/teamconfig/teamconfig.gradle'

此文件包含特定于团队的配置。

希望这有帮助。