版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/JourneyX/article/details/73620318
【
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
# 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()
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'
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'
}
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'
}
提示:關於更多的統一配置都可以以此種形式進行,如打包簽名的配置等。。
】