在android基础项目gradle文件中运行常用的gradle代码

时间:2016-06-14 18:53:40

标签: android gradle shared configure

我有一个使用我们所有应用程序的库。 除了共享代码和资源我想共享gradle代码。 每个应用程序构建: some_app /  |    - settings.gradle    - build.gradle  |    - mylib /      |       -build.gradle    - other_lib /    - app /      |         - build.gradle

现在我正在谈论根目录中的build.gradle。定义带有dependecies和allprojects {}闭包的buildScript闭包的那个。

我尝试使用来自:' ./ mylib / gradle_files / baseProject.gradle' 它工作,但然后我的应用程序无法找到Android插件。由于某种原因,类路径依赖项没有注入构建脚本! 然后我尝试创建一个列表并使用它:

ext.projectBaseDependencies = ['com.android.tools.build:gradle:2.1.2', 'com.google.gms:google-services:3.0.0']

然后我在封口中使用了这个列表:

apply from: './mylib/gradle_files/baseProject.gradle'

buildscript {     存储库{         jcenter()     }     依赖{         classpath projectBaseDependencies     } }

它不起作用,gradle没有找到我在baseProject.gradle中定义的变量。 我有一个预感,适用于编译后,对buildScript闭包进行评估: - (

有没有办法在所有应用程序之间共享gradle脚本代码?如果不适用,什么可以工作?在afterEvaluate {}子句中跳过配置和配置?

1 个答案:

答案 0 :(得分:0)

为什么不在公共库中使用Gradle文件?如果您的公共库包含build.gradle文件,则可以应用android库插件:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        consumerProguardFiles "proguard-rules.pro"
    }
}

dependencies {
    // Your common dependencies here
}

然后,您可以将该库添加到项目settings.gradle

include ':mobile'
project(':common').projectDir = new File("/Users/example/path/to/common")

然后将其添加到项目build.gradle

中的依赖项中
dependencies {
    compile project(':common')
    // Your other project dependencies
}