最近应用市场审核要求APP在首次打开弹出用户协议或隐私政策,在用户同意政策之前不能对相关SDK进行初始化。以防应用提前收集用户信息,保护用户信息安全。为满足最新应用市场审核需求,项目中集成过老的友盟SDK需要重新集成最新版本。
工具:AndroidStudio 版本:4.0.1+
步骤1:
在app文件下的build.gradle的defalutConfig闭包中做如下配置:
defaultConfig {
applicationId "com.smg.xxx"//包名
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
manifestPlaceholders = [qqappid: "tencent11056021xx"]//QQ申请的appid
//解决新增第三方okhttp中引入的Java8特性
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
在依赖的闭包配置文件中引入远程依赖如下:
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'androidx.appcompat:appcompat:1.2.0'
testImplementation 'junit:junit:4.12'
implementation "com.android.support:appcompat-v7:28.0.0"
implementation "com.android.support:support-v4:28.0.0"
implementation "com.android.support:design:28.0.0"
implementation 'com.umeng.umsdk:common:9.4.4'//必选
implementation 'com.umeng.umsdk:asms:1.4.1'//必选
implementation 'com.umeng.umsdk:link:1.2.0'//集成U-Link,可选,如要统计分享回流次数和分享新增用户指标则必选
implementation 'com.umeng.umsdk:share-core:7.1.7'//分享核心库,必选
implementation 'com.umeng.umsdk:share-board:7.1.7'//分享面板功能,可选
implementation 'com.umeng.umsdk:share-wx:7.1.7'//微信完整版
implementation 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:6.8.0'//微信官方依赖库,必选
implementation 'com.umeng.umsdk:share-qq:7.1.7'//QQ完整版
implementation files('libs/open_sdk_3.5.7.4_r1bc9afe_lite.jar') //QQ官方依赖库,必选
implementation 'com.squareup.okhttp3:okhttp:3.14.9'//QQ官方sdk 3.53及之后版本需要集成okhttp3.x,必选
implementation 'com.umeng.umsdk:share-sina:7.1.7'//新浪微博完整版
implementation 'io.github.sinaweibosdk:core:11.11.1@aar'//新浪微博官方SDK依赖库,必选
}
其中目前QQ分享远程依赖有问题,只能采用本地依赖方式进行,在本地app目录下的库文件夹libs中新增
open_sdk_3.5.7.4_r1bc9afe_lite.jar
该jar包可以在友盟平台下载,本文最后给出最新集成的demo中有。
远程依赖配置基本完成,点击右上角sync同步,这时同步可能会出现下载失败的问题,可以在外层build.gradle文件中添加
maven { url 'https://repo1.maven.org/maven2/'}
maven { url "https://dl.bintray.com/thelasterstar/maven/" }
配置后如下:
buildscript {
repositories {
google()
jcenter()
maven { url 'https://repo1.maven.org/maven2/'}
maven { url "https://dl.bintray.com/thelasterstar/maven/" }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://repo1.maven.org/maven2/'}
maven { url "https://dl.bintray.com/thelasterstar/maven/" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
这时只要环境联网状态下基本上可以同步完成。
步骤2:
AndroidManifest文件中配置权限等配置,基本权限如下:
<queries>
<package android:name="com.tencent.mm" />
<package android:name="com.tencent.mobileqq" />
<package android:name="com.qzone" />
<package android:name="com.sina.weibo" />
</queries>
<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.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
下面是集成分享微信,QQ和微博所需配置:
<activity
android:name=".wxapi.WXEntryActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:exported="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.smgtech.kankanews.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths"/>
</provider>
<!--qq-->
<activity
android:name="com.tencent.tauth.AuthActivity"
android:launchMode="singleTask"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="tencent11056021xx"/>
</intent-filter>
</activity>
<activity
android:name="com.tencent.connect.common.AssistActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="orientation|keyboardHidden|screenSize"
tools:replace="android:configChanges" />
微信分享需要吊起微信,需要在报名文件下新建目录wxapi文件夹,然后新建文件名为
WXEntryActivity并继承WXCallbackActivity,不需要实现任何方法便可。
另外在res文件夹下的xml文件夹下新建filepaths.xml文件,适配FileProvider中图片资源等。文件如下:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path name="opensdk_external" path="Images/tmp"/>
<external-files-path name="umeng_cache" path="umeng_cache/"/>
<root-path name="opensdk_root" path=""/>
<external-files-path name="opensdk_external" path="Images/tmp"/>
<!-- 新浪微博 官方分享SDK 10.10.0共享路径 -->
<external-files-path name="share_files" path="." />
</paths>
步骤3:
基本继承环境搭建完成后,我们在初始化的时候需要把微信,QQ,微博,友盟等SDK配置文件进行初始化,新建一个友盟相关的初始化工具类UmInitConfig,供初始化的时候调用,代码如下:
public class UmInitConfig {
public void UMinit(Context context) {
//初始化组件化基础库, 统计SDK/推送SDK/分享SDK都必须调用此初始化接口
UMConfigure.init(context, "xxe3a5b4e0f55a14b60005xx", "Umeng", UMConfigure.DEVICE_TYPE_PHONE,
"xxe3a5b4e0f55a14b60005xx");
String FileProvider = "com.smgtech.kankanews.fileprovider";
PlatformConfig.setWeixin("wx62222360bd053cxx", "314f05566c1e12b96ce11fbf7ce690xx");
PlatformConfig.setWXFileProvider(FileProvider);
PlatformConfig.setSinaWeibo("27459938xx", "9088b66bfab08cb91115c4899aec43xx", "http://sns.whalecloud.com/sina2/callback");
PlatformConfig.setSinaFileProvider(FileProvider);
PlatformConfig.setQQZone("1105602135", "5zI9UFWYsIswes1W");
PlatformConfig.setQQFileProvider(FileProvider);
PlatformConfig.setSinaWeibo("27459938xx", "9088b66bfab08cb91115c4899aec43xx", "http://sns.whalecloud.com/sina2/callback");
//集成umeng-crash-vx.x.x.aar,则需要关闭原有统计SDK异常捕获功能
MobclickAgent.setCatchUncaughtExceptions(false);
//PushSDK初始化(如使用推送SDK,必须调用此方法)
//initUpush(context);
//统计SDK是否支持采集在子进程中打点的自定义事件,默认不支持
UMConfigure.setProcessEvent(true);//支持多进程打点
MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);
}
}
步骤4:
一般我们初始化操作都是放在Application文件夹中,在应用的Application的onCreate方法中做如下初始化,可以把如下方法封装在一个方法中,一边根据实际业务进行调用:
//设置LOG开关,默认为false
UMConfigure.setLogEnabled(true);
//解决推送消息显示乱码的问题
//PushAgent.setup(this, "59892f08310c9307b6002xxx", "669c30a9584623e70e8cd01b0381dxxx");
//友盟预初始化
UMConfigure.preInit(getApplicationContext(),"57e3a5b4e0f55a14b6000xxx","Umeng");
//UMConfigure.init(this,"61b1bdcce0f9bb492b8b5xxx","umeng",UMConfigure.DEVICE_TYPE_PHONE,"");//58edcfeb310c93091c000xxx 5965ee00734be40b58000xxx
/**
* 打开app首次隐私协议授权,以及sdk初始化,判断逻辑请查看SplashTestActivity
*/
//判断是否同意隐私协议,uminit为1时为已经同意,直接初始化umsdk
if(sharedPreferencesHelper.getSharedPreference("uminit","").equals("1")){
//友盟正式初始化
UmInitConfig umInitConfig=new UmInitConfig();
umInitConfig.UMinit(getApplicationContext());
//QQ官方sdk授权
Tencent.setIsPermissionGranted(true);
}
这里需要注意的是判断隐私协议那块,需要根据自己的实际业务需求来进行初始化。
至此友盟分享集成完成。