build.gradle 的設置和相關說明
<pre name="code" class="java">
// Module的項目(Android工程)下的 build.gradle
//=======================================================
//聲明當前構建的是 android 應用 還是 library
apply plugin: 'com.android.library'
apply plugin: 'com.android.application'
//依賴包
dependencies{
//加載本地目錄下的jar包
compile fileTree(dir:'libs',include:'*.jar')
//加載本地目錄下的.so庫,版本>2.4 Gradle可使用,低版本添加.so 請google
compile fileTree(dir: 'libs/armeabi', include: '*/so')
//直接指定依賴的包,地址是當前工程爲根目錄
compile files('libs/android-support-v4.jar')
//引用當前目錄下的android-library項目,‘eventbuslib’爲項目名稱,注意":"一定要有
compile project(':eventbuslib')
//配置遠程倉庫中的jar包,打包時,檢查本地有沒有,若沒有則下載到本地
//下載的位置:項目名/build/intermediates/exploded-arr/
compile 'com.android.support:appcompat-v7:22.0.0'
}
//設置編碼類型
tasks.withType(JavaCompile) { options.encoding = "UTF-8" }
//設置當前Android應用構建信息
android {
//打包SDK版本
compileSdkVersion 21
//編譯工具版本
buildToolsVersion "20.0.0"
//默認打包配置
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
//資源文件目錄
sourceSets{
main{
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
//打簽名設置
signingConfigs{
myConfig{
//key文件,絕對路徑和相對路徑都可以
storeFile file("doucument/pragram_key/bluekey")
storePassword "key鑰密碼"
keyAlias "別名"
keyPassword "別名密碼"
}
}
//構建簽名和混淆
buildTypes {
release {
//簽名文件
signingConfig signingConfigs.myConfig
//是否打開混淆
minifyEnabled false
//混淆配置文件
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
//打包設置
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
exclude 'META-INF/ASL2.0'
}
//測試所在的路徑,這裏假設是tests文件夾,沒有可以不寫這一行
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
//=======================================================
//Android Studio 的 Project 目錄下的 build.gradle
//=======================================================
//設置腳本的運行環境
buildscript {
//使用maven的依賴,用於項目的依賴
repositories {
mavenCentral()
}
//依賴插件
dependencies {
//注意,插件使用這裏的Gradle版本儘量要和你Android Studio的Gradle插件版本一致
classpath 'com.android.tools.build:gradle:1.3.0'
}
}
//舉例
//共享配置
allprojects {
//所有的項目(包括當前項目以及其子項目)應用上idea插件。
apply plugin: 'idea'
}
//舉例
//共享配置:這就會給所有子項目設置上java的插件、使用mavenCentral作爲 //所有子項目的repository以及對Guava[4]和JUnit的項目依賴
subprojects {
apply plugin: 'java'
repositories {
mavenCentral()
}
ext {
guavaVersion = ’14.0.1’
junitVersion = ‘4.10’
}
dependencies {
compile(“com.google.guava:guava:${guavaVersion}”)
testCompile(“junit:junit:${junitVersion}”)
}
}
}
//=======================================================
//=======================多渠道打包================================
// 第一步:在對於的 Adnroid工程 目錄下的 AndroidManifest.xml中
// <application></application>
// 中添加
// <meta-data
// android:name="統計SDK的key例如U_MENG的"
// android:value="${CHANNEL_NAME}"
// />
// 第二步:在 Module項目(Android工程)下的 build.gradle中的添加如下
android {
// 你的其他配置代碼
productFlavors {
yingyongbao {
manifestPlaceholders = [ CHANNEL_NAME:"YING_YON_GBAO"]
}
umeng {
manifestPlaceholders = [ CHANNEL_NAME:"U_MENG" ]
}
wandoujia {
manifestPlaceholders = [ CHANNEL_NAME:"WAN_DOU_JIA" ]
}
}
// 你的其他配置代碼
}
還未完善,待續
更新時間 2015-9-28