bugly的异常报告,统计与升级
1.首先,自定义Application继承TinkerApplication,添加构造方法,代码如下:
public class MyAppliction extends TinkerApplication {
public MyAppliction() {
super(ShareConstants.TINKER_ENABLE_ALL,"com.lml.hotfix.activity.myhoxfixhello.MyApplicationLike",
"com.tencent.tinker.loader.TinkerLoader", false);
}
}
super中的第二个参数为Application的代理类,在MyApplication中不进行处理,把处理的操作都放入到MyApplicationLike中。
2.实现MyApplicationLike类,继承DefaultApplicationLike类,把原先对Application的配置都放入此类中代码如下:
public class MyApplicationLike extends DefaultApplicationLike {
public MyApplication Like(Application application, int tinkerFlags,
boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime,
long applicationStartMillisTime,Intent tinkerResultIntent) {
super(application,tinkerFlags,tinkerLoadVerifyFlag,applicationStartElapsedTime,applicationStartMillisTime,tinkerResultIntent);
}
@Override
public void onCreate() {
super.onCreate();
//这里实现SDK初始化,appId替换成你的在Bugly平台申请的appId
//调试时,将第三个参数改为true
CrashReport.UserStrategy strategy =newCrashReport.UserStrategy(getApplication());
strategy.setAppChannel("ceshi");//打包后使用的渠道号
strategy.setAppVersion("4.0");
strategy.setAppPackageName("com.lml.hotfix.activity.myhoxfixhello");
Beta.autoCheckUpgrade=true;
Bugly.init(getApplication(),"填入appid", false,strategy);
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
public void onBaseContextAttached(Context base) {
super.onBaseContextAttached(base);
// you must install multiDex whatever tinker is installed!
MultiDex.install(base);
//安装tinker
// TinkerManager.installTinker(this);替换成下面Bugly提供的方法
Beta.installTinker(this);
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void registerActivityLifecycleCallback(Application.ActivityLifecycleCallbacks callbacks) {
getApplication().registerActivityLifecycleCallbacks(callbacks);
}
}
CrashReport.UserStrategy 类是对版本信息的一些处理,setAppVersion 统计信息的版本号,记得填入Appid,此值为关联到bugly后台。
3.添加Activity测试类。MainActivity代码如下:
public class MainActivity extends AppCompatActivity {
private String text=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((TextView)findViewById(R.id.tv_info)).setText("first:当前版本号versionName为:"+ BuildConfig.VERSION_NAME);
findViewById(R.id.btn_throw).setOnClickListener(newView.OnClickListener() {
@Override
public void onClick(View v) {
//AlertDialog.Builder builder =newAlertDialog.Builder(MainActivity.this);
//builder.setTitle("haha");
//builder.show();
Log.i("1",text.toString());
}
});
}
}
当点击此按钮时,程序会抛出异常。用于测试热更新。
4.在清单文件中添加权限,添加Application和Activity:
不要忘了bugly自带的BetaActivity的注册。
5.在app下的gradle的配置
defaultConfig {
applicationId"com.lml.hotfix.activity.myhoxfixhello"
minSdkVersion15
targetSdkVersion25
versionCode10
versionName"5.0"
testInstrumentationRunner"android.support.test.runner.AndroidJUnitRunner"
ndk {
//设置支持的SO库架构
abiFilters'armeabi'//, 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
}
multiDexEnabledtrue
}
加入ndk支持so包。
dependencies {
compile'com.android.support:multidex:1.0.1'
compile'com.tencent.bugly:crashreport_upgrade:latest.release'
//其中latest.release指代最新版本号,也可以指定明确的版本号,例如1.2.0
compile'com.tencent.bugly:nativecrashreport:latest.release'
//其中latest.release指代最新版本号,也可以指定明确的版本号,例如2.2.
}
在dependencies中添加上面依赖。
6.新建tinker-support.gradle类,代码如下:
apply plugin:'com.tencent.bugly.tinker-support'
def bakPath = file("${buildDir}/bakApk/")
/**
*此处填写每次构建生成的基准包目录
*/
def baseApkDir ="123456"
/**
*对于插件各参数的详细解析请参考
*/
tinkerSupport {
//开启tinker-support插件,默认值true
enable =true
//指定归档目录,默认值当前module的子目录tinker
autoBackupApkDir ="${bakPath}"
//是否启用覆盖tinkerPatch配置功能,默认值false
//开启后tinkerPatch配置不生效,即无需添加tinkerPatch
overrideTinkerPatchConfiguration =true
//编译补丁包时,必需指定基线版本的apk,默认值为空
//如果为空,则表示不是进行补丁包的编译
// @{link tinkerPatch.oldApk }
baseApk ="${bakPath}/${baseApkDir}/app-release.apk"
//对应tinker插件applyMapping
baseApkProguardMapping ="${bakPath}/${baseApkDir}/app-release-mapping.txt"
//对应tinker插件applyResourceMapping
baseApkResourceMapping ="${bakPath}/${baseApkDir}/app-release-R.txt"
tinkerId ="4.0.0-base"
// buildAllFlavorsDir = "${bakPath}/${baseApkDir}"
enableProxyApplication =false
}
/**
*一般来说,我们无需对下面的参数做任何的修改
*对于各参数的详细介绍请参考:
* https://github.com/Tencent/tinker/wiki/Tinker-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97
*/
tinkerPatch {
//oldApk ="${bakPath}/${appName}/app-release.apk"
ignoreWarning =false
useSign =true
dex {
dexMode ="jar"
pattern = ["classes*.dex"]
loader = []
}
lib {
pattern = ["lib/*/*.so"]
}
res {
pattern = ["res/*","r/*","assets/*","resources.arsc","AndroidManifest.xml"]
ignoreChange = []
largeModSize =100
}
packageConfig {
}
sevenZip {
zipArtifact ="com.tencent.mm:SevenZip:1.1.10"
// path = "/usr/local/bin/7za"
}
buildConfig {
keepDexApply =false
//tinkerId = "1.0.1-patch"
//applyMapping = "${bakPath}/${appName}/app-release-mapping.txt" //可选,设置mapping文件,建议保持旧apk的proguard混淆方式
//applyResourceMapping = "${bakPath}/${appName}/app-release-R.txt" //可选,设置R.txt文件,通过旧apk文件保持ResId的分配
}
}
加粗字体稍后编译有用。在app下的gradle中添加集成tinker-support.gradle。
7.在项目的gradle下添加如下代码:
classpath"com.tencent.bugly:tinker-support:1.0.8"
8.添加混淆规则:
-dontwarncom.tencent.bugly.**
-keeppublic class com.tencent.bugly.**{*;}
-keepclass android.support.**{*;}
9.项目配置完成,现开始生成基准包:
现设置tinkerId ="4.0.0-base",加黑为版本号,自定义。
点击ide右侧的gradle,再点击assembleRelease,生成基准包:
如要使用热更新,需要原先apk集成了bugly的SDK,上面圈处的即是。
可先在手机上安装此Apk。
修改Activity代码:
AlertDialog.Builder builder =newAlertDialog.Builder(MainActivity.this);
builder.setTitle("haha");
builder.show();
// Log.i("1",text.toString());
接着生成补丁包,需要修改两处tinker-support.gradle。
1.def baseApkDir ="123456" 此为生成基准包所在的文件夹名,上面的话,即为“app-0706-17-20-03”
2.tinkerId ="4.0.0-base" 把base修改问patch
然后会在会在项目目录下出现补丁包:
10.发布补丁:
在热更新上发布补丁。
11.测试:
先安装好基准包,点击按钮是app崩溃。等发布补丁后,稍等一会,系统bug修复,展示AlertDialog。崩溃信息也可在bugly后台查看,也可查看用户使用信息。