美文网首页
Android马甲包开发之路

Android马甲包开发之路

作者: 小风小雨 | 来源:发表于2018-03-13 12:00 被阅读0次

何为马甲包?

马甲包是指与原APP包除了包名,签名、包名称图标等给用户加以区分的东西不一样之外,其他功能基本不变的APP包。



最近公司需要一套代码,生成多个马甲包的需求,为了方便代码维护和打包方便,主要通过gradle工具配置不同包名,不同签名,不同资源名,不同马甲包部分差异化,不同兼容包名不同的差异化兼容需求(如:微信分享跟包名有关)。用Terminal命令或者Tasks一键生成多个包。

下面就描述下只需配置,就可以一键生成多个马甲包和主包(当成一个马甲包就行)?

    1.签名文件路径配置(只有一个签名文件,不同马甲包对应不同别名就行)
    2.主module的build.gradle中一些相关配置
    3.AndroidManifest.xml中的一些相关配置(${}的使用)
    4.获取MetaData值和getPackageName()获取包名
    5.如何打包
1.签名文件路径配置(只有一个签名文件,不同马甲包对应不同别名就行)

这一步主要是每个人电脑签名文件位置不一样,我把地址配置放在这里。放在其他地方也行。
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中一些相关配置

(1)applicationId 马甲包包名配置。如: applicationId "com.raythins.herri.xinshou"//(如:新手版)
(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"//默认包名
        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(应用宝)
                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优化
            zipAlignEnabled true
            // 移除无用的resource文件
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    productFlavors {
        majia_xinshou_vivo{//新手版)
            applicationId "com.raythins.herri.xinshou"//(如:新手版)
            manifestPlaceholders = [
                 JPUSH_PKGNAME    : "com.raythins.herri.xinshou",//极光包名
                JPUSH_APPKEY  :  "sdfewds234324343243243243sdfdsd",//极光appkey
                JPUSH_CHANNEL    : "developer-default", //暂时填写默认值即可.
                BAIDU_APPKEY  :  "SDFDSFDFDSFRT72LSDFDSFDFDS",//
                TENCENT_ID  :  "1323123134341" , //腾讯id(应用宝)
                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"//(如:商城版)
            manifestPlaceholders = [
                 JPUSH_PKGNAME    : "com.raythins.herri.xinshou",//极光包名
                JPUSH_APPKEY  :  "sdfewds234324343243243243sdfdsd",//极光appkey
                JPUSH_CHANNEL    : "developer-default", //暂时填写默认值即可.
                BAIDU_APPKEY  :  "SDFDSFDFDSFRT72LSDFDSFDFDS",//
                TENCENT_ID  :  "1323123134341" , //腾讯id(应用宝)
                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中的一些相关配置(${}的使用)

主要是通过${}将主module的build.gradle中一些相关配置映射到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}" />
              <!-- 目前这个渠道统计功能的报表还未开放。 -->
        <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登录的回调必须要配置的 -->
            <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>

            <!-- 新浪微博客户端分享回调必须配置 -->
            <intent-filter>
                <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
       
        <!-- 微信分享回调 -->
        <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参数将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 可选项。用于同一设备中不同应用的JPush服务相互拉起的功能。 -->
        <!-- 若不启用该功能可删除该组件,将不拉起其他应用也不能被其他应用拉起 -->
        <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接口结果返回需要开发者配置一个自定的广播 -->
        <!-- 该广播需要继承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    // 构建productFlavors下所有Variant Release版本
gradle assembleDebug      // 构建productFlavors下所有Variant Debug版本
gradle assemble[productFlavors.name]          // 构建productFlavors下name的Variant Release和Debug版本
gradle assemble[productFlavors.name]Relase    // 构建productFlavors下name的Variant Releaseg版本
gradle assemble[productFlavors.name]Debug     // 构建productFlavors下name的Variant Debug版本
image.png
(2)Tasks或者Generate signed APK方式
如微信回调类配置
image.png

相关文章

网友评论

      本文标题:Android马甲包开发之路

      本文链接:https://www.haomeiwen.com/subject/mkoczxtx.html