Gradle命令行参数以覆盖build.gradle中的属性 [英] Gradle command line arguments to override properties in build.gradle

查看:155
本文介绍了Gradle命令行参数以覆盖build.gradle中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论何时导入现有的android项目,在大多数情况下,我们都需要更改已安装工具的版本号之后的值

Whenever an existing android project is imported, in most cases we need to change values following with our installed tools version number

在project/build.gradle

in project/build.gradle

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

在project/app/build.gradle

in project/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版本号作为参数)来构建我不维护的任何项目.

My idea is to use one same command (with my installed sdk version numbers as arguments) to build any project not maintained by me.

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

it is helpful if we have to build multiple projects which are managed by others.it can save much time by overriding their build configuration by ours through command line args so that we do not need to change it every time by going at particular location in each newly imported project.

推荐答案

放入您的 gradle.properties 默认值:

SDK_VERSION=26

build.gradle 中使用:

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

使用: ./gradlew build -PSDK_VERSION = 26

PS:别忘了,您还必须更改支持库的主要版本.

PS: Don't forget, that you must change support libraries major version also.

这篇关于Gradle命令行参数以覆盖build.gradle中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆