8

配置gradle.properties - JackPeng博客

 3 years ago
source link: http://yuanfentiank789.github.io/2017/09/29/%E9%85%8D%E7%BD%AEgradle.properties/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
配置gradle.properties - JackPeng博客

在一些项目中会分拆app 和 lib , 这时候引用support的时候,一旦更改版本会出现需要同步更改两个地方的问题.这种情况,可以通过配置gradle.properties实现替换.

在项目编译过程中,gradle.properties配置的值会被编译解析,其作为配置文件使用是很有必要的.

在项目根目录的gradle.properties文件配置:

# Project-wide Gradle settings.
#添加ndk支持(按需添加)
android.useDeprecatedNdk=true
# 应用版本名称
VERSION_NAME=1.0.0
# 应用版本号
VERSION_CODE=100
# 支持库版本
SUPPORT_LIBRARY=24.2.1
# MIN_SDK_VERSION
ANDROID_BUILD_MIN_SDK_VERSION=14
# TARGET_SDK_VERSION
ANDROID_BUILD_TARGET_SDK_VERSION=24
# BUILD_SDK_VERSION
ANDROID_BUILD_SDK_VERSION=24
# BUILD_TOOLS_VERSION
ANDROID_BUILD_TOOLS_VERSION=24.0.3

这时候配置app和lib的build.gradle可以这样写:

android {
    compileSdkVersion project.ANDROID_BUILD_SDK_VERSION as int
    buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
 
    defaultConfig {
        applicationId project.APPLICATION_ID // lib项目不需要配置这一项
        versionCode project.VERSION_CODE as int
        versionName project.VERSION_NAME
        minSdkVersion project.ANDROID_BUILD_MIN_SDK_VERSION as int
        targetSdkVersion project.ANDROID_BUILD_TARGET_SDK_VERSION as int
    }
}
 
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    //这里注意是双引号
    compile "com.android.support:appcompat-v7:${SUPPORT_LIBRARY}"
    compile "com.android.support:design:${SUPPORT_LIBRARY}"
    compile "com.android.support:recyclerview-v7:${SUPPORT_LIBRARY}"
    compile "com.android.support:support-annotations:${SUPPORT_LIBRARY}"
    compile "com.android.support:cardview-v7:${SUPPORT_LIBRARY}"
    compile "com.android.support:support-v4:${SUPPORT_LIBRARY}"
}

这样配置后,当你需要升级你的编译版本,版本号,支持库等的时候,仅需要修改项目根目录的gradle.properties文件即可,是不是又方便了一点点?



About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK