Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Gradle Multidex Build issue on API 19

I have a project in which I have enabled multidex to avoid 65k limit and also productFlavors (dev API 21 and prod API 19) for customization. Building my Project on API 21 i.e dev flavor is successful but on API 19 i.e. prod flavor, it is continuously giving me exception in app task shrink{component}MultiDexComponents

Complete Error log:

:app:shrinkProdDebugMultiDexComponents FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:shrinkProdDebugMultiDexComponents'.
> java.io.IOException: Can't read [{Project Path}/app/build/intermediates/multi-dex/prod/debug/allclasses.jar] (Can't process class [com/olivephone/office/a/b/e/p.class] (Unknown verification type [17] in stack map frame))

build.gradle

apply plugin: 'com.android.application'

android {  
    compileSdkVersion 23  
    buildToolsVersion '23.0.0'  
    defaultConfig {  
        applicationId '{Project Name}'  
        minSdkVersion 15  
        targetSdkVersion 23  
        versionCode 1  
        versionName "1.0"  
        multiDexEnabled true  
    }  
    productFlavors {  
        dev {  
            // dev utilizes minSDKVersion = 21 to allow the Android gradle plugin  
            // to pre-dex each module and produce an APK that can be tested on  
            // Android Lollipop without time consuming dex merging processes.  
            minSdkVersion 21  
        }  
        prod {  
            // The actual minSdkVersion for the application.  
            minSdkVersion 19  
        }  
    }  
    buildTypes {  
        release {  
            minifyEnabled false  
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'  
        }  
    }  
}  

dependencies {  
    compile fileTree(include: ['*.jar'], dir: 'libs')  
    compile 'com.android.support:appcompat-v7:23.0.1'  
    compile 'com.android.support:recyclerview-v7:23.0.1'  
    compile 'com.android.support:cardview-v7:23.0.1'  
    compile 'com.oguzdev:CircularFloatingActionMenu:1.0.2'  
    compile 'com.google.code.gson:gson:2.3.1'  
    compile 'com.google.android.gms:play-services:8.1.0'  
    compile 'com.android.support:multidex:1.0.1'  
    compile files('libs/linkedin-j-android.jar')  
    compile files('libs/itsrts-pptviewer.jar')  
    compile files('libs/signpost-core-1.2.1.1.jar')  
    compile 'org.twitter4j:twitter4j-core:4.0.2'  
    compile files('libs/universal-image-loader-1.9.2-SNAPSHOT-with-sources.jar')
    compile files('libs/dropbox-android-sdk-1.6.3.jar')  
    compile files('libs/json_simple-1.1.jar')  
    compile 'com.joanzapata.pdfview:android-pdfview:1.0.1@jar'  
    compile 'com.facebook.android:facebook-android-sdk:4.1.0'  
}

Any help please anyone ??

like image 879
Sidakpreet N Avatar asked Sep 28 '15 06:09

Sidakpreet N


4 Answers

Multidex support for Android 5.0 and higher

Android 5.0 and higher uses a runtime called ART which natively supports loading multiple dex files from application APK files. 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.

This is the reason why your app is working fine on API level 21.

Multidex support prior to Android 5.0

Versions of the platform prior to Android 5.0 use the Dalvik runtime for executing app code. By default, 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 file of your app and then manages access to the additional DEX files and the code they contain.

So, Firstly making sure you have imported correct dependency, which It seems you did it.

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

In your 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>

Alternative to that, If your app 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);
    ...
}

I hope it will help you out.

like image 197
Bhavesh Patadiya Avatar answered Oct 04 '22 05:10

Bhavesh Patadiya


Solved above crash issue. I have remove some unused dependency and also removed rx java dependency io.reactivex.rxjava . In replacement of Rx java i have added some dummy classes in my package which is already described here https://realm.io/docs/java/latest.

// File 1
package io.reactivex;
public class Flowable {
}
// File 2
package io.reactivex;
public class Observable {
}
// File 3
package io.reactivex;
public enum BackpressureStrategy {
   LATEST;
}
like image 30
Devganiya Hitesh Avatar answered Oct 04 '22 04:10

Devganiya Hitesh


I haven't peeped into the logic behind this, but in my case, it was due to version conflicts of compile 'com.android.support:appcompat-v7:25.3.1' and compile 'com.google.android.gms:play-services:10.2.4' in my build.gradle file.

Only enabling multidex didn't work for me. While searching for the other solutions, I found some people complaining about wierd issue of version conflict of play-services. So, I backtracked the code changes and finally changed play-services version from 10.2.4 to 10.2.1 and it worked for me.

like image 20
Ankur Avatar answered Oct 04 '22 06:10

Ankur


In API 21, :app:shrinkProdDebugMultiDexComponents command is not called as API 21 already uses ART instead of Dalvik. Thus, natively support multidex.

For API below 21, then the command :app:shrinkProdDebugMultiDexComponents is executed. Checking your build.gradle everything looks fine, which brings me to the following.

Have you setup multidex support properly?

Have you setup your manifest to support Multidex?

<?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>

OR if you actually extending the application class, you can do this:

public class MyApplication extends Application {
    ...
    @Override
    protected void attachBaseContext(Context base) {
       super.attachBaseContext(base);
       MultiDex.install(this);
    }
    ...     
}

or use this "pre-built" version

public class MyApplication extends MultiDexApplication{

 ...     

}
like image 21
Fadils Avatar answered Oct 04 '22 04:10

Fadils