Androidstudio配置:
gradle(module)
defaultConfig{
ndk {
// 設(shè)置支持的SO庫(kù)架構(gòu)
abiFilters 'armeabi' //, 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
}
}
dependencies{
//bugly
implementation 'com.tencent.bugly:crashreport:latest.release' //其中l(wèi)atest.release指代最新Bugly SDK版本號(hào),也可以指定明確的版本號(hào),例如2.1.9
implementation 'com.tencent.bugly:nativecrashreport:latest.release' //其中l(wèi)atest.release指代最新Bugly NDK版本號(hào),也可以指定明確的版本號(hào),例如3.0
}
AndroidManifest.xml
<!--bugly配置-->
<!-- 配置APP ID -->
<meta-data
android:name="BUGLY_APPID"
android:value="8823c5a1be"/>
<!-- 配置APP版本號(hào) 配置的是Bugly平臺(tái)的APP版本號(hào)。-->
<meta-data
android:name="BUGLY_APP_VERSION"
android:value="1.0.0"/>
<!-- 配置APP渠道號(hào) -->
<meta-data
android:name="BUGLY_APP_CHANNEL"
android:value="baidu"/>
<!-- 配置Bugly調(diào)試模式 isDebug (true或者false)-->
<meta-data
android:name="BUGLY_ENABLE_DEBUG"
android:value="true"/>
<!--bugly配置-->
初始化:
private void initbugly() {
// 獲取當(dāng)前包名
String packageName = mContext.getPackageName();
// 獲取當(dāng)前進(jìn)程名
String processName = getProcessName(android.os.Process.myPid());
// 設(shè)置是否為上報(bào)進(jìn)程
CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(mContext);
strategy.setUploadProcess(processName == null || processName.equals(packageName));
// 初始化Bugly
CrashReport.initCrashReport(mContext, strategy);
// CrashReport.testJavaCrash();
}
/**
* 獲取進(jìn)程號(hào)對(duì)應(yīng)的進(jìn)程名
*
* @param pid 進(jìn)程號(hào)
* @return 進(jìn)程名
*/
public static String getProcessName(int pid) {
BufferedReader reader;
reader = null;
try {
reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
String processName = reader.readLine();
if (!TextUtils.isEmpty(processName)) {
processName = processName.trim();
}
return processName;
} catch (Throwable throwable) {
throwable.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException exception) {
exception.printStackTrace();
}
}
return null;
}
測(cè)試:
textview.settext(null);
CrashReport.testJavaCrash();
try catch CrashReport:
try {
textview.settext(null);
} catch (e: Exception) {
CrashReport.postCatchedException(e)
}
//長(zhǎng)傳自己的文字的異常
try {
textview.settext(null);
} catch (Exception e) {
CrashReport.postCatchedException(new Exception("我是異常") )
}
}