当前位置: 代码迷 >> Android >> Android使用gradle不同配置多项目封装
  详细解决方案

Android使用gradle不同配置多项目封装

热度:11   发布时间:2016-04-27 23:28:54.0
Android使用gradle不同配置多项目打包
//build.gradle该配置文件中路径均是相对路径apply plugin: 'com.android.application'android {    //编译sdk版本    compileSdkVersion 21    //编译工具版本    buildToolsVersion "23.0.0 rc2"    //是否强制唯一包名    enforceUniquePackageName = false    lintOptions {        //执行lint检查,不会终止编译        abortOnError false    }    dexOptions {        preDexLibraries = false    }    signingConfigs {        commonConfig {            storeFile file("wasu.keystore")            storePassword "wasu1234"            keyAlias "android.keystore"            keyPassword "wasu1234"        }    }    defaultConfig {        applicationId "cn.com.wasu.backplay"        minSdkVersion 11        targetSdkVersion 15        versionCode 11        versionName "1.0.0.11"    }    buildTypes {        release {            //是否优化            zipAlignEnabled true            //是否混淆            minifyEnabled true            proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-project.txt'//这里的proguard-project.txt要修改成混淆配置文件名        }    }    productFlavors {        def flavorSign = signingConfigs.commonConfig        projectName001 {            sourceSets."$name".java.srcDirs += "build-types/projectName001"            signingConfig flavorSign            //如果要单独设置每个项目的版本号,将下面的versionCode和versionName取消注释//            versionCode 10//            versionName "1.0.0.10"        }        projectName002 {            sourceSets."$name".java.srcDirs += "build-types/projectName002"            signingConfig flavorSign//            versionCode 10//            versionName "1.0.0.10"        }    }    sourceSets {        main {            assets.srcDirs = ['src/main/assets']            res.srcDirs = ['src/main/res']            aidl.srcDirs = ['src/main/java']            resources.srcDirs = ['src/main/java']            renderscript.srcDirs = ['src/main/java']            jniLibs.srcDirs = ['libs']//同时包含*.so,*.jar文件            manifest.srcFile 'src/main/AndroidManifest.xml'            java {                srcDirs = ['src/main/java']                exclude 'cn/com/a/b/c/Common.java'//排除项目中的该类            }        }    }    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'    }    setup()    clean {        delete += "output"    }}dependencies {//在混淆文件中需要注释相关jar包,如下//#-libraryjars libs/dom4j-1.6.1.jar//#-libraryjars libs/jackson-core-asl-1.8.0.jar//#-libraryjars libs/jackson-mapper-asl-1.8.0.jar//#-libraryjars libs/junit4.4.jar//#-libraryjars libs/spring-android-rest-template-1.0.0.M2.jar//#-libraryjars libs/Log.jar//#-libraryjars libs/gson-2.2.4.jar    compile 'com.android.support:support-v4:21.0.3'    compile 'com.google.code.gson:gson:2.2.4'    compile files('libs/Log.jar')    compile files('libs/dom4j-1.6.1.jar')    compile files('libs/jackson-core-asl-1.8.0.jar')    compile files('libs/jackson-mapper-asl-1.8.0.jar')    compile files('libs/junit4.4.jar')    compile files('libs/spring-android-rest-template-1.0.0.M2.jar')}build.doLast {    def today = new Date().format('yyyyMMdd');    def dir_debug = 'output/' + today + '/debug'    def dir_release = 'output/' + today + '/release'    copy {//复制release版本apk到单独文件夹        from('build/outputs/apk')        into(dir_debug)        include('*_debug.apk')        exclude("*-unaligned.apk");        exclude("*-unsigned.apk");    }    copy {//复制debug版本apk到单独文件夹        from('build/outputs/apk')        into(dir_release)        include('*.apk')        exclude('*_debug.apk')        exclude("*-unaligned.apk");        exclude("*-unsigned.apk");    }}def setup() {//这个方法是用来命名debug和release版本的apk名称    def name = "androidxxxxxxx";    def today = new Date().format('yyyyMMddHHmm');    android.applicationVariants.all { variant ->        variant.outputs.each { output ->            def version = android.defaultConfig.versionName;//            def version = variant.productFlavors[0].versionName;            def file = output.outputFile            def buildTypeOfApp = variant.buildType.name            def outputName = name + variant.productFlavors[0].name + "_abcd_" + version             if (!variant.buildType.name.equalsIgnoreCase("debug")){                output.outputFile = new File(file.parent, outputName + "_" + today + ".apk")            }else{                output.outputFile = new File(file.parent, outputName + "_debug.apk");            }        }    }}

版权声明:本文为博主原创文章,未经博主允许不得转载。

  相关解决方案