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

相关推荐
檀越剑指大厂2 小时前
容器化 Android 开发效率:cpolar 内网穿透服务优化远程协作流程
android
MiyamuraMiyako3 小时前
从 0 到发布:Gradle 插件双平台(MavenCentral + Plugin Portal)发布记录与避坑
android
NRatel3 小时前
Unity 游戏提升 Android TargetVersion 相关记录
android·游戏·unity·提升版本
叽哥6 小时前
Kotlin学习第 1 课:Kotlin 入门准备:搭建学习环境与认知基础
android·java·kotlin
风往哪边走6 小时前
创建自定义语音录制View
android·前端
用户2018792831676 小时前
事件分发之“官僚主义”?或“绕圈”的艺术
android
用户2018792831676 小时前
Android事件分发为何喜欢“兜圈子”?不做个“敞亮人”!
android
Kapaseker8 小时前
你一定会喜欢的 Compose 形变动画
android
QuZhengRong8 小时前
【数据库】Navicat 导入 Excel 数据乱码问题的解决方法
android·数据库·excel
zhangphil9 小时前
Android Coil3视频封面抽取封面帧存Disk缓存,Kotlin(2)
android·kotlin