版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28261207/article/details/82783283
Android同一套代码打多个APP包并能够在同一个手机上安装运行
Android同一套代码打多个APP包并能够在同一个手机上安装运行,同时APP名称、桌面icon图标也都不同
需要能够在同一个手机上运行:只有修改的包名不同,才能使同一套代码在同一个手机上运行。
给同一套代码起不同的多个包名,并把APP名称和桌面图标设置为根据每个APP判断显示哪个的
app下的build.gradle中:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.xxx.papplicationId1"
minSdkVersion 14
targetSdkVersion 27
versionCode 1
versionName '1.0.0'
// -------------------------- 注意这里 ! ! ! --------------------------
// 版本名后面添加一句话,意思就是flavor dimension 它的维度就是该版本号,这样维度就是都是统一的了
flavorDimensions "versionCode"
// -----------------------------------------------------------------------
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
buildToolsVersion '27.0.3'
// -------------------------- 注意这里 ! ! ! --------------------------
productFlavors {
// 通用app
app1 {
// 设置applicationId(这里很重要,两个相同applicationId的apk不同同时安装在同一台Android手机中)
applicationId "com.xxx.applicationId1"
// 自动生成@string/app_name为 app1的名称
// 注意,这里是添加,在 string.xml 不能有这个字段,会重名!!!
resValue "string", "app_name", "app111"
// 定义app_icon等字段,在AndroidManifest.xml文件中用到
manifestPlaceholders = [app_icon : "@mipmap/ic_launcher",
app_roundicon: "@mipmap/ic_launcher",
// 因为每个APP包名不同,所以需要单独配置每个APP的第三方key
// 修改 AndroidManifest.xml 里 地图appkey
map_appkey : "xxxxxxxxxxxxxx"]
}
// app2
app2 {
applicationId "com.xxx.applicationId2"
resValue "string", "app_name", "app222"
manifestPlaceholders = [app_icon : "@mipmap/ic_launcher2",
app_roundicon: "@mipmap/ic_launcher2",
map_appkey : "xxxxxxxxxxxxx"]
}
}
// -----------------------------------------------------------------------
// 移除lint检测的error
lintOptions {
checkReleaseBuilds false
abortOnError false
}
sourceSets.main {
jniLibs.srcDirs = ['libs']
}
}
dependencies {
......
}
allprojects {
repositories {
flatDir {
dirs 'libs'
}
}
}
使用
AndroidManifest.xml中 :
<application
android:name=".xxxxx"
android:allowBackup="true"
android:icon="${app_icon}"
android:label="@string/app_name"
android:roundIcon="${app_roundicon}"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="android:allowBackup">
<!--tools:replace="icon,label,theme">-->
<!-- 地图appkey, 使用build.gradle中动态的值 -->
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="${map_appkey}"/>