Android馬甲包開發(fā)之路

何為馬甲包?

馬甲包是指與原APP包除了包名,簽名、包名稱圖標等給用戶加以區(qū)分的東西不一樣之外,其他功能基本不變的APP包。



最近公司需要一套代碼,生成多個馬甲包的需求,為了方便代碼維護和打包方便,主要通過gradle工具配置不同包名,不同簽名,不同資源名,不同馬甲包部分差異化,不同兼容包名不同的差異化兼容需求(如:微信分享跟包名有關(guān))。用Terminal命令或者Tasks一鍵生成多個包。

下面就描述下只需配置,就可以一鍵生成多個馬甲包和主包(當成一個馬甲包就行)?


????1.簽名文件路徑配置(只有一個簽名文件,不同馬甲包對應(yīng)不同別名就行)
????2.主module的build.gradle中一些相關(guān)配置
????3.AndroidManifest.xml中的一些相關(guān)配置(${}的使用)
????4.獲取MetaData值和getPackageName()獲取包名
????5.如何打包


1.簽名文件路徑配置(只有一個簽名文件,不同馬甲包對應(yīng)不同別名就行)

這一步主要是每個人電腦簽名文件位置不一樣,我把地址配置放在這里。放在其他地方也行。
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 簽名文件配置 。每個馬甲包簽名文件或者別名不一樣。


簽名文件配置.png

(3)productFlavors 配置要生成的馬甲包。 如:majia_xinshou_vivo 新手版;majia_shop_vivo 商城版
(4)manifestPlaceholders 資源配置(如:馬甲包app的logo、名稱、微信appkey等。)


manifestPlaceholders資源配置(.png

(5) signingConfig 配置馬甲包編譯時使用的簽名。如: signingConfig signingConfigs.shop 使用商城版簽名

詳情見gradle.gradle代碼
apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion '25.0.2'
    defaultConfig {
        applicationId "com.raythins.herri"http://默認包名
        minSdkVersion 15   //最小版本號
        targetSdkVersion 23  
        versionCode 110//版本code   
        versionName "1.1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        ndk { abiFilters   'armeabi-v7a' ,'armeabi'}//指定ndk,目前市場上手機基本覆蓋兼容這兩種

        multiDexEnabled true//MultiDex的配置

        manifestPlaceholders = [
                JPUSH_PKGNAME    : "com.raythins.herri",//極光包名
                JPUSH_APPKEY  :  "sdfewds234324343243243243sdfdsd",//極光appkey
                JPUSH_CHANNEL    : "developer-default", //暫時填寫默認值即可.
                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
            // 移除無用的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", //暫時填寫默認值即可.
                BAIDU_APPKEY  :  "SDFDSFDFDSFRT72LSDFDSFDFDS",//
                TENCENT_ID  :  "1323123134341" , //騰訊id(應(yīng)用寶)
                TENCENT_APPKEY  :  "SDFDSFDS9089SDF" , //騰訊APPKEY
                LAUNCHER_ICON  :  "@drawable/ic_launcher_xinshou" ,  //logo圖片路徑
                APP_NAME :  "Demo新手版" ,  //app名稱
                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", //暫時填寫默認值即可.
                BAIDU_APPKEY  :  "SDFDSFDFDSFRT72LSDFDSFDFDS",//
                TENCENT_ID  :  "1323123134341" , //騰訊id(應(yīng)用寶)
                TENCENT_APPKEY  :  "SDFDSFDS9089SDF" , //騰訊APPKEY
                LAUNCHER_ICON  :  "@drawable/ic_launcher_shop" ,  //logo圖片路徑
                APP_NAME :  "Demo商城版",  //app名稱
                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'
    })

    //自帶的類庫v4以及v7
    compile 'com.android.support:appcompat-v7:23.0.1'
}
3.AndroidManifest.xml中的一些相關(guān)配置(${}的使用)

主要是通過${}將主module的build.gradle中一些相關(guān)配置映射到manifest中。打包自動填充進去。如:${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}" />
              <!-- 目前這個渠道統(tǒng)計功能的報表還未開放。 -->
        <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>

            <!-- 新浪微博客戶端分享回調(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放在其他進程中 -->
        <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 可選項。用于同一設(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é)果返回需要開發(fā)者配置一個自定的廣播 -->
        <!-- 該廣播需要繼承JPush提供的JPushMessageReceiver類, 并如下新增一個 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.如何打包

通過命令方式、Tasks、Generate signed APK打包生成多個馬甲包。

(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版本
image.png
(2)Tasks或者Generate signed APK方式
如微信回調(diào)類配置
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.介紹 如果你正在查閱build.gradle文件的所有可選項,請點擊這里進行查閱:DSL參考 1.1新構(gòu)建系統(tǒng)...
    Chuckiefan閱讀 12,370評論 8 72
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,649評論 19 139
  • 最近在項目中遇到需要實現(xiàn) Apk 多渠道、定制化打包, Google 、百度查找了一些資料,成功實現(xiàn)了上述功能,在...
    看一季殘花落幕閱讀 2,645評論 1 8
  • 版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。 最近在項目中遇到需要實現(xiàn) Apk 多渠道、定制化打包, Go...
    牙鍋子閱讀 6,327評論 3 33
  • 這兩天鋪天蓋地的王寶強離婚的消息,熱度甚至都超過了奧運會。無限感慨明星效應(yīng),或者說離婚出軌這種事更貼近我們的生活...
    qiao毛線閱讀 232評論 0 0

友情鏈接更多精彩內(nèi)容