何為馬甲包?
馬甲包是指與原APP包除了包名藐吮,簽名溺拱、包名稱(chēng)圖標(biāo)等給用戶(hù)加以區(qū)分的東西不一樣之外,其他功能基本不變的APP包谣辞。
最近公司需要一套代碼迫摔,生成多個(gè)馬甲包的需求,為了方便代碼維護(hù)和打包方便泥从,主要通過(guò)gradle工具配置不同包名句占,不同簽名,不同資源名躯嫉,不同馬甲包部分差異化纱烘,不同兼容包名不同的差異化兼容需求(如:微信分享跟包名有關(guān))。用Terminal命令或者Tasks一鍵生成多個(gè)包祈餐。
下面就描述下只需配置擂啥,就可以一鍵生成多個(gè)馬甲包和主包(當(dāng)成一個(gè)馬甲包就行)?
????1.簽名文件路徑配置(只有一個(gè)簽名文件帆阳,不同馬甲包對(duì)應(yīng)不同別名就行)
????2.主module的build.gradle中一些相關(guān)配置
????3.AndroidManifest.xml中的一些相關(guān)配置(${}的使用)
????4.獲取MetaData值和getPackageName()獲取包名
????5.如何打包
1.簽名文件路徑配置(只有一個(gè)簽名文件哺壶,不同馬甲包對(duì)應(yīng)不同別名就行)
這一步主要是每個(gè)人電腦簽名文件位置不一樣,我把地址配置放在這里。放在其他地方也行变骡。
signingStoreFilePath = "E:/raythinks/keystore.jks" //font color=red>簽名文件目錄
ext {
signingStoreFilePath = "E:/raythinks/keystore.jks" //font color=red>簽名文件目錄
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
2.主module的build.gradle中一些相關(guān)配置
(1)applicationId 馬甲包包名配置离赫。如: applicationId "com.raythins.herri.xinshou"http://(如:新手版)
(2)signingConfigs 簽名文件配置 。每個(gè)馬甲包簽名文件或者別名不一樣塌碌。
(3)productFlavors 配置要生成的馬甲包渊胸。 如:majia_xinshou_vivo 新手版;majia_shop_vivo 商城版
(4)manifestPlaceholders 資源配置(如:馬甲包app的logo台妆、名稱(chēng)翎猛、微信appkey等。)
(5) signingConfig 配置馬甲包編譯時(shí)使用的簽名接剩。如: signingConfig signingConfigs.shop 使用商城版簽名
詳情見(jiàn)gradle.gradle代碼
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '25.0.2'
defaultConfig {
applicationId "com.raythins.herri"http://默認(rèn)包名
minSdkVersion 15 //最小版本號(hào)
targetSdkVersion 23
versionCode 110//版本code
versionName "1.1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk { abiFilters 'armeabi-v7a' 切厘,'armeabi'}//指定ndk,目前市場(chǎng)上手機(jī)基本覆蓋兼容這兩種
multiDexEnabled true//MultiDex的配置
manifestPlaceholders = [
JPUSH_PKGNAME : "com.raythins.herri",//極光包名
JPUSH_APPKEY : "sdfewds234324343243243243sdfdsd",//極光appkey
JPUSH_CHANNEL : "developer-default", //暫時(shí)填寫(xiě)默認(rèn)值即可.
BAIDU_APPKEY : "SDFDSFDFDSFRT72LSDFDSFDFDS",//
TENCENT_ID : "1323123134341" , //騰訊id(應(yīng)用寶)
TENCENT_APPKEY : "SDFDSFDS9089SDF" , //騰訊APPKEY
LAUNCHER_ICON : "@drawable/ic_launcher" , //logo圖片路徑
WCHAT_APPID : "wxsdf4eds323r32432432432",// 微信appid
WCHAT_TEMPLETE_ID : "sdfdsfsdfsdf233243243243243243243243243",//微信SDK 訂閱id
WCHAT_SECRET : "dfdfdsffdsfdsfdsfdsfds3432432432432432432432",//微信SDK secret
]
}
//debug和release版本的簽名配置
signingConfigs {
xinshou{//新手版簽名文件信息
storeFile file(rootProject.ext.signingStoreFilePath)
storePassword "123456"
keyAlias "別名1"
keyPassword "123456"
v1SigningEnabled true
v2SigningEnabled true
}
shop{//商城版簽名文件信息
storeFile file(rootProject.ext.signingStoreFilePath)
storePassword "123456"
keyAlias "別名2"
keyPassword "123456"
v1SigningEnabled true
v2SigningEnabled true
}
}
buildTypes {
release {
minifyEnabled true
//Zipalign優(yōu)化
zipAlignEnabled true
// 移除無(wú)用的resource文件
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
majia_xinshou_vivo{//新手版)
applicationId "com.raythins.herri.xinshou"http://(如:新手版)
manifestPlaceholders = [
JPUSH_PKGNAME : "com.raythins.herri.xinshou",//極光包名
JPUSH_APPKEY : "sdfewds234324343243243243sdfdsd",//極光appkey
JPUSH_CHANNEL : "developer-default", //暫時(shí)填寫(xiě)默認(rèn)值即可.
BAIDU_APPKEY : "SDFDSFDFDSFRT72LSDFDSFDFDS",//
TENCENT_ID : "1323123134341" , //騰訊id(應(yīng)用寶)
TENCENT_APPKEY : "SDFDSFDS9089SDF" , //騰訊APPKEY
LAUNCHER_ICON : "@drawable/ic_launcher_xinshou" , //logo圖片路徑
APP_NAME : "Demo新手版" , //app名稱(chēng)
WCHAT_APPID : "wxsdf4eds323r32432432432",// 微信appid
WCHAT_TEMPLETE_ID : "sdfdsfsdfsdf233243243243243243243243243",//微信SDK 訂閱id
WCHAT_SECRET : "dfdfdsffdsfdsfdsfdsfds3432432432432432432432",//微信SDK secret
]
signingConfig signingConfigs.xinshou//簽名信息
}
majia_shop_vivo{//商城版)
applicationId "com.raythins.herri.shop"http://(如:商城版)
manifestPlaceholders = [
JPUSH_PKGNAME : "com.raythins.herri.xinshou",//極光包名
JPUSH_APPKEY : "sdfewds234324343243243243sdfdsd",//極光appkey
JPUSH_CHANNEL : "developer-default", //暫時(shí)填寫(xiě)默認(rèn)值即可.
BAIDU_APPKEY : "SDFDSFDFDSFRT72LSDFDSFDFDS",//
TENCENT_ID : "1323123134341" , //騰訊id(應(yīng)用寶)
TENCENT_APPKEY : "SDFDSFDS9089SDF" , //騰訊APPKEY
LAUNCHER_ICON : "@drawable/ic_launcher_shop" , //logo圖片路徑
APP_NAME : "Demo商城版", //app名稱(chēng)
WCHAT_APPID : "wxsdf4eds323r32432432432",// 微信appid
WCHAT_TEMPLETE_ID : "sdfdsfsdfsdf233243243243243243243243243",//微信SDK 訂閱id
WCHAT_SECRET : "dfdfdsffdsfdsfdsfdsfds3432432432432432432432",//微信SDK secret
]
signingConfig signingConfigs.shop//簽名信息
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
repositories {
mavenCentral()
repositories {
flatDir {
dirs 'libs'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
//自帶的類(lèi)庫(kù)v4以及v7
compile 'com.android.support:appcompat-v7:23.0.1'
}
3.AndroidManifest.xml中的一些相關(guān)配置(${}的使用)
主要是通過(guò)${}將主module的build.gradle中一些相關(guān)配置映射到manifest中懊缺。打包自動(dòng)填充進(jìn)去疫稿。如:${applicationId}、${LAUNCHER_ICON}鹃两、${APP_NAME}遗座、${BAIDU_APPKEY}等。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.raythinks.herri">
<!-- Required -->
<permission
android:name="${applicationId}.permission.JPUSH_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:name=".base.CustomApplication"
android:allowBackup="true"
android:icon="${LAUNCHER_ICON}"
android:label="${APP_NAME}"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="android:allowBackup">
<!-- //百度appkey -->
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="${BAIDU_APPKEY}" />
<!-- //微信appid-->
<meta-data
android:name="WCHAT_APPID"
android:value="${WCHAT_APPID}" />
<!-- //微信secret-->
<meta-data
android:name="WCHAT_SECRET"
android:value="${WCHAT_SECRET}" />
<!-- //微信secret-->
<meta-data
android:name="WCHAT_TEMPLETE_ID"
android:value="${WCHAT_TEMPLETE_ID}" />
<!-- //騰訊ID-->
<meta-data
android:name="TENCENT_ID"
android:value="${TENCENT_ID}" />
<!-- //騰訊appkey-->
<meta-data
android:name="TENCENT_APPKEY"
android:value="${TENCENT_APPKEY}" />
<!-- 友盟集成 -->
<meta-data
android:name="UMENG_APPKEY"
android:value="586481eb82b63522b30005a9" />
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}" />
<!-- 目前這個(gè)渠道統(tǒng)計(jì)功能的報(bào)表還未開(kāi)放俊扳。 -->
<meta-data
android:name="JPUSH_CHANNEL"
android:value="${JPUSH_CHANNEL}" />
<!-- Required. AppKey copied from Portal -->
<meta-data
android:name="JPUSH_APPKEY"
android:value="${JPUSH_APPKEY}" />
<!-- ShareSDK -->
<activity
android:name="com.mob.tools.MobUIShell"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:windowSoftInputMode="stateHidden|adjustResize">
<!-- QQ和QQ空間分享 QQ登錄的回調(diào)必須要配置的 -->
<intent-filter>
<data android:scheme="tencent${TENCENT_ID}" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!-- 新浪微博客戶(hù)端分享回調(diào)必須配置 -->
<intent-filter>
<action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- 微信分享回調(diào) -->
<activity
android:name="${applicationId}.wxapi.WXEntryActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:exported="true"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<activity
android:name=".ui.activity.MainActivity"
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTask"
android:screenOrientation="portrait" />
<!-- 極光推送 Required SDK 核心功能 -->
<!-- 可配置android:process參數(shù)將PushService放在其他進(jìn)程中 -->
<service
android:name="cn.jpush.android.service.PushService"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTER" />
<action android:name="cn.jpush.android.intent.REPORT" />
<action android:name="cn.jpush.android.intent.PushService" />
<action android:name="cn.jpush.android.intent.PUSH_TIME" />
</intent-filter>
</service>
<!-- since 1.8.0 option 可選項(xiàng)途蒋。用于同一設(shè)備中不同應(yīng)用的JPush服務(wù)相互拉起的功能。 -->
<!-- 若不啟用該功能可刪除該組件馋记,將不拉起其他應(yīng)用也不能被其他應(yīng)用拉起 -->
<service
android:name="cn.jpush.android.service.DaemonService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="cn.jpush.android.intent.DaemonService" />
<category android:name="${applicationId}" />
</intent-filter>
</service>
<!-- Required SDK核心功能 -->
<service
android:name="cn.jpush.android.service.DownloadService"
android:enabled="true"
android:exported="false" />
<!-- Required SDK核心功能 -->
<receiver
android:name="cn.jpush.android.service.PushReceiver"
android:enabled="true">
<intent-filter android:priority="1000">
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />
<category android:name="${applicationId}" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
<!-- Optional -->
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<!-- Required SDK核心功能 -->
<receiver android:name="cn.jpush.android.service.AlarmReceiver" />
<!-- Required since 3.0.7 -->
<!-- 新的tag/alias接口結(jié)果返回需要開(kāi)發(fā)者配置一個(gè)自定的廣播 -->
<!-- 該廣播需要繼承JPush提供的JPushMessageReceiver類(lèi), 并如下新增一個(gè) Intent-Filter -->
<receiver
android:name=".receiver.MyJPushMessageReceiver"
android:enabled="true">
<intent-filter>
<action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<!-- 極光Required SDK核心功能 -->
<activity
android:name="cn.jpush.android.ui.PushActivity"
android:configChanges="orientation|keyboardHidden"
android:exported="false"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="cn.jpush.android.ui.PushActivity" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="${applicationId}" />
</intent-filter>
</activity>
<!-- SDK核心功能 -->
<activity
android:name="cn.jpush.android.ui.PopWinActivity"
android:configChanges="orientation|keyboardHidden"
android:exported="false"
android:theme="@style/MyDialogStyle">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="${applicationId}" />
</intent-filter>
</activity>
<receiver
android:name="com.tendcloud.appcpa.ReferralReceiver"
android:exported="true"
tools:ignore="ExportedReceiver">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
</application>
</manifest>
4.獲取MetaData值和getPackageName()獲取包名
public static <T> T getMetaData(Context context, String name) {
try {
final ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA);
if (ai.metaData != null) {
return (T) ai.metaData.get(name);
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
如:AppUtil.getMetaData(activity, "WCHAT_APPID")
5.如何打包
通過(guò)命令方式号坡、Tasks、Generate signed APK打包生成多個(gè)馬甲包梯醒。
(1)Terminal命令方式
gradle assembleRelease // 構(gòu)建productFlavors下所有Variant Release版本
gradle assembleDebug // 構(gòu)建productFlavors下所有Variant Debug版本
gradle assemble[productFlavors.name] // 構(gòu)建productFlavors下name的Variant Release和Debug版本
gradle assemble[productFlavors.name]Relase // 構(gòu)建productFlavors下name的Variant Releaseg版本
gradle assemble[productFlavors.name]Debug // 構(gòu)建productFlavors下name的Variant Debug版本