友盟ionic多渠道自动签名app

时间:2023-03-09 05:20:14
友盟ionic多渠道自动签名app

这几天公司的运营部门要求添加流量统计功能,集成了友盟的插件,中间遇到很多的问题,在这里说明记录一下,为遇到相同问题的朋友们减少麻烦

问题一、插件的使用直接将友盟sdk系在放置到项目根目录下plugins文件夹下,并进行配置,主要需要配置plugins下的两个文件

1.android.json

  在dependent_plugins下添加

  

"Umeng": {
"PACKAGE_NAME": "你的appid"
}

2.fetch.json

  在其末尾添加如下代码

  

    "Umeng": {
"source": {
"type": "local",
"path": "umeng"
},
"is_top_level": true,
"variables": {}
}

问题二、多渠道自动生成且自动签名重命名,参考文档http://www.jianshu.com/p/7236ceca2630

cordova默认使用的是gradle进行打包的,多渠道分发的需要手动生成很多文件,比较麻烦,现在介绍批量生成修改渠道且签名份额被指方法,

1.需要生成签名,详细步骤请看 http://www.cnblogs.com/happen-/p/5991794.html

2.需要先在项目根目录下运行ionic build android --release

3.修改platform/android下的文件

(1)AndroidManifest.xml

在application节点下找到<meta-data android:name="UMENG_CHANNEL" android:value="你设置的值" />

改为<meta-data android:name="UMENG_CHANNEL" android:value="${UMENG_CHANNEL_ID}" />

(2)新建release-signing.properties

并添加内容

keyAlias = ***//证书别名
keyPassword = ***//密码可与证书密码一样
storeFile = tianji.keystore//证书路径
storePassword = tianji//证书密码

(3)新建文件proguard-rules.pro,内容可为空,没有文件生成文件会报错

(4)重要部分,需要配置build.gradle

  1.找到productFlavors节点,修改为如下部分

  

    productFlavors {
     //名字是渠道的名称
baidu { }
google { }
wandoujia { }
xiaomi { }
owen { }
}

  2.找到signingConfigs节点

  修改如下

  

        signingConfigs {
release {
// These must be set or Gradle will complain (even if they are overridden).
keyAlias "**"//可以写成**,会读取release-signing.properties文件
keyPassword "***" //可以写成**
storeFile file('***')//需要一个名字不能是**,证书的路径
storePassword "***"//可以写成**
     } }

  3.找到buildTypes节点,

  替换如下

  

        buildTypes {
release {
zipAlignEnabled true
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}

  4.在android节点内部的最后部分添加如下代码

  

    applicationVariants.all { variant ->
variant.outputs.each { output ->
// 重命名产出的apk文件
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
File outputDirectory = new File(outputFile.parent);
def fileName
if (variant.buildType.name == "release") {
fileName = "${variant.productFlavors[0].name}.apk"
} else {
fileName = "${defaultConfig.versionCode}_}_debug.apk"
}
output.outputFile = new File(outputDirectory, fileName)
} // 删除unaligned apk
if (output.zipAlign != null) {
output.zipAlign.doLast {
output.zipAlign.inputFile.delete()
}
}
}
}

  然后在platform/android下依次运行命令

  gradlew clean

  gradlew assembleRelease

  补充说明:经过几天的实验,确定以上方法存在问题,下边是原因以及解决方法,

  原因:官方给的插件需要自己初始化,

  初始化方法:在run》$ionicPlatform.ready内部添加MobclickAgent.init('你的appid','渠道');

  当然这里不能这么写,因为上边的多渠道分发只是更改xml内部信息,但是不能更改js代码

  只需要该一下插件内部src>android>UMPlugin.java

  在头部添加

  

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

  在execute方法内部的init改为一下代码,这段代码的主要作用是初始化插件,但是appid和渠道是从配置文件内部读取

  

        if (action.equals("init")) {
try{
ApplicationInfo appInfo = mContext.getPackageManager()
.getApplicationInfo(mContext.getPackageName(),
PackageManager.GET_META_DATA);
String appKey=appInfo.metaData.getString("UMENG_APPKEY");
String channelId=appInfo.metaData.getString("UMENG_CHANNEL");
MobclickAgent.startWithConfigure(new UMAnalyticsConfig(mContext, appKey, channelId));
MobclickAgent.setScenarioType(mContext, EScenarioType.E_UM_NORMAL);
MobclickAgent.onResume(mContext);
}catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
// String appKey = args.getString(0);
// String channelId = args.getString(1);
return true;
}

  然后在将调用初始化方法内部的两个参数删掉即可

  

MobclickAgent.init();