Android Gradle 配置選項合集

來源:互聯網
上載者:User

標籤:

//讓gradle 引入構建安卓app的外掛程式apply plugin: ‘com.android.application‘//自訂變數customProp {    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 ‘demo‘            //讀取配置            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"]    }    //構建變種, flavor 和 defaultConfig類型相同    productFlavors {        //變種1        flavor1 {            packageName "com.example.flavor1"            versionCode 20        }        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 } //測試依賴 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‘}//清理構建後資源的tasktask clean(type: Delete) { delete rootProject.buildDir}

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

Android Gradle 配置選項合集

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.