介绍多版本apk安装在同一手机上和集成第三方sdk需要提供多版本apk的情况。
一.多版本apk安装在同一个手机上
重点:提供不同包名的apk,只要包名不同,签名信息相同也是可以安装在同一台手机上的。
1.设置productFlavors中的属性
在设置productFlavors时直接进行applicationId的设置,实现applicationId的不同
flavorDimensions "default"
productFlavors{
xiaomi{
applicationId "com.example.zhangqianqian.packagetest_3.xiaomi"
manifestPlaceholders=[UMENG_CHANNEL_VALUE:"xiaomi",app_name:"packagetest_3.xiaomi",icon:"@drawable/one"]
//在Java代码中使用,具体使用方法为:context.getResource().getString(R.string.strKey)
resValue("string","strKey","xiaomiStrValue")
}
qh360{
applicationId "com.example.zhangqianqian.packagetest_3.qh360"
manifestPlaceholders=[UMENG_CHANNEL_VALUE:"qh360",app_name:"packagetest_3.qh360",icon:"@drawable/two"]
resValue("string","strKey","qh360StrValue")
}
baidu{
applicationId "com.example.zhangqianqian.packagetest_3.baidu"
manifestPlaceholders=[UMENG_CHANNEL_VALUE:"baidu",app_name:"packagetest_3.baidu",icon:"@drawable/three"]
resValue("string","strKey","baiduStrValue")
}
}
application实现重新配置这个属性,这个属性可以使打包出来的apk包名产生对应的变化。
只要我们重写了applicationId这个属性就会覆盖defaultConfig中相对应属性的信息,从而使打包出来的两种apk的包名不一样,达到在同一台手机上安装的目的。
为了在java代码中可以方便获取到gradle配置文件的数据,我们可以通过以下在gradle文件中配置,在java 代码中使用方式为:context.getResources().getString(R.string.strKey);
如果想更改程序icon和appName,在productFlavors的每个flavor中通过manifestPlaceholders属性配置即可,manifestPlaceholders是一个类似HashMap的容器,因此在manifestPlaceholders可以配置多个属性,以便在AndroidManifest.xm中使用,比如我们需要为每种版本的apk替换特定的icon和appName这时我们可以这样配置:
manifestPlaceholders=[UMENG_CHANNEL_VALUE:"xiaomi",app_name:"packagetest_3.xiaomi",icon:"@drawable/one"]
manifestPlaceholders=[UMENG_CHANNEL_VALUE:"qh360",app_name:"packagetest_3.qh360",icon:"@drawable/two"]
manifestPlaceholders=[UMENG_CHANNEL_VALUE:"baidu",app_name:"packagetest_3.baidu",icon:"@drawable/three"]
同时需要在AndroidManifest.xml中进行一定的修改:
activity标签下添加:
application标签下修改:
android:icon="${icon}"
android:label="${app_name}"
注:
build.gradle的完全代码:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.zhangqianqian.packagetest_3"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs{
releaseConfig{
storeFile file(KEY_PATH)
storePassword KEY_PASS
keyAlias ALIAS_NAME
keyPassword ALIAS_PASS
}
debugConfig{
storeFile file(KEY_PATH)
storePassword KEY_PASS
keyAlias ALIAS_NAME
keyPassword ALIAS_PASS
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.releaseConfig
}
debug{
signingConfig signingConfigs.debugConfig
}
}
flavorDimensions "default"
productFlavors{
xiaomi{
applicationId "com.example.zhangqianqian.packagetest_3.xiaomi"
manifestPlaceholders=[UMENG_CHANNEL_VALUE:"xiaomi",app_name:"packagetest_3.xiaomi",icon:"@drawable/one"]
//在Java代码中使用,具体使用方法为:context.getResource().getString(R.string.strKey)
resValue("string","strKey","xiaomiStrValue")
}
qh360{
applicationId "com.example.zhangqianqian.packagetest_3.qh360"
manifestPlaceholders=[UMENG_CHANNEL_VALUE:"qh360",app_name:"packagetest_3.qh360",icon:"@drawable/two"]
resValue("string","strKey","qh360StrValue")
}
baidu{
applicationId "com.example.zhangqianqian.packagetest_3.baidu"
manifestPlaceholders=[UMENG_CHANNEL_VALUE:"baidu",app_name:"packagetest_3.baidu",icon:"@drawable/three"]
resValue("string","strKey","baiduStrValue")
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
AndroidManifest.xml完全代码:
注意:
修改applicationId影响:
applicationVariants.all { variant ->
variant.outputs.all { output ->
def outputFile = output.outputFile
def date = new Date().format("yyyyMMddHHmm", TimeZone.getTimeZone("GMT+08"))
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "测试打包_"+buildType.name +
"_${variant.productFlavors[0].name}_" +
"V${defaultConfig.versionName}_${date}.apk"
//格式:测试打包_debug_baidu_V1.0_201803021541.apk
// 测试打包_release_baidu_V1.0_201803021541.apk
// output.outputFile = new File(outputFile.parent, fileName)
outputFileName = fileName
}
}
}
build.gradle完全代码:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.zhangqianqian.packagetest_4"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs{
releaseConfig{
storeFile file(KEY_PATH)
storePassword KEY_PASS
keyAlias ALIAS_NAME
keyPassword ALIAS_PASS
}
debugConfig{
storeFile file(KEY_PATH)
storePassword KEY_PASS
keyAlias ALIAS_NAME
keyPassword ALIAS_PASS
}
}
applicationVariants.all { variant ->
variant.outputs.all { output ->
def outputFile = output.outputFile
def date = new Date().format("yyyyMMddHHmm", TimeZone.getTimeZone("GMT+08"))
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "测试打包_"+buildType.name +
"_${variant.productFlavors[0].name}_" +
"V${defaultConfig.versionName}_${date}.apk"
//格式:测试打包_debug_baidu_V1.0_201803021541.apk
// 测试打包_release_baidu_V1.0_201803021541.apk
// output.outputFile = new File(outputFile.parent, fileName)
outputFileName = fileName
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.releaseConfig
}
debug{
signingConfig signingConfigs.debugConfig
}
}
flavorDimensions "default"
productFlavors{
xiaomi{
manifestPlaceholders=[UMENG_CHANNEL_VALUE:"xiaomi"]
}
qh360{
manifestPlaceholders=[UMENG_CHANNEL_VALUE:"qh360"]
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
3.多平台多版本apk
1)首先要配置NDK
productFlavors{
xiaomi{
manifestPlaceholders=[UMENG_CHANNEL_VALUE:"xiaomi"]
ndk{
abiFilters "arm64-v8a" , "armeabi" , "armeabi-v7a"
}
}
qh360{
manifestPlaceholders=[UMENG_CHANNEL_VALUE:"qh360"]
ndk{
abiFilters "x86" , "x86_64"
}
}
}
build.gralde的完整代码:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.zhangqianqian.face_project"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk {
//设置支持的SO库架构(开发者可以根据需要,选择一个或多个平台的so)
abiFilters "armeabi", "armeabi-v7a","x86","x86_64","armeabi-v8a"
}
}
signingConfigs{
releaseConfig{
storeFile file(KEY_PATH)
storePassword KEY_PASS
keyAlias ALIAS_NAME
keyPassword ALIAS_PASS
}
debugConfig{
storeFile file(KEY_PATH)
storePassword KEY_PASS
keyAlias ALIAS_NAME
keyPassword ALIAS_PASS
}
}
applicationVariants.all { variant ->
variant.outputs.all { output ->
def outputFile = output.outputFile
def date = new Date().format("yyyyMMddHHmm", TimeZone.getTimeZone("GMT+08"))
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "测试打包_"+buildType.name +
"_${variant.productFlavors[0].name}_" +
"V${defaultConfig.versionName}_${date}.apk"
//格式:测试打包_debug_baidu_V1.0_201803021541.apk
// 测试打包_release_baidu_V1.0_201803021541.apk
// output.outputFile = new File(outputFile.parent, fileName)
outputFileName = fileName
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.releaseConfig
}
debug{
signingConfig signingConfigs.debugConfig
}
}
flavorDimensions "default"
productFlavors{
xiaomi{
manifestPlaceholders=[UMENG_CHANNEL_VALUE:"xiaomi"]
ndk{
abiFilters "arm64-v8a" , "armeabi" , "armeabi-v7a"
}
}
qh360{
manifestPlaceholders=[UMENG_CHANNEL_VALUE:"qh360"]
ndk{
abiFilters "x86" , "x86_64"
}
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
//卡片样式
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
//圆形图片
implementation 'de.hdodenhof:circleimageview:2.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
//图片加载
implementation 'com.github.bumptech.glide:glide:3.7.0'
implementation 'com.android.support:support-v4:28.0.0'
//虹软相关
implementation 'com.guo.android_extend:android-extend:1.0.6'
//虹软jar包
implementation files('libs/facedetection.jar')
implementation files('libs/facerecognition.jar')
implementation files('libs/facetracking.jar')
implementation files('libs/ageestimation.jar')
implementation files('libs/genderestimation.jar')
//轮播依赖
implementation 'com.youth.banner:banner:1.4.10'
//网络框架
// Okhttp库
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
// Retrofit库
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
//GSON解析库
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}