Twitter Fabric + MultiDex Raises NoClassDefFoundError

I have Multidex support in my project and it works fine on Android 5.0 and above. But when I started testing my application with Android 4.3 and below, it started giving me NoClassDefFoundError for TwitterConfig .

I can’t disable Multidex support. I do not understand why I am getting this problem. I tried to find a solution on the network, but did not help.

Here is the Exception I get.

 java.lang.NoClassDefFoundError: com.twitter.sdk.android.core.TwitterAuthConfig$1 at com.twitter.sdk.android.core.TwitterAuthConfig.<clinit>(TwitterAuthConfig.java:39) at MyApplication.onCreate(MyApplication.java:46) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4702) at android.app.ActivityThread.access$1400(ActivityThread.java:168) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1389) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5493) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025) at dalvik.system.NativeStart.main(Native Method) 

Here is my app.gradle

  buildscript { repositories { maven { url 'https://maven.fabric.io/public' } } dependencies { classpath 'io.fabric.tools:gradle:1.+' } } apply plugin: 'com.android.application' apply plugin: 'io.fabric' repositories { maven { url 'https://maven.fabric.io/public' } mavenCentral() } android { packagingOptions { exclude 'META-INF/NOTICE.txt' exclude 'META-INF/LICENSE.txt' } compileSdkVersion 22 buildToolsVersion "23.0.0 rc3" defaultConfig { minSdkVersion 16 targetSdkVersion 22 versionCode 1 versionName "1.0" multiDexEnabled true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } dexOptions { javaMaxHeapSize "2g" } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.2.1' compile 'com.android.support:multidex:1.0.1' compile 'com.android.support:design:22.2.1' compile 'com.android.support:recyclerview-v7:22.2.1' compile 'com.android.support:cardview-v7:22.2.1' compile 'com.android.support:support-v4:22.2.1' compile project(':MPChartLib') compile 'com.facebook.android:facebook-android-sdk:4.6.0' compile('com.crashlytics.sdk.android:crashlytics: 2.5.2@aar ') { transitive = true; } compile('com.twitter.sdk.android:twitter: 1.8.0@aar ') { transitive = true; } } 

Here is my Twitter code in the MyApplication class.

 @Override public void onCreate() { super.onCreate(); //Configure twitter using Twitter key and Secret Key TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET); // add functionalities which we are using with Fabric Fabric.with(this, new Crashlytics(), new Twitter(authConfig)); FacebookSdk.sdkInitialize(getApplicationContext()); // Initialize the SDK before executing any other operations, // especially, if you're using Facebook UI elements. } 

Even I tried this solution, but it didn’t help.

Any hint or recommendation is appreciated.

+6
source share
2 answers

Support for multiple applications for Android 5.0 and higher

Android 5.0 and above uses a runtime called ART, which initially supports downloading multiple dex files from the application’s APK files. FIGURE ART pre-compiles during installation of an application that scans classes (..N) .dex and compiles them into a single .oat file for execution on an Android device. For more information about Android 5.0, see Introducing ART.

This means that your application will run fine at API level 21 or higher.

Multidex support up to Android 5.0

Platform versions prior to Android 5.0 use Dalvik runtime to execute application code. By default, Dalvik restricts applications to one classes.dex for each APK. To get around this limitation, you can use the multidex support library, which becomes part of the main DEX file of your application, and then controls access to additional DEX files and the code that they contain.

So, firstly, make sure you import the correct dependency, which seems like you did.

 dependencies { compile 'com.android.support:multidex:1.0.1' } 

In the manifest, add the MultiDexApplication class from the multidex support library to the application element.

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.multidex.myapplication"> <application ... android:name="android.support.multidex.MultiDexApplication"> ... </application> </manifest> 

Alternatively, if your application extends the Application class, you can override the attachBaseContext() method and call MultiDex.install(this) to enable multidex .

 public void onCreate(Bundle arguments) { MultiDex.install(getTargetContext()); super.onCreate(arguments); ... } 

Finally, you will need to update the build.gradle file as shown below by adding multiDexEnabled true :

 defaultConfig { applicationId '{Project Name}' minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" multiDexEnabled true } 

I hope this helps you.

+14
source

I solved the problem by manually installing the gradle plugin version to lower the version of "1.21.6". There was probably a problem with the latter, which in my case was "1.23.0". ( Fabric Plugin Version History )

So I installed

 dependencies { classpath 'io.fabric.tools:gradle:1.21.6' } 

instead

 dependencies { classpath 'io.fabric.tools:gradle:1.+' } 
0
source

All Articles