Android Espresso multidex fail

前端 未结 1 610
臣服心动
臣服心动 2021-01-12 06:41

We use multidex in our app for a long time but recently with latest update it fails on android API <19 e.g. emulator with api 16 It is standard java.lang.NoClassDefFou

相关标签:
1条回答
  • 2021-01-12 07:25

    Explanation by Google dev:

    The issue is that the rx.plugins.RxJavaHooks class referenced from the CustomJunitRunner.onCreate() method is in the secondary dex file of the main app, and you are accessing it before the class loaders get fully patched.

    When the main application and test code share a dependency, we will remove it from the test's dependencies (as we expect it to be available in the main application). However, with legacy multidex, this is causing problems.

    Currently, there are 2 workarounds:

    Option 1 Ensure the rx.plugins.RxJavaHooks is in the main dex by creating a file multidexKeepProguard.pro and adding "-keep class rx.plugins.**"

    Option 2 Remove references to RxJavaHooks from onCreate(), and move them to onStart() (not sure if this accomplishes when you want though): @Override public void onStart() { super.onStart(); //hook up schedulers to rxjava so espresso idling resouces can fetch it properly RxJavaHooks.setOnComputationScheduler(current -> Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR)); RxJavaHooks.setOnIOScheduler(current -> Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR)); RxJavaHooks.setOnNewThreadScheduler(current -> Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR)); }

    Solution

    Workaround

    So current workaround would be either use multidexKeepProguard.pro file and in your debug config point to that file:

     debug {
                applicationIdSuffix '.debug'
                multiDexKeepProguard file('../proguardRules/multidex-proguard.pro')
            }
    

    Your multidex proguard file should contain classes which are not being found in the main dex file, in my case it was RxJavaPlugin, so my multidexproguard file contains:

    -keep class rx.** { *; }
    
    0 讨论(0)
提交回复
热议问题