2

我们有一个应用程序,它具有 3 种不同的构建类型(alpha、beta、release)和不同的风格(英国、美国......),具体取决于部署的国家/地区。

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

编辑:Gradle 文件(没有库)

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 ...
}
4

1 回答 1

0

你可以使用命令行

./gardlew 构建

或者在 android studio 应用程序中使用 gradle 菜单 -> 任务 -> 构建 -> 构建

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

于 2019-02-21T11:02:59.823 回答