Android 多渠道配置

Android 多包名,icon

本篇文章主要记录下android 下的同一工程,打包时配置不同的包名,icon,名称等信息.

1: 多包名

首先讲述下如何配置多包名.

在build.gralde的android 标签下添加:

productFlavors{
xiaomi{
applicationId “com.test.usagetest”
}
huawei{
applicationId “com.test.usagetest1”
}
}

此时如果我们运行的话,会出现下面错误:

A problem occurred configuring project ':app'.
> All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html

解决办法:

defaultConfig添加一行代码:

flavorDimensions “versionCode”

此时编译重新运行即可.

2: 多icon

  1. 修改manifest.xml

    <application
        android:allowBackup="true"
        android:icon="${app_icon}"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.UsageTest">
    

    将icon属性由原来的"@mipmap/ic_launcher" 替换成${app_icon}

  2. 修改build.gradle

    productFlavors{
    xiaomi{
    applicationId “com.test.usagetest”
    manifestPlaceholders = [app_icon : “@mipmap/ic_launcher”]
    }
    huawei{
    applicationId “com.test.usagetest1”
    manifestPlaceholders = [app_icon : “@mipmap/ic_launcher2”]
    }
    }

运行后可以看到icon已替换.

3:多名称

修改方法与icon一致.

productFlavors{
xiaomi{
applicationId “com.test.usagetest”
manifestPlaceholders = [app_icon : “@mipmap/ic_launcher”,
app_name : “test1”]
}
huawei{
applicationId “com.test.usagetest1”
manifestPlaceholders = [app_icon : “@mipmap/ic_launcher2”,
app_name : “test2”]
}
}

4: 多资源

不同的包名对应不同的资源文件.

配置res的不同路径.

sourceSets{
xiaomi{
res.srcDir(“src/main/res”)
}
huawei{
res.srcDir(“src/hw/res”)
}
}

相同资源名称下设置不同的值即可.

  • 30
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 多渠道打包指的是在同一份代码基础上,生成不同渠道的 APK 文件,这些 APK 文件在安装后会显示不同的渠道信息。 下面是一种常见的多渠道打包方式: 1. 在 app 模块的 build.gradle 文件中添加以下代码: ``` android { // 省略其他配置项 flavorDimensions "channel" productFlavors { googlePlay { dimension "channel" // 配置 Google Play 版本的包名等信息 applicationId "com.example.app.google" // 省略其他配置项 } xiaomi { dimension "channel" // 配置小米版本的包名等信息 applicationId "com.example.app.xiaomi" // 省略其他配置项 } huawei { dimension "channel" // 配置华为版本的包名等信息 applicationId "com.example.app.huawei" // 省略其他配置项 } } } ``` 2. 在 gradle.properties 文件中添加以下代码: ``` CHANNELS=googlePlay,xiaomi,huawei ``` 3. 创建一个名为 channel.gradle 的文件,用于动态生成不同渠道的 APK 文件: ``` def getChannelName() { // 从 gradle.properties 文件中读取 CHANNELS 变量 def channels = rootProject.ext.CHANNELS.split(',') // 获取当前渠道的索引,注意这个值是根据 assemble 命令的参数确定的 def channelIndex = project.getProperties().get("channelIndex") // 返回当前渠道的名字 return channels[channelIndex.toInteger()] } android.applicationVariants.all { variant -> variant.outputs.all { // 修改 APK 文件名,添加渠道信息 outputFileName = "${variant.name}-${variant.versionName}-${getChannelName()}.apk" } } ``` 4. 执行以下命令生成不同渠道的 APK 文件: ``` ./gradlew assembleGooglePlay -PchannelIndex=0 ./gradlew assembleXiaomi -PchannelIndex=1 ./gradlew assembleHuawei -PchannelIndex=2 ``` 上述命令中的 assembleGooglePlay、assembleXiaomi、assembleHuawei 分别对应不同的 productFlavors,-PchannelIndex 参数用于指定当前渠道的索引。执行完这些命令后,会在 app/build/outputs/apk 目录下生成三个不同渠道的 APK 文件。 这是一种较为简单的多渠道打包方式,具体实现方式可能会因项目需求而有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值