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

相关推荐
雨白6 小时前
单例设计模式:从 Java/Kotlin 实现到 Android 实战
android
hai_android11 小时前
一行 asLiveData,背后是一座生命周期的桥
android·java·kotlin
阿杰m12 小时前
Android源码第二十三章:Telephony、RIL 与 IMS——来电怎么到应用?
android
booboosui12 小时前
没人认领 .md:安卓把报告交给了音乐播放器打开
android
用户607097591490614 小时前
AOSP 14 OTA 升级指导(四):update_engine 内部机制——Action 流水线与写盘
android
恋猫de小郭18 小时前
Jetpack Compose 1.13 发布,憋了这么久终有交代了
android·前端·flutter
JMchen12318 小时前
【Android 性能优化实战 60 讲】12 异步 Inflate 实战与痛击:源码拆解原生瓶颈,手写增强版布局预加载框架
android·性能优化·实战·启动优化·异步inflate·x2c
好的佩奇1 天前
蓝牙之设备角色与连接
android·flutter·蓝牙
雨白2 天前
Android 消息机制剖析:从 Handler 投递到 Looper 循环的全链路
android
GY-932 天前
【Android】数据分层和依赖注入
android