Questions:
The library of a new open source library in its own project, the compilation ran successfully on the phone began to appear abnormal, but the mobile phone version above or equal to 21 is normal. Error message:
java.lang.NoClassDefFoundError: rx.plugins.RxJavaPlugins
In fact, there are some Rxjava to use the file prompts display, but before the use of Rxjava are normal, and import a library after the error, the error is definitely not on the Rxjava, so the real error is the first half sentence.
java.lang.NoClassDefFoundError
Reason:
The cause of the problem is that the project does not initialize the MULTIDEX option!
Official documentation notes: Multidex support forAndroid 5.0 and higher
Android 5.0 and higher uses a runtime called ART which natively supports loading multiple Dex files from application APK F Iles. ART performs pre-compilation at application install time which scans for classes (.. N). dex files and compiles them into a single. oat file for execution by the Android device. For more information on the Android 5.0 runtime, see Introducing ART. The reason why your app was working fine on API level 21.
Overall,Android 5.0 and later has a time-consuming operation called ART that supports loading multiple Dex files from an application's apk file. Art performs pre-compilation during application installation, scanning classes (.. N). dex file and compile it as a single. oat file for Android devices to execute.
The above is why the application can work properly at API level 21.
Workaround:
Multidex support prior to Android 5.0
Versions of the platform prior to Android 5.0 with the Dalvik runtime for executing app code. By default, the Dalvik limits apps to a single Classes.dex bytecode file per APK. In order to get around this limitation, you can use the Multidex support library, which becomes part of the primary DEX fi Le of your app and then manages access to the additional DEX files and the code they contain.
The above is for Android version before 5.0 is less than 5.0 when the solution (3 steps, indispensable):
- 1 in the app folder, specify the location of the Build.gradle file to add:
true
- 2 Add Multidex package dependency in the Build.gradle file under the app folder
‘com.android.support:multidex:1.0.0‘
- 3 inherit the project custom application multidexapplication, or override the method directly in the custom application code as follows:
@Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); }
Compile again and the problem can be solved.
This problem is primarily a version issue that is caused by the art program, which java.lang.NoClassDefFoundError
can be resolved by this method.
Import Open Source Library Java.lang.NoClassDefFoundError:rx.plugins.RxJavaPlugins