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"))
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,038评论 3 119
  • 通常来讲,人具有两种基本属性,一种名为自然属性,另一种则称之为社会属性。自然属性很好理解,是指人的肉体特征和生物特...
    笔热阅读 2,870评论 0 0
  • 女友红送的07年的熟普,昨晚居然让我泡成乌龙,好尴尬啊。 我真是狠狠佩服了一下自己。 当然也得到了资深茶友小白和嘉...
    梁瑞芳安阅读 3,459评论 0 0
  • 上午还是晴空万里,骄阳似火,而下午,乌云从四面八方汇聚,碧蓝的天空没在了黑压压的云层之中,极容易让人想起来...
    泉霖妈阅读 1,781评论 0 0