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

相关推荐
_祝你今天愉快5 小时前
UI核心 - 绘制流程(测量、布局、绘制)
android
冬奇Lab6 小时前
LLM 驱动的自动化测试系列(07):移动端自动化(三)——Mobile-Agent-v3 与自研 GUI-Owl 模型路线
android·人工智能·测试
leobertlan9 小时前
痛苦系列 | DSP-02 频域切片:DTFT与DFT的探索
android·后端
leobertlan10 小时前
痛苦系列 | DSP-01 从连续到离散:DSP基础与采样
android·后端
事圆则缓12 小时前
Android 视频压缩上传实战:从 content Uri、转码到断点续传
android
冉冉同学15 小时前
AI Agent 开始操作真实手机:移动端自动化的 3 个新考点
android·ai编程
帅得不敢出门16 小时前
Android Framework关闭软件低电关机功能测试PMIC低电保护
android
恋猫de小郭16 小时前
Flutter + EmbeddingGemma 2,谷歌发布完全端侧的 AI Edge Foresight
android·前端·flutter
墨天梦18 小时前
B16_Material主题与可访问交互
android·kotlin·交互
弯_弯18 小时前
企业知识库 RAG 落地:从 Demo 到生产,四条工程链路与参数级自查
android·java·javascript