Gradle版本统一配置

1.修改local.properties文件

这个文件是properties文件,就是配置一下信息。
## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Fri Jun 23 10:22:35 CST 2017

ndk.dir = D\:\\ ndk\\ android-ndk-r9d
sdk.dir = D\:\\ sdk

minSdkVersion = 15
targetSdkVersion = 25
compileSdkVersion = 25
buildToolsVersion = 25.0.2
versionName = 1.0
versionCode = 1

compileSupportversion = 25.3.1


2.读取local.properties文件
     在工程根目录下创建一文件夹,名字任取暂且叫:gradleconfiguration------------》

     然后在文件下创建initgradle.gradle文件。

    
点击"Yes".-------------------------------------->在该文件中定义引用local.properties中的整体配置资源定义。
// 定义一个方法
def initProjectEnvironment() {
    // 打印
    Properties  properties new Properties()
    File  propertyFile new File( rootDir. getAbsolutePath() +  "/local.properties")
    properties.load( propertyFile.newDataInputStream())

    //config
    gradle.ext.minSdkVersion =  properties.getProperty( 'minSdkVersion').toInteger().intValue()
    gradle.ext.targetSdkVersion =  properties.getProperty( 'targetSdkVersion').toInteger().intValue()
    gradle.ext.compileSdkVersion =  properties.getProperty( 'compileSdkVersion').toInteger().intValue()
    gradle.ext.buildToolsVersion =  properties.getProperty( 'buildToolsVersion')
    gradle.ext.versionName =  properties.getProperty( 'versionName')
    gradle.ext.versionCode =  properties.getProperty( 'versionCode').toInteger().intValue()
    gradle.ext.compileSupportversion =  properties.getProperty( "compileSupportversion")

    println  "initialize  Gradle Environment completes..."
}

// 调用
initProjectEnvironment()

3.调用该文件

在哪里调用呢?
肯定是在构建项目的 初始化阶段也就是gradle的初始化阶段。
也就是settings.gradle文件
修改如下:
// 和你的 selfgradle 文件结构放在哪里有关
apply  from'./gradleconfiguration/initgradle.gradle'

include  ':appKong'':butterknifetest'

4.使用配置的信息

在你的主工程或者各个Modle下使用即可。往往在一个较大型的应用中,都会有较多的 library   ,即可用以上的配置信息统一控制。在对应的build.gradle中做如下修改:
apply  plugin'com.android.application'

android {
    compileSdkVersion  gradle.ext.compileSdkVersion
    buildToolsVersion  gradle.ext.buildToolsVersion

    defaultConfig {
        applicationId  "com.xys.butterknifetest"
        minSdkVersion  gradle.ext.minSdkVersion
        targetSdkVersion  gradle.ext.targetSdkVersion
        versionCode gradle.ext.versionCode
        versionName gradle.ext.versionName

        testInstrumentationRunner  "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled  false
            proguardFiles getDefaultProguardFile( 'proguard-android.txt'),  'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree( dir'libs'include: [ '*.jar'])
    androidTestCompile( 'com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude  group'com.android.support'module'support-annotations'
    })
    compile  'com.android.support:appcompat-v7:'+ gradle.ext.compileSupportversion
    testCompile  'junit:junit:4.12'
}

提示:关于更多的统一配置都可以以此种形式进行,如打包签名的配置等。。
  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值