Android - 从buildType和flavor的组合生成所有可能的.apks

时间:2016-11-06 19:58:35

标签: android gradle android-productflavors android-build-type

我们的应用程序有3种不同的构建类型(alpha,beta,release)和不同的口味(英国,美国...),基于部署的国家/地区。

不是手动编译buildType和flavor的每个组合,而是有一个Gradle任务一次生成它们(uk-alpha,uk-beta,uk-release,usa-alpha,usa-beta,usa-release, ...)?

编辑:Gradle文件(不包含libs)

repositories {

    flatDir {
        dirs 'libs'
    }
}

final VERSION_MAJOR = 0
final VERSION_MINOR = 7
final VERSION_PATCH = 1

android {
    defaultConfig {
        applicationId "com.app"
        multiDexEnabled true
        versionName "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}"
        versionCode VERSION_MAJOR * 10000000 + VERSION_MINOR * 100000 + VERSION_PATCH * 1000
    }

    signingConfigs {
        release {
            keyAlias 'xxx'
            keyPassword 'xxx'
            storeFile file('xxx')
            storePassword 'xxx'
        }
    }

    dexOptions {
        preDexLibraries = true
        javaMaxHeapSize "2g"
    }

    buildTypes {
        release {
            minifyEnabled true
            debuggable false
            versionNameSuffix ".0-release_location-on_live-server"
            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        beta { 
            versionNameSuffix ".1-beta_location-off_live-server"
            minifyEnabled true
            debuggable true
            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        alpha { 
            versionNameSuffix ".2-alpha_location_off_test-server"
            minifyEnabled false
            debuggable true
        }

        debug { 
            versionNameSuffix ".3-debug_location_off_test-server"
            minifyEnabled false
            debuggable true
        }
    }
    productFlavors {
        uk {
            minSdkVersion 16
            targetSdkVersion 24
        }

        usa {
           minSdkVersion 16
           targetSdkVersion 24
       }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        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'
        exclude 'META-INF/services/javax.annotation.processing.Processor'
        exclude 'META-INF/rxjava.properties'
        exclude 'META-INF/rxandroid.properties'
    }
    lintOptions {
        checkReleaseBuilds false
        abortOnError true
        disable 'InvalidPackage'
    }
    applicationVariants.all { variant ->
//        if(variant.buildType.name.equals("release"))
//            variant.versionCode += 0
//        if(variant.buildType.name.equals("beta"))
//            variant.versionCode += 1
//        if(variant.buildType.name.equals("debug"))
//            variant.versionCode += 2

        variant.outputs.each { output ->
            output.outputFile = new File(
                    output.outputFile.parent,
                    output.outputFile.name.replace(".apk", "-${variant.versionName}.apk"))
        }
    }
}

afterEvaluate {
    tasks.matching {
        it.name.startsWith('dex')
    }.each { dx ->
        if (dx.additionalParameters == null) {
            dx.additionalParameters = ['--multi-dex']
        } else {
            dx.additionalParameters += '--multi-dex'
        }
    }
}

dependencies {
    ... libs ...
}

1 个答案:

答案 0 :(得分:0)

您可以使用命令行

./ gardlew构建

或在Android Studio应用中使用gradle菜单->任务->构建->构建

它将生成app / build / outputs / apk中的所有APK