ACRA可以用于图书馆项目吗?

时间:2012-10-21 18:05:42

标签: android init illegalstateexception acra

ACRA本身就出现了一个奇怪的问题:

IllegalStateException: Cannot access ErrorReporter before ACRA#init

我有一个ACRA 4.3.0的应用,效果很好。我将整个应用程序更改为库,因此我可以进行小修改。我创建了一个完全空白的新项目,而不是清单和链接到这个新库。对于任何其他尝试此操作的人,在AcraApplication.java中,您必须删除“resToastText = R.string.crash_toast_text”行并在Acra.init(this)下面添加一个新行;

ACRA.getConfig().setResToastText(R.string.crash_toast_text);

项目构建正常,在调试中我已经确认了ACRA.init(this);在我的主程序代码之前和错误发生之前运行。在主程序中,我们设置了一些自定义数据:

ACRA.getErrorReporter().putCustomData("Orientation", "L");

它会导致崩溃(或更确切地说,ACRA本身会导致错误)并且不会生成ACRA报告。

任何想法接下来要尝试什么或指示哪里看?可能是ACRA与库不兼容,如果是这种情况,我可以用不同的方式将它拉出来,但有点破坏了库的目的。


解决方案:在init行之前添加以下三行,而不是添加Acra.init(this);以下的行:

ACRAConfiguration config = ACRA.getNewDefaultConfig(this); 
config.setResToastText(R.string.crash_toast_text);
ACRA.setConfig(config);

ACRA.init(this);

请注意,这仅适用于v4.3.0及更高版本。

3 个答案:

答案 0 :(得分:1)

我有同样的问题,它是由proguard混淆引起的。

解决方案是将以下自定义添加到proguard.cfg文件(取自ACRA wiki页面here):

请注意,ACRA.init()应保留在开头:

@Override
  public void onCreate() {
    ACRA.init(this);
    ACRA.getErrorReporter().setReportSender(new MySender());

    super.onCreate();
  } 

proguard.cfg:

#ACRA specifics
# we need line numbers in our stack traces otherwise they are pretty useless
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable

# ACRA needs "annotations" so add this... 
-keepattributes *Annotation*

# keep this class so that logging will show 'ACRA' and not a obfuscated name like 'a'.
# Note: if you are removing log messages elsewhere in this file then this isn't necessary
-keep class org.acra.ACRA {
    *;
}

# keep this around for some enums that ACRA needs
-keep class org.acra.ReportingInteractionMode {
    *;
}

-keepnames class org.acra.sender.HttpSender$** {
    *;
}

-keepnames class org.acra.ReportField {
    *;
}

# keep this otherwise it is removed by ProGuard
-keep public class org.acra.ErrorReporter
{
    public void addCustomData(java.lang.String,java.lang.String);
    public void putCustomData(java.lang.String,java.lang.String);
    public void removeCustomData(java.lang.String);
}

# keep this otherwise it is removed by ProGuard
-keep public class org.acra.ErrorReporter
{
    public void handleSilentException(java.lang.Throwable);
}

答案 1 :(得分:1)

确保您已在清单文件中添加

<application
    android:name="com.test.MyApp"

你有以下的应用程序类

import org.acra.ACRA;
import org.acra.ReportField;
import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes;

import android.app.Application;

@ReportsCrashes(formKey = "", mailTo = "your_email_address", customReportContent = {
            ReportField.APP_VERSION_CODE, ReportField.APP_VERSION_NAME,
            ReportField.ANDROID_VERSION, ReportField.PHONE_MODEL,
            ReportField.CUSTOM_DATA, ReportField.STACK_TRACE, ReportField.LOGCAT }, mode = ReportingInteractionMode.TOAST, resToastText = R.string.crash_toast_text)
    public class MyApp extends Application
    {
        @Override
        public void onCreate()
        {
            super.onCreate();
            ACRA.init(this);
        }
    }

答案 2 :(得分:1)

在我的情况下,我错过了@ReportCrashes配置...希望这会起作用

@ReportsCrashes(
    formUri = "uploadurl",
    reportType = HttpSender.Type.JSON,
    httpMethod = HttpSender.Method.POST,
    formUriBasicAuthLogin = "llenigingeneyederrownlys",
    formUriBasicAuthPassword = "1a35b13f9f54271d23a9aed988451182e5b97211",
    formKey = "", // This is required for backward compatibility but not used
    customReportContent = {
            ReportField.APP_VERSION_CODE,
            ReportField.APP_VERSION_NAME,
            ReportField.ANDROID_VERSION,
            ReportField.PACKAGE_NAME,
            ReportField.REPORT_ID,
            ReportField.BUILD,
            ReportField.STACK_TRACE
    },
    mode = ReportingInteractionMode.TOAST,
    resToastText =R.string.msg

)