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
       //移除無用資源
        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 }
   compile (project(":libray")){
        //不引用如下包
        exclude module: 'support-v4'
        //不傳遞引用
        transitive false
    }
//測試依賴 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 }

gradle android dsl 屬性大全 http://google.github.io/android-gradle-dsl/current/index.html 

以上配置如有錯誤, 還請指出


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM