15

i've overloaded the Application class in my android app and i'm using the ACRA report system. My app looks like ( real source code here ) :

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

        /*
         * Initialize my singletons etc
         * ...
         * ...
         */
        super.onCreate();
    }
}

And as far as i know, the Application object should be created only once, so the onCreate method should be called only once. The problem is, that in my crash reports ( from ACRA ) i have this:

java.lang.RuntimeException: Unable to create service it.evilsocket.myapp.net.N ...
java.lang.RuntimeException: Unable to create service it.evilsocket.myapp.net.NetworkMonitorService: java.lang.RuntimeException: Unable to create application it.evilsocket.myapp.MyApplication: **java.lang.IllegalStateException: ACRA#init called more than once**
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2283)
    at android.app.ActivityThread.access$1600(ActivityThread.java:127)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4441)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to create application it.evilsocket.myapp.MyApplication: java.lang.IllegalStateException: ACRA#init called more than once
    at android.app.LoadedApk.makeApplication(LoadedApk.java:495)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2269)
    ... 10 more
Caused by: java.lang.IllegalStateException: ACRA#init called more than once
    at org.acra.ACRA.init(ACRA.java:118)
    at it.evilsocket.myapp.MyApplication.onCreate(MyApplication.java:46)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:969)
    at android.app.LoadedApk.makeApplication(LoadedApk.java:492)
    ... 11 more
java.lang.RuntimeException: Unable to create application it.evilsocket.myapp.MyApplication: java.lang.IllegalStateException: ACRA#init called more than once
    at android.app.LoadedApk.makeApplication(LoadedApk.java:495)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2269)
    at android.app.ActivityThread.access$1600(ActivityThread.java:127)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4441)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: ACRA#init called more than once
    at org.acra.ACRA.init(ACRA.java:118)
    at it.evilsocket.myapp.MyApplication.onCreate(MyApplication.java:46)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:969)
    at android.app.LoadedApk.makeApplication(LoadedApk.java:492)
    ... 11 more
java.lang.IllegalStateException: ACRA#init called more than once
    at org.acra.ACRA.init(ACRA.java:118)
    at it.evilsocket.myapp.MyApplication.onCreate(MyApplication.java:46)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:969)
    at android.app.LoadedApk.makeApplication(LoadedApk.java:492)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2269)
    at android.app.ActivityThread.access$1600(ActivityThread.java:127)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4441)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)

So it seems like the app onCreate is being called multiple times, any idea on this ?

NOTES:

  • In my android xml manifest, i did NOT use the android:process="string" attribute.
  • Yes, i'm sure that in my initialization routines i'm not accidentally calling MyApplication.onCreate .
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Simone Margaritelli
  • 4,584
  • 10
  • 45
  • 70
  • 3
    I'm not talking about Activity.onCreate ( which is normally being called every time a configuration change occurs ), but Application.onCreate! – Simone Margaritelli Oct 09 '12 at 23:17
  • Have you tried setting a breakpoint here? – CommonsWare Oct 09 '12 at 23:27
  • it's something i can't reproduce on my device, i just see this from the ACRA reports ... if i break there or put a Log.d, onCreate is called only once on both my smartphone and tablet. – Simone Margaritelli Oct 09 '12 at 23:30
  • This question is not a duplicate of the referenced question. The referenced question is about Activity.onCreate() whereas here is about Application.onCreate(). Answers can't be merged. – Kevin Gaudin Nov 03 '12 at 23:51
  • How did u solve this issue ? – Harsha M V Jan 24 '13 at 19:12
  • In my case, onCreate method of application class is only being called when I install the app in mobile, it does not being invoked every time when I kill the app and restart it. – Shubham Mogarkar May 04 '22 at 07:33

4 Answers4

33

I think that you have additional process in your application. That is why Application.onCreate is called more than once. Look into your manifest file and try to find the activity or service with something like android:process= . This means that activity/service is starting in second Dalvik VM, and that's why another application instance is created.

Alex Kutsko
  • 1,129
  • 11
  • 9
10

If you look at the stack trace, it looks like ACRA.init is calling makeApplication. I suspect that there's some sort of code to check if the application has been created already and if not, create it and that it is caused by your calling ACRA.init before super.onCreate. Generally when overriding onCreate methods (whether Application or Activity) it's recommended to call super.onCreate as the first line of your implementation and do your custom stuff afterwards. I'd give that a shot and see if it fixes things.

kabuko
  • 36,028
  • 10
  • 80
  • 93
  • That could be an explanation. I documented ACRA with an init before super.onCreate() in order to let ACRA report exceptions that would happen as soon as possible in the application creation process. I might reconsider this if this behavior is reported by more users. – Kevin Gaudin Nov 03 '12 at 23:48
  • 6
    @KevinGaudin - I've tried Acra with init after super.onCreate(), and still get "ACRA#init called more than once" error from time to time – Amir Uval Dec 26 '12 at 14:42
  • 3
    I can confirm that even if you call ACRA.init() after super(), this issue still happens. – Aswin Kumar Apr 22 '13 at 10:02
  • 1
    agree with you, @AswinKumar – thecr0w Mar 14 '14 at 05:19
6

I'm also seeing this with ACRA 4.4.0 in the wild.

Perhaps something as simple as this under the init method?

if (mApplication != null) {
    throw new IllegalStateException("ACRA#init called more than once");
    //(return or finish or gracefully exit somehow)       
} else {
    mApplication = app;
    //and then continue with rest of acra init...

Edit: 12/27/12 As a follow up to this, it looks like Kevin has adopted these changes. Details are here: https://github.com/ACRA/acra/commit/cda06f5b803a09e9e7cc7dafae2c65c8fa69b861

logray
  • 2,332
  • 1
  • 29
  • 30
1

I have been looking at ACRA source code recently and think this issue has been addressed in ACRA in two ways:

  1. Instead of onCreate(), now ACRA's doc recommends initializing ACRA in attachBaseContext which is called before onCreate().
  2. ACRA does have some logic to check if there are other ACRA instances during the initialization. If yes, ACRA will unregister the existing reporter and bypass the current reporter by some Proxy treatment.

Check init function in ACRA.kt

Freddie
  • 210
  • 3
  • 6