1. 程式人生 > >Android Gradle 配置選項合集

Android Gradle 配置選項合集

//讓gradle 引入構建安卓app的外掛

apply plugin: 'com.android.application'

//自定義變數, 使用的時候不需要 ext 字首

ext {

minSdkVersion = 15

prop2 = "foo"

}

//自定義變數

def customProp2 = ["targetSdkVersion":23, "prop2":"bar"];

//自定義變數

// 根據日期自動生成build號

def calendar = Calendar.getInstance();

def tt = String.format("%d%02d%02d%02d",

calendar.get(Calendar.YEAR),

calendar.get(Calendar.MONTH)+1,

calendar.get(Calendar.DAY_OF_MONTH),

calendar.get(Calendar.HOUR_OF_DAY));

// 讀取local.properties檔案

def Properties properties = new Properties()

properties.load(project.rootProject.file(

'local.properties').newDataInputStream())

android {

compileSdkVersion 24

buildToolsVersion "24.0.0"

//簽名選項

signingConfigs {

demoSignCfg {

keyAlias PROPERTY_FROM_GRADLE.PROPERTIES

//讀取配置

keyPassword properties.getProperty("key.password")

storeFile file('demo.keystore'

)

storePassword properties.getProperty("key.password")

}

}

//編譯選項

compileOptions {

//使用jdk1.8 編譯

sourceCompatibility JavaVersion.VERSION_1_8

targetCompatibility JavaVersion.VERSION_1_8

}

//程式碼檢查選項

lintOptions {

//檢查釋出構建

checkReleaseBuilds rootProject.ext.checkReleaseBuilds

//遇到錯誤停止

abortOnError false

}

//打包選項

packagingOptions {

//去除的檔案

exclude 'META-INF/LICENSE'

exclude 'META-INF/LICENSE.txt'

exclude 'META-INF/NOTICE'

exclude 'META-INF/NOTICE.txt'

}

//資源打包選項

aaptOptions {

//不壓縮的檔案

noCompress 'foo', 'bar'

//過濾檔案

ignoreAssetsPattern "!.svn:!.git:!.ds_store:!*.scc:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"

}

//編譯dex選項

dexOptions {

//堆疊記憶體最多4g

javaMaxHeapSize "4g"

}

//配置

configurations {

//去掉所有的 com.android.support:support-annotations 依賴

all*.exclude group: 'com.android.support', module: 'support-annotations';

}

//預設全域性配置選項

defaultConfig {

applicationId "com.example.gradle_test"

minSdkVersion customProp.minSdkVersion

targetSdkVersion customProp2.targetSdkVersion

//使用生成的版本號

versionCode Integer.parseInt(tt)

versionName "1.0"

//Manifest 裡用的佔位符: <... android:label="${activityLabel}" />

manifestPlaceholders = [ activityLabel:"defaultName"]

//設定BuildConfig 欄位

     buildConfigField "String", "BASE_URL", '"https://www.baidu.com/"'

}

//構建變種, flavor 和 defaultConfig型別相同

productFlavors {

//變種1

flavor1 {

packageName "com.example.flavor1"

versionCode 20

//覆蓋defaultConfig的欄位

        buildConfigField "String", "BASE_URL", '"https://www.baidu.com/"'

}

flavor2 {

}

}

//配置各種目錄

sourceSets {

//主要

main {

manifest.srcFile 'AndroidManifest.xml'

java.srcDirs = ['src']

resources.srcDirs = ['src']

aidl.srcDirs = ['src']

renderscript.srcDirs = ['src']

res.srcDirs = ['res']

assets.srcDirs = ['assets']

jniLibs.srcDirs = ['libs']

}

//測試資源路徑

instrumentTest.setRoot('tests')

debug.setRoot('build-types/debug')

release.setRoot('build-types/release')

}

//構建型別

buildTypes {

//debug型別(只是個名字而已)

debug {

//app id 字尾: com.example.app.debug

applicationIdSuffix ".debug"

}

//釋出型別(只是個名字而已)

release {

//關閉混淆

minifyEnabled false

//使用的混淆檔案

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

//簽名選項

signingConfig signingConfigs.demoSignCfg<br>

//移除無用資源<br>       

shrinkResources true 

//版本名字尾

versionNameSuffix ".0"

//指定打包檔名

       applicationVariants.all { variant ->

         variant.outputs.each { output ->

def outputFile = output.outputFile

if (outputFile != null && outputFile.name.endsWith('.apk')) {

//輸出apk名稱為boohee_v1.0_2015-01-15_wandoujia.apk

def fileName = "apk_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"

             output.outputFile = new File(outputFile.parent, fileName)

           }

         }

       }

//過濾abi庫

ndk {

//只打包如下平臺的so

abiFilters "x86", "armeabi-v7a", "armeabi", 'mips'

}

}

//自定義

jnidebug {

// 繼承自上面的debug.

initWith debug

applicationIdSuffix ".jnidebug"

jniDebuggable true

}

}

//依賴倉庫

repositories {

maven { url "https://jitpack.io" }

}

}

//依賴管理

dependencies {

//編譯/執行時依賴

compile fileTree(dir: 'libs', include: ['*.jar'])

//$rootProject.ext 指的是 專案的那個build.gradle裡面定義了一個ext 變數

compile ("com.android.support:design:$rootProject.ext.SupportVersion"){

//不引用如下包

exclude module: 'support-v4'

exclude module: 'appcompat-v7'

//不傳遞引用

transitive false

}

1

2

3

4

5

6

compile (project(":libray")){

//不引用如下包

exclude module: 'support-v4'

//不傳遞引用

transitive false

}

1

//測試依賴 testCompile 'junit:junit:4.12' //外部提供, 不打包 provided 'com.android.support:appcompat-v7:23.4.0' //falvor1需要的依賴, f1Compile, f1Provided..... flavor1Compile 'com.android.support:appcompat-v7:24.0.0' } //清理構建後資源的task task clean(type: Delete) { delete rootProject.buildDir }