由于众所周知的原因,国内上不了Google,从而也就打不开google play了,由此出现了众多的市场,那么打包就成了
一项繁琐的问题,今天我们就来研究一下如何利用友盟进行快捷方便的多渠道打包和统计的功能。
先上效果图:
1.既然使用第三方,那么肯定要添加依赖。
compile 'com.umeng.analytics:analytics:latest.integration'
2.注册友盟,创建工程得到AppKey,这里不在赘述,请参考官网。
https://www.umeng.com/
3.添加appkey、权限、默认渠道号到mainfest中;
权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
appkey和默认渠道:
<meta-data android:value="592fae08ae1bf830d20011c4" android:name="UMENG_APPKEY"/>
<meta-data android:value="${UMENG_CHANNEL_VALUE}" android:name="UMENG_CHANNEL"/>
其中,第一条的value为我的appkey,需要替换为自己的;
4.添加签名证书(如果没有先注册一个),这里先不给代码,下面会贴上build.gradle的完整代码;
5.添加市场;
bulid.gradle的完整代码:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "visahall.com.multipackingdemo"
minSdkVersion 17
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true //突破65535
manifestPlaceholders = [UMENG_CHANNEL_CALUE:"umeng"] //默认为uMeng
}
//添加证书
signingConfigs{
debug{}
release{
storeFile file("my.jks")
storePassword "123456"
keyAlias "my"
keyPassword "123456"
}
}
buildTypes {
release {
minifyEnabled false
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//输出的为渠道包的名字,例如 xiaomi.apk
applicationVariants.all{ variant ->
variant.outputs.each{ output ->
def outFile = output.outputFile
if (outFile != null && outFile.name.endsWith(".apk")){
def fileName = "${variant.productFlavors[0].name}" + ".apk"
output.outputFile = new File(outFile.parent, fileName);
}
}
}
}
}
//渠道
productFlavors {
googleplay {}
huawei {}
xiaomi {}
wandoujia {}
baidu {}
yingyongbao {}
android360 {}
uc {}
umeng {}
meizu{}
//批量配置
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
//添加友盟依赖
compile 'com.umeng.analytics:analytics:latest.integration'
}
6.签名打包
然后就完成了渠道打包,就拿着对应的包去到相应的市场就Ok了。
7.对App的状况进行统计,需要在activity的onResume中添加 MobclickAgent.onResume(this);
onPause中添加MobclickAgent.onPause(this);
8.到此基本结束,Demo中点击事件崩溃是为了测试友盟对于页面崩溃及崩溃信息的统计。