Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve ConcurrentModificationException

After updating the App I get errors from Firebase almost in all activities.

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.app/com.project.app.MainActivity}: java.util.ConcurrentModificationException
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895)
   at android.app.ActivityThread.-wrap11()
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1616)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loop(Looper.java:176)
   at android.app.ActivityThread.main(ActivityThread.java:6651)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

Caused by java.util.ConcurrentModificationException
   at androidx.collection.SimpleArrayMap.put(SimpleArrayMap.java:482)
   at com.google.android.gms.measurement.internal.zzin.zza(com.google.android.gms:play-services-measurement-impl@@17.2.2:108)
   at com.google.android.gms.measurement.internal.zzid.onActivityCreated(com.google.android.gms:play-services-measurement-impl@@17.2.2:11)
   at android.app.Application.dispatchActivityCreated(Application.java:199)
   at android.app.Activity.onCreate(Activity.java:1034)
   at androidx.core.app.ComponentActivity.onCreate(ComponentActivity.java:81)
   at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:149)
   at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:313)
   at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:106)
   at com.project.app.MainActivity.onCreate(MainActivity.java:74)
   at android.app.Activity.performCreate(Activity.java:7088)
   at android.app.Activity.performCreate(Activity.java:7079)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895)
   at android.app.ActivityThread.-wrap11()
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1616)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loop(Looper.java:176)
   at android.app.ActivityThread.main(ActivityThread.java:6651)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

I am sure the problem appeared after changing build.gradle (Module:app).

This is some lines of build.gradle (old version):

implementation 'com.google.android.material:material:1.2.0-alpha02'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta3'
implementation 'com.android.billingclient:billing:2.0.3'
implementation 'com.google.firebase:firebase-ads:18.3.0'
implementation 'com.google.firebase:firebase-core:17.2.1'
implementation 'com.google.firebase:firebase-analytics:17.2.1'
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'

This is some lines of build.gradle (new version):

implementation 'com.google.android.material:material:1.2.0-alpha04'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
implementation 'com.android.billingclient:billing:2.1.0'
implementation 'com.google.firebase:firebase-ads:18.3.0'
//implementation 'com.google.firebase:firebase-core:17.2.2'
implementation 'com.google.firebase:firebase-analytics:17.2.2'
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'

I think is due to firebase-analytics:17.2.2, but I'm not sure. How do you think where is the problem? And Do I need to downgrade to the old version?

like image 964
TimWeb Avatar asked Feb 09 '20 19:02

TimWeb


People also ask

What is ConcurrentModificationException and how it can be prevented?

ConcurrentModificationException is a predefined Exception in Java, which occurs while we are using Java Collections, i.e whenever we try to modify an object concurrently without permission ConcurrentModificationException occurs which is present in java. util package.

What causes ConcurrentModificationException?

What Causes ConcurrentModificationException. The ConcurrentModificationException generally occurs when working with Java Collections. The Collection classes in Java are very fail-fast and if they are attempted to be modified while a thread is iterating over it, a ConcurrentModificationException is thrown.

What is ConcurrentModificationException how do you avoid it while iterating a collection?

The ConcurrentModificationException occurs when an object is tried to be modified concurrently when it is not permissible. This exception usually comes when one is working with Java Collection classes. For Example - It is not permissible for a thread to modify a Collection when some other thread is iterating over it.

Which method of the iterator throws ConcurrentModificationException?

ConcurrentModificationException is thrown at next call to next() method during iteration. So its not remove() method of collection which throws this exception, but its next() method of iterator implementation.


1 Answers

Its because firebase-core lib they fixed it in version 17.2.3 see changelog: https://firebase.google.com/support/release-notes/android#analytics_v17-2-3

change your

'com.google.firebase:firebase-core:17.2.1'

to

'com.google.firebase:firebase-core:17.2.3'

like image 55
Daniel Pína Avatar answered Sep 29 '22 07:09

Daniel Pína