作用:实现应用自动更新提示
配置app/build.gradle
版本compileSdkVersion 28
minSdkVersion 15
android {
...
ndk {
//设置支持的SO库架构
abiFilters 'armeabi' //, 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
}
}
dependencies {
//注释掉原有bugly的仓库
//compile 'com.tencent.bugly:crashreport:latest.release'//其中latest.release指代最新版本号,也可以指定明确的版本号,例如1.3.4
/* 使用 implementation'com.tencent.bugly:crashreport:latest.release'会出现
<activity
android:name="com.tencent.bugly.beta.ui.BetaActivity"这里出现错误
android:configChanges="keyboardHidden|orientation|screenSize|locale"
android:theme="@android:style/Theme.Translucent" />*/
//在这里我使用1.3.6
implementation 'com.tencent.bugly:crashreport_upgrade:1.3.6'
implementation 'com.tencent.bugly:nativecrashreport:latest.release'
}
AndroidMainfest.xml中进行以下配置:
- 权限配置
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission
android:name="android.permission.READ_LOGS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
- Activity配置,在标签里添加:
<!--注册Bugly Activity组件-->
<activity
android:name="com.tencent.bugly.beta.ui.BetaActivity"
android:configChanges="keyboardHidden|orientation|screenSize|locale"
android:theme="@android:style/Theme.Translucent" />
- 配置FileProvider。
注意:如果您想兼容Android N或者以上的设备,必须要在AndroidManifest.xml文件中配置FileProvider来访问共享路径的文件。
- app/build.gradle下载:
dependencies {
/*配置FileProvider*/
implementation 'com.android.support:support-v4:28.0.0'
}
- 在AndroidManifest.xml文件下,标签里
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
如果你使用的第三方库也配置了同样的FileProvider, 可以通过继承FileProvider类来解决合并冲突的问题,示例如下:
<provider
android:name=".utils.BuglyFileProvider"
android:authorities="${applicationId}.fileProvider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="name,authorities,exported,grantUriPermissions">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"
tools:replace="name,resource"/>
</provider>
- 在res目录新建xml文件夹,创建provider_paths.xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!--Bugly:这里配置的两个外部存储路径是升级SDK下载的文件可能存在的路径,一定要按照上面格式配置,不然可能会出现错误。-->
<!-- /storage/emulated/0/Download/${applicationId}/.beta/apk-->
<external-path
name="beta_external_path"
path="Download/" />
<!--/storage/emulated/0/Android/data/${applicationId}/files/apk/-->
<external-path
name="beta_external_files_path"
path="Android/data/" />
<root-path
name="root"
path="root/" />
<!--代表设备的根目录new File("/");-->
<files-path
name="my_image"
path="images/" />
<!--代表context.getFilesDir()-->
<cache-path
name="cache"
path="caches/" />
<!--代表context.getCacheDir()-->
<external-path
name="my_dir"
path="dirs/" />
<!--代表Environment.getExternalStorageDirectory()-->
<!--<external-files-path/>-->
<!--代表context.getExternalFilesDirs()-->
<!--<external-cache-path/>-->
<!--代表getExternalCacheDirs()-->
</paths>
- 混淆配置,在app/proguard-rules.pro文件中:
#添加混淆规则
-dontwarn com.tencent.bugly.**
-keep public class com.tencent.bugly.**{*;}
-keep class android.support.**{*;}
测试验证
- SDK初始化
//统一初始化方法:
//Bugly.init(getApplicationContext(), "注册时申请的APPID", false);
Bugly.init(getApplicationContext(), "59a5100f3e", false);
- 参数解析:
参数1:上下文对象
参数2:注册时申请的APPID
参数3:是否开启debug模式,true表示打开debug模式,false表示关闭调试模式 - 提示:已经接入Bugly用户改用上面的初始化方法,不影响原有的crash上报功能;
init方法会自动检测更新,不需要再手动调用Beta.checkUpgrade(), 如需增加自动检查时机可以使用Beta.checkUpgrade(false,false);
参数1:isManual 用户手动点击检查,非用户点击操作请传false
参数2:isSilence 是否显示弹窗等交互,[true:没有弹窗和toast] [false:有弹窗或toast]
- 将项目打包成apk:
- 创建“我的产品”
- 上面SDK初始化会使用到appID
- 打开应用升级的全量更新界面,发布新升级
在app/build.gradle中的版本改成2.0
versionCode 2
versionName "2.0"
发布新版本
- 测试 :
安装版本1.0的
版本更新为2.0