1.设置kotlin版本

ext.kotlin_version = '1.5.21'

2.在app module的build.gradle添加如下内容:

apply plugin: 'kotlin-kapt'
defaultConfig {
    ...
    //add next
    javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true }}
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
implementation("ch.acra:acra-http:5.8.3")
kapt 'com.google.auto.service:auto-service:1.0-rc6'
api 'com.google.auto.service:auto-service:1.0-rc6'

3.在Application中添加如下

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);

    CoreConfigurationBuilder builder = new CoreConfigurationBuilder(this);
    builder.withBuildConfigClass(BuildConfig.class)
            .withReportFormat(StringFormat.JSON);
    builder.getPluginConfigurationBuilder(HttpSenderConfigurationBuilder.class)
            .withUri("https://yourserver.com/report")
            .withEnabled(true);
    ACRA.init(this, builder);
}

4.添加自定义的CrashSender

import android.content.Context;
import android.util.Log;

import com.google.auto.service.AutoService;

import org.acra.config.CoreConfiguration;
import org.acra.data.CrashReportData;
import org.acra.sender.ReportSender;
import org.acra.sender.ReportSenderException;
import org.acra.sender.ReportSenderFactory;
import org.jetbrains.annotations.NotNull;

public class CrashSender implements ReportSender {

    @Override
    public void send(@NotNull Context context, @NotNull CrashReportData errorContent) throws ReportSenderException {
        Log.d("test", ">>>>Report Sent!");
    }


    @AutoService(ReportSenderFactory.class)
    public static class MySenderFactory implements ReportSenderFactory {
        @NotNull
        @Override
        public ReportSender create(@NotNull Context context, @NotNull CoreConfiguration coreConfiguration) {
            return new CrashSender();
        }
    }
}
Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐