Gradle命令行参数覆盖build.gradle

时间:2017-11-01 09:18:21

标签: android groovy android-gradle build.gradle command-line-arguments

每当导入现有的android项目时,在大多数情况下我们需要使用已安装的工具版本号更改值

在project / build.gradle中

buildscript {
    ...
    dependencies {
        classpath 'com.android.tools.build:gradle:x.x.x'
    }
}
项目/ app / build.gradle中的

android {
    compileSdkVersion xx
    buildToolsVersion "xx.x.x"

    defaultConfig {
        applicationId "com.example.app.xyz"
        minSdkVersion xx
        targetSdkVersion xx
        versionCode x
        versionName "x.x"
    }
...
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:x.xx'
    compile 'com.android.support:appcompat-v7:xx.x.x'
    compile 'com.android.support:recyclerview-v7:xx.x.x'
}

如何覆盖

的上述值
classpath
compileSdkVersion
buildToolsVersion
targetSdkVersion

使用命令行参数,例如

./gradlew installDebug -PCompileSdkVersion=26 -PbuildToolsVersion=26.0.0

或类似的东西?

我的想法是使用一个相同的命令(使用我安装的sdk版本号作为参数)来构建我未维护的任何项目。

如果我们必须构建由其他人管理的多个项目,这将有所帮助。通过命令行参数覆盖我们的构建配置可以节省大量时间,这样我们就不需要每次都通过特定方式来更改它每个新进口项目的位置。

1 个答案:

答案 0 :(得分:3)

输入您的gradle.properties默认值:

SDK_VERSION=26

build.gradle中使用:

android {
    compileSdkVersion project.getProperties().get("SDK_VERSION")
}

使用:./gradlew build -PSDK_VERSION=26

PS:不要忘记,您还必须更改支持库的主要版本。

相关问题