AndroidStudio 使用Gradle多环境多渠道打包-方法2

由于项目需求需要在手机上安装不同服务器环境的APP,所以想到通过改变applicationIdSuffix后缀,因为用到了高德地图所以同时申请了两个key

源码如下:

apply plugin: 'com.android.application'
apply plugin: 'android-apt'

def APPLICATION_ID = "***.******.**";
android {
    signingConfigs {
        config {
            keyAlias '******'
            keyPassword '******'
            storeFile file('**.jks')
            storePassword '1******'
            v2SigningEnabled false
        }

        config_test {
            keyAlias '******_test'
            keyPassword '******'
            storeFile file('**_test.jks')
            storePassword '******'
            v2SigningEnabled false
        }
    }
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId APPLICATION_ID
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 7
        versionName "1.3.0"
        ndk {
            // 设置支持的SO库架构
            abiFilters 'armeabi', 'arm64-v8a', "x86"
        }
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    lintOptions {
        checkReleaseBuilds false
        abortOnError false
        ignoreWarnings true
    }

    aaptOptions.cruncherEnabled = false
    aaptOptions.useNewCruncher = false
    packagingOptions {
        exclude 'META-INF/*'
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.config
            resValue "string", "API_SERVER", "https://ebike.uma.com/"
            manifestPlaceholders = [APP_NAME: "******", AMAP_APPKEY: "*************************"]
        }

        debug {
            signingConfig signingConfigs.config_test
            applicationIdSuffix "test"
            resValue "string", "API_SERVER", "https://xaas-test.uma.com/"
            manifestPlaceholders = [APP_NAME: "******_dev", AMAP_APPKEY: "*************************"]
        }

    }

    //配置不同渠道
    productFlavors {
        main {

        }
    }

    //打包输出
    applicationVariants.all { variant ->
        def fileName
        def buildName
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            variant.productFlavors.each { product ->
                buildName = product.name //获取渠道名字
            }

            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                fileName = "******_V${defaultConfig.versionName}" + buildName + "_" + variant.buildType.name + "_" + createDate() + ".apk"
                output.packageApplication.outputFile = new File("apk" + '/', fileName)

            }
        }
    }
}

def createDate() {
    return new Date().format("MMdd_HH", TimeZone.getTimeZone("GMT+8"))
}

你可能感兴趣的:(AndroidStudio 使用Gradle多环境多渠道打包-方法2)