Gradle 命令行参数覆盖 build.gradle 中的属性

2024-02-16

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

在项目/build.gradle中

buildscript {
    ...
    dependencies {
        classpath 'com.android.tools.build:gradle:x.x.x'
    }
}

在项目/应用程序/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 版本号作为参数)来构建任何不是由我维护的项目。

如果我们必须构建由其他人管理的多个项目,这会很有帮助。通过命令行参数覆盖我们的构建配置,这样我们就不需要每次都通过每个项目中的特定位置来更改它,从而节省大量时间新导入的项目。


放入你的gradle.properties默认值:

SDK_VERSION=26

Use in build.gradle:

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

Use: ./gradlew build -PSDK_VERSION=26

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Gradle 命令行参数覆盖 build.gradle 中的属性 的相关文章

随机推荐