【
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'
}
提示:关于更多的统一配置都可以以此种形式进行,如打包签名的配置等。。
】