轉載請注明出處:http://blog.csdn.net/u013813116/article/details/54344549
大家好,接觸android已經快兩年了,在大學那會學得都比較雜:有寫過微信開發、web端開發、HTML5,後面才接觸的android。在這兩年裡多虧了各路大神的幫忙,一直都是看着大神們的部落格成長的。不能總是一味的索取,不做任何貢獻。是以決定從今天起也和大家分享下自己在成長路上的一些心得體會。
哈!廢話多了,那就讓我們開始吧! 很多東西我們都喜歡智能化點,當然對于程式我們也是希望如此。當我們進行工程項目開發時難免會有多個modules,而每個modules目錄下都會有一個build.gradle,build.gradle隻對相應的modules起作用,可以重寫任何的參數,它大概應該是這樣的:
android {
compileSdkVersion 24//編譯的SDK版本
buildToolsVersion "24.0.3" //編譯的Tools版本
defaultConfig { //預設配置
applicationId "xxxxxx"//應用程式的包名
minSdkVersion 15 //支援的最低版本
targetSdkVersion 24 //支援的目标版本
versionCode 1 //版本号
versionName "1.0" //版本名
}
signingConfigs {//簽名配置
release {//釋出版簽名配置
storeFile file('xxxxxx')
storePassword 'xxxx'
keyAlias 'xxxxxxx'
keyPassword 'xxxxxx'
}
debug {//debug版簽名配置
storeFile file('xxxxxx')
storePassword 'xxxx'
keyAlias 'xxxxxxx'
keyPassword 'xxxxxx'
} }
buildTypes { //build類型
release {
minifyEnabled false//混淆關閉
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'//指定混淆規則檔案
signingConfig signingConfigs.release//設定簽名資訊
}
debug {//調試
signingConfig signingConfigs.release
}
}
}
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'jniLibs')//編譯lib目錄下的.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:24.2.1'
testCompile 'junit:junit:4.12'
compile project(':xxxx')//編譯附加的項目
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
compile files('xxxxx')
}
當我們需要統一設定依賴庫、版本号或者依賴庫版本需要更新時,你會發現有點麻煩,需要進入到一個個build.gradle檔案裡進行修改。為什麼我們不建立一個可以進行全局配置的gradle檔案呢。
全局設定
首先在項目的根目錄下建立一個config.gradle檔案,如下圖所示:

config.gradle裡的資料根據項目需要進行設定,如下圖所示:
設定好config.gradle檔案後,接下來對每個modules下的build.gradle進行配置,如下圖所示:
是不是 so easy!等等,你該不會以為這樣就大功告成了吧!還有至關重要的一步哦,那就是工程項目根目錄下的build.gradle 裡還需添加一行: 然後sync下,ok!perfect。這樣以後要是版本更新,修改資料就友善多了