我应该把 ACRA.init(this); 放在哪里?

Where should I place ACRA.init(this);?

我开始使用 ACRA (https://github.com/ACRA/acra) 进行崩溃报告。测试时,一切都很完美。尽管如此,当我发布该应用程序时,我在 Google Play 控制台中看到了一个错误,这是我发布的版本的新错误,由 ACRA.init(this);:

引起
java.lang.RuntimeException: 
at android.app.ActivityThread.handleBindApplication 
(ActivityThread.java:6209)
at android.app.ActivityThread.access00 (ActivityThread.java:236)
at android.app.ActivityThread$H.handleMessage 
(ActivityThread.java:1784)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loop (Looper.java:214)
at android.app.ActivityThread.main (ActivityThread.java:7032)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run 
(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:965)
Caused by: java.lang.IllegalStateException: 
at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1666)
at android.app.ContextImpl.startService (ContextImpl.java:1611)
at android.content.ContextWrapper.startService 
(ContextWrapper.java:677)
at org.acra.sender.SenderServiceStarter.startService 
(SenderServiceStarter.java:43)
at org.acra.util.ApplicationStartupProcessor.sendApprovedReports 
(ApplicationStartupProcessor.java:75)
at org.acra.ACRA.init (ACRA.java:230)
at org.acra.ACRA.init (ACRA.java:156)
at org.acra.ACRA.init (ACRA.java:139)
at com.myapplication.MyApplication.onCreate 
(MyApplication.java:132)
at android.app.Instrumentation.callApplicationOnCreate 
(Instrumentation.java:1154)
at android.app.ActivityThread.handleBindApplication 
(ActivityThread.java:6204)

MyApplication.java:132的内容是:

ACRA.init(this);

讽刺的是,这意味着初始化 ACRA 导致了崩溃。为了提供一些上下文,这是我 ACRA.init(this):

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    ACRA.init(this);

我正在阅读 https://groups.google.com/forum/#!topic/acra-discuss/XUKJ5dFHBl0 上的讨论,并且我阅读了 Malcolm Cooke 提出的解决方案:

For the benefit of anyone else I discovered what my problem was.

The class MyDBOpenHelper was being triggered from the onCreate method of a ContentProvider, which gets called before the application class's onCreate method. Resolved it for now by moving the acra init method call within the application class as follows

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

    ACRA.init(this);
    // some of your own operations before content provider will launch
}

我应该把ACRA.init(this);放在哪里?我在 public void onCreate() 中有它,但它把这个 java.lang.IllegalStateException 扔给我。所以我想我应该按照 Malcolm Cooke 的建议尝试将其放入 protected void attachBaseContext(Context base)。谁能确认 ACRA.init(this); 的正确位置是什么?谢谢。

更新 1:

实用的第 3 章 Android:14 个关于高级技术和方法的完整项目 (https://www.amazon.com/dp/B078SK4W1M/ref=dp-kindle-redirect?_encoding=UTF8&btkr=1) 提供了一个使用 ACRA 的项目示例,在他们的 MyApplication.java 文件中,他们使用这个:

package com.wickham.android.crashlog;

import org.acra.annotation.ReportsCrashes;
import org.acra.*;
import android.app.Application;

@ReportsCrashes(
                customReportContent = { ReportField.REPORT_ID,
                                        ReportField.APP_VERSION_CODE,
                                        ReportField.APP_VERSION_NAME,
                                        ReportField.PACKAGE_NAME, 
                                        ReportField.PHONE_MODEL, 
                                        ReportField.ANDROID_VERSION, 
                                        ReportField.STACK_TRACE,
                                        ReportField.TOTAL_MEM_SIZE,
                                        ReportField.AVAILABLE_MEM_SIZE,
                                        ReportField.DISPLAY,
                                        ReportField.USER_APP_START_DATE,
                                        ReportField.USER_CRASH_DATE,
                                        ReportField.LOGCAT,
                                        ReportField.DEVICE_ID,
                                        ReportField.SHARED_PREFERENCES,
                                        ReportField.CUSTOM_DATA },
                //formKey = "",
                formUri = "https://example.com/crashed.php",
                httpMethod = org.acra.sender.HttpSender.Method.POST,
                mode = ReportingInteractionMode.TOAST,
                resToastText = R.string.msg_crash_text)

public class MyApplication extends Application
{
    @Override
    public void onCreate() 
    {
        super.onCreate();
        ACRA.init(this);
    }
}

他们将 ACRA.init(this); 放在 public void onCreate() 中。它在我测试时对我有用。尽管如此,当我发布该应用程序时,我已经在 Google Play 控制台中看到由 ACRA.init(this); 引起的崩溃,正如我在问题中所解释的那样。所以我想我可以尝试将 ACRA.init(this); 放在 protected void attachBaseContext(Context base) 中,正如 Malcolm Cooke 所建议的那样。谁能给我澄清一下?

更新 2:

正在阅读 https://github.com/ACRA/acra/wiki/BasicSetup,我看到他们有这个:

import org.acra.*;
import org.acra.annotation.*;

@AcraCore(buildConfigClass = BuildConfig.class)
public class MyApplication extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

        // The following line triggers the initialization of ACRA
        ACRA.init(this);
    }
}

更新 3:

即使我将其放入 attachBaseContext,ACRA 也会让我的应用程序崩溃:

java.lang.RuntimeException: 
  at android.app.LoadedApk.makeApplication (LoadedApk.java:1164)
  at android.app.ActivityThread.handleBindApplication (ActivityThread.java:6529)
  at android.app.ActivityThread.access00 (ActivityThread.java:267)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1963)
  at android.os.Handler.dispatchMessage (Handler.java:109)
  at android.os.Looper.loop (Looper.java:207)
  at android.app.ActivityThread.main (ActivityThread.java:7470)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:524)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:958)
Caused by: java.lang.IllegalStateException: 
  at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1842)
  at android.app.ContextImpl.startService (ContextImpl.java:1797)
  at android.content.ContextWrapper.startService (ContextWrapper.java:664)
  at org.acra.sender.SenderServiceStarter.startService (SenderServiceStarter.java:43)
  at org.acra.util.ApplicationStartupProcessor.sendApprovedReports (ApplicationStartupProcessor.java:75)
  at org.acra.ACRA.init (ACRA.java:230)
  at org.acra.ACRA.init (ACRA.java:156)
  at org.acra.ACRA.init (ACRA.java:139)
  at com.myapp.MyApplication.attachBaseContext (MyApplication.java:126)
  at android.app.Application.attach (Application.java:224)
  at android.app.Instrumentation.newApplication (Instrumentation.java:1128)
  at android.app.LoadedApk.makeApplication (LoadedApk.java:1156)

根据 https://github.com/ACRA/acra/issues/630 上的信息,我的解决方案是使用这个:

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    ACRA.init(this, new ConfigurationBuilder(this).build(), false);
    MultiDex.install(this);
}

在日志中,我可以看到 LOGCAT 显示了这一行:

08-18 16:31:50.489 I/ACRA    (11890): ACRA is enabled for com.myapp, initializing...

初始化成功