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

相关推荐
ChinaDragonDreamer1 小时前
Kotlin:2.0.20 的新特性
android·开发语言·kotlin
网络研究院3 小时前
Android 安卓内存安全漏洞数量大幅下降的原因
android·安全·编程·安卓·内存·漏洞·技术
凉亭下3 小时前
android navigation 用法详细使用
android
小比卡丘6 小时前
C语言进阶版第17课—自定义类型:联合和枚举
android·java·c语言
前行的小黑炭7 小时前
一篇搞定Android 实现扫码支付:如何对接海外的第三方支付;项目中的真实经验分享;如何高效对接,高效开发
android
落落落sss8 小时前
MybatisPlus
android·java·开发语言·spring·tomcat·rabbitmq·mybatis
代码敲上天.9 小时前
数据库语句优化
android·数据库·adb
GEEKVIP11 小时前
手机使用技巧:8 个 Android 锁屏移除工具 [解锁 Android]
android·macos·ios·智能手机·电脑·手机·iphone
model200513 小时前
android + tflite 分类APP开发-2
android·分类·tflite
彭于晏68913 小时前
Android广播
android·java·开发语言