AndroidStudio3.0多渠道打包

本文详细介绍了如何在Android应用开发中实现多渠道打包的过程。主要步骤包括:在AndroidManifest.xml中配置渠道信息,在build.gradle中设置多渠道打包的参数及自定义输出配置,通过productFlavors实现不同渠道的应用配置差异等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

###1、在AndroidManifest.xml中添加

android:name="CHANNEL"
android:value="${CHANNEL_VALUE}" />
复制代码

###2、在app模块下的build.gradle中添加一下代码

android {
    defaultConfig {
        // 默认是umeng的渠道
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
       flavorDimensions "release"
    }
    lintOptions {
        abortOnError false
    }
    signingConfigs {
        debug {
            storeFile file("../keystore/xiaoxing.jks")
            storePassword "xuxingxing"
            keyAlias "xiaoxing"
            keyPassword "xuxingxing"
        }
        release {
            storeFile file("../keystore/xiaoxing.jks")
            storePassword "xuxingxing"
            keyAlias "xiaoxing"
            keyPassword "xuxingxing"
        }
    }
     buildTypes {
        debug {
            minifyEnabled false
            buildConfigField "boolean", "LOG_DEBUG", "true"
        }
        release {
            minifyEnabled false
            // Zipalign优化
            zipAlignEnabled true
            // 移除无用的resource文件
            shrinkResources false

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            signingConfig signingConfigs.release
            // 自定义输出配置
            applicationVariants.all { variant ->
                variant.outputs.all { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        // 输出apk名称为JPay_0.0.1_xiaomi.apk
                        def fileName = "xiaoxing_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
                      // output.outputFile = new File(outputFile.parent, fileName)
                        outputFileName = new File("", fileName)
                    }
                }
            }
        }
    }

    //多渠道打包
    productFlavors {
        maqigou {
            dimension "release"
            // 每个环境包名不同
            applicationId "com.qingqu.wc.maqi"
            // 动态添加 string.xml 字段;
            // 注意,这里是添加,在 string.xml 不能有这个字段,会重名!!!
            resValue "string", "app_name", "瑪奇購"
            resValue "bool", "auto_updates", 'false'
            // 动态修改 常量 字段
            buildConfigField "String", "ENVIRONMENT", '"瑪奇購"'
            // 修改 AndroidManifest.xml 里渠道变量
            manifestPlaceholders = [CHANNEL_VALUE: "maqigou"
                                    , app_icon   : "@mipmap/logo"]
        }
    }

    productFlavors.all {
        flavor -> flavor.manifestPlaceholders = [CHANNEL_VALUE: name]
    }

}
复制代码

###3、说明

AndroidStudio3.0版本多渠道打包和之前的版本打包的区别: ① 在 defaultConfig 中添加 flavorDimensions "release,debug" ② 在buildTypes 中release 中 之前的写法是 output.outputFile = new File(outputFile.parent, fileName) 现在改为 outputFileName = new File("", fileName) ③ 在productFlavors 中的maqigou 添加 dimension "release"或者dimension "debug"

转载于:https://juejin.im/post/5a33279a5188250690541254

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值