3

I have a view with a viewModel that observes my LiveData. I get my data from a Repository (which gets data either from Room or WebAPI). I have implemented a NetworkBoundResource abstract class so I can manage data loading in a cleaner way. MediatorLiveData object is used in this class:

private final MediatorLiveData<Resource<ResultType>> result = new MediatorLiveData<>();

The crash happens at the first line of my constructor:

@MainThread
public NetworkBoundResource() {

    result.setValue(Resource.<ResultType>loading(null));

    final LiveData<ResultType> dbSource = loadFromDb();

    result.addSource(dbSource, new Observer<ResultType>() {
        @Override
        public void onChanged(@Nullable ResultType newData) {
            result.removeSource(dbSource);
            if(shouldFetch(newData))
            {
                fetchFromNetwork(dbSource);
            } else {
                result.setValue(Resource.success(newData));
            }
        }
    });
}

Here is the error message:

java.lang.NoSuchMethodError: No virtual method iteratorWithAdditions()Landroid/arch/core/internal/SafeIterableMap$ListIterator; in class Landroid/arch/core/internal/SafeIterableMap; or its super classes (declaration of 'android.arch.core.internal.SafeIterableMap' appears in /data/app/com.example.smostofi.upswing-XxPvhI837wneIYVTOrU2Dw==/split_lib_dependencies_apk.apk:classes25.dex)
                                                                            at android.arch.lifecycle.LiveData.dispatchingValue(LiveData.java:145)
                                                                            at android.arch.lifecycle.LiveData.setValue(LiveData.java:293)
                                                                            at android.arch.lifecycle.MutableLiveData.setValue(MutableLiveData.java:33)
                                                                            at com.example.smostofi.upswing.data.Util.NetworkBoundResource.<init>(NetworkBoundResource.java:44)

First time asking a question here, hopefully I provided enough info! Thanks in advance!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
ShokouhM
  • 33
  • 3

2 Answers2

3

It is not duplicate dependency issue, just change your life cycle version from

compile 'android.arch.lifecycle:extensions:1.0.0-alpha4'

to

compile 'android.arch.lifecycle:extensions:1.0.0-alpha9'
JanBrus
  • 1,198
  • 9
  • 13
0

You must have added the same dependency more than once in your Gradle.

I had a same issue. The reason was, I had added the Appcompact library twice in the Gradle as below :

implementation 'com.android.support:appcompat-v7:26.1.0'
compile rootProject.ext.supportLibAppCompat

By removing one, the issue got resolved.

Please check your Gradle for the same. Hope it helps :)

iMDroid
  • 2,108
  • 1
  • 16
  • 29
  • 1
    wow! Thanks a lot! I could not figure out where the duplication existed, but I guess that was my issue too, cause I replaced my whole dependency list with one from a similar app , and now it is working! thanks again! – ShokouhM Nov 09 '17 at 18:39