user7360646
user7360646

Reputation:

number of referenced method in .dex file cannot exceed 64K and java.lang.UnsupportedOperationException

I am new to android. I am trying to build an apk file. While building an app it ends with two errors:
a). In the first error it says The number of referenced method in .dex file cannot exceed 64K and gave me this Link.
b). In the second error it says java.lang.UnsupportedOperationException.
I have successfully added multidex support library and generated an apk file for testing and fix the issue. Like this

android {
    compileSdkVersion 22
    buildToolsVersion "23.0.0"

         defaultConfig {
             minSdkVersion 14 //lower than 14 doesn't support multidex
             targetSdkVersion 22

             // Enabling multidex support.
             multiDexEnabled true
         }
}

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


But still I have few doubts here
a). Why do I need to add multidex support library ?
b). What's the future purpose ?
c). What does the second error mean ?
d). In what sense it say that .dex file cannot exceed 64K ? Can you help me ?
Thank you.

Upvotes: 2

Views: 221

Answers (2)

Gopal
Gopal

Reputation: 1784

a). Why do I need to add Multidex Support Library?

-> The purpose of this is to split up your Dex file into multiple Dex files.This library provides support for building apps with multiple Dalvik Executable (DEX) files. Apps that reference more than 65536 methods are required to use Multidex configurations.

b). What's the future purpose?

-> Android has a problem whereby there is an upper limit on the number of method definitions in a Dex file (64k). This means that once that limit is reached, you cannot expand your application any further.

Before Multidex, it was advised to use ProGuard to reduce the number of method definitions by removing methods that aren't used in the code. Especially useful when implementing the Google Play Services Framework.

Multidex was then introduced and allows Apps to continue to expand without worrying about method count limits. It does carry the danger of making the App more unstable. Therefore it is advised to try ProGuard first to reduce the method count.

c). What does the second error mean?

java.lang.UnsupportedOperationException

Ref: https://stackoverflow.com/a/21061985/3758024

Please provide the complete crash log/stack trace of this crash from your app with the relevant code snippet.

d). In what sense it say that .dex file cannot exceed 64K? Can you help me?

-> Android app (APK) files contain executable bytecode files in the form of Dalvik Executable (DEX) files, which contain the compiled code used to run your app. The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536—including Android framework methods, library methods, and methods in your own code. In the context of computer science, the term Kilo, K, denotes 1024 (or 2^10). Because 65,536 is equal to 64 X 1024, this limit is referred to as the '64K reference limit'.

More References.

1) https://developer.android.com/studio/build/multidex.html#avoid

2) https://medium.com/@rotxed/dex-skys-the-limit-no-65k-methods-is-28e6cb40cf71#.3eg897jca

3) https://blog.mustafaali.xyz/dexs-64k-limit-is-not-a-problem-anymore-well-almost-2b1faac3508#.wiaruldue

4)http://www.fasteque.com/deep-dive-into-android-multidex/

Upvotes: 3

Quick learner
Quick learner

Reputation: 11457

Also 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">
<!--If you are using your own custom Application class then extend -->
<!--MultiDexApplication and change above line as-->
            android:name=".YourCustomApplicationClass">

            ...
        </application>
    </manifest>

override attachBaseContext method

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(newBase);
    MultiDex.install(this);
}

Upvotes: 0

Related Questions