android 打包多个 apk

1、配置 gradle 文件

./app/build.gradle

arduino 复制代码
android {
  defaultConfig {
    flavorDimensions "default"
  }
  // 多应用打包
  productFlavors {
      // 默认
      defaults {
          manifestPlaceholders = [app_name:"默认名称",app_icon:"@mipmap/ic_launcher"]
          applicationId "com.example.xxx.defaults"
          // 设置 resValue 环境变量
          resValue("string","product_value","defaults")
      }

      test1 {
          manifestPlaceholders = [app_name:"名称1",app_icon:"@mipmap/ic_launcher1"]
          applicationId "com.example.xxx.test1"
          resValue("string","product_value","test1");
      }

      test2 {
          manifestPlaceholders = [app_name:"名称2",app_icon:"@mipmap/ic_launcher2"]
          applicationId "com.example.xxx.test2"
          resValue("string","product_value","test2")
      }
  }

  // 打包不检查错误
  lintOptions {
      checkReleaseBuilds false
      abortOnError false
  }
}

2、配置 AndroidManifest.xml

android:authorities 设置为唯一

ini 复制代码
<application
  android:icon="${app_icon}"
  android:label="${app_name}">
  <provider
      android:name="androidx.core.content.FileProvider"
      android:authorities="${applicationId}.fileProvider"
      android:exported="false"
      android:grantUriPermissions="true">
      <meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          android:resource="@xml/file_paths"/>
  </provider>
</application>

3、注释或删除 strings.xml

./app/src/main/res/values/strings.xml

xml 复制代码
<resources>
<!--    <string name="app_name">xxx</string>-->
</resources>

4、导入多个图标

可使用 iconfont 图标下载透明 png 图标,使用 Image Asset 导入图标

选中 res 目录右键 -> New -> Image Asset -> Foreground Layer(配置前景图标) -> Background Layer(配置背景颜色) -> Next

5、在代码中获取版本数据

scss 复制代码
// 获取 resValue
String product_value = getResources().getString(R.string.product_value);

// 判断版本
if(BuildConfig.FLAVOR.equals("defaults")) {
  // ...
}

6、生成 apk

Build -> Generate Signed Bundle or APK -> APK -> 选择多个 Debug - Finish

相关推荐
努力努力再努力wz32 分钟前
【MySQL 进阶系列】拒绝滥用root:从 mysql.user 到权限校验,带你彻底理解用户管理与授权机制!
android·c语言·开发语言·数据结构·数据库·c++·mysql
HaiXCoder1 小时前
AndroidAutoSize 框架原理分析与核心问题
android
fengci.1 小时前
CTF+随机困难题目
android·开发语言·前端·学习·php
Le_ee2 小时前
SWPUCTF 2025 秋季新生赛wp2
android
pengyu3 小时前
【Kotlin 协程修仙录 · 金丹境 · 初阶】 | 并发艺术:async/await 与并发组合的优雅之道
android·kotlin
沐言人生4 小时前
ReactNative 源码分析3——ReactActivity之初始化RN应用
android·react native
YaBingSec4 小时前
网络安全靶场WP:Grafana 任意文件读取漏洞(CVE-2021-43798)
android·笔记·安全·web安全·ssh·grafana
YF02115 小时前
彻底解决Android非SDK接口绕过限制的深度实践
android·google·app
IVEN_5 小时前
Gradle 依赖下载 403 Forbidden 修复:全局镜像配置实战
android·后端
恋猫de小郭5 小时前
Flutter 3.44 发布前夕,官方宣布 SwiftPM 将完全取代 CocoaPods
android·前端·flutter