Search code examples
androidgradledex

64k limitation while testing - Android


I'm trying to run some tests on a specific module in my android application but for some reason I keep getting these build error message:

Error:The number of method references in a .dex file cannot exceed 64K.
Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html
Error:Execution failed for task ':javadgd:transformClassesWithDexForDebugAndroidTest'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: Error while executing java process with main class com.android.dx.command.Main with arguments {--dex --num-threads=4 --output C:\Projects\DGDEditor\javadgd\build\intermediates\transforms\dex\androidTest\debug\folders\1000\1f\main C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\classes_03333ab422775c158385448eafde7255a8524b93.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\javax.inject-1_721956a06ac86a777d8de583b4c13faebe0d5c5c.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\classes_94d5d0d0b278f09742c1e1dd1e53e80c88aeaba2.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\classes_2fd8a6413454d324a2bc2c41cedf751944c4a85a.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\jsr305-2.0.1_d0dc22afb31a509a829f5e56cd36e012ebe188e5.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\classes_feb1c246f0d10c407b60ae0ecd1d6ea8d8beca61.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\debug_f41558349a77243324d91f8782be0d487cd7d6ed.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\pkix-1.51.0.0_b76041394e3ad7737254c45a49f806aadb0e59da.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\hamcrest-core-1.3_92e7cef8754982c623a8492d3b3952651a36c08f.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\parceler-1.1.8_c0288db4cd1aae135e8b25494edc84eb400f3614.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\classes_6375c37ed5b22eb7cf9cd3a95bff1c6408d121ab.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\classes_bfa059c3d3a726ece320b7ef44fc899d58e51e52.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\classes_c7168d8cf335e64692d4939d085bc7489d9e37ee.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\classes_f1704eb11b8725da59212cbcea409be026a3ffcd.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\classes_67453a78ef3c9cab581c393c472c3c3d612dcc16.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\javax.annotation-api-1.2_99de26727b595d3c7e23682b1e351f77584621a6.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\classes_19aa33b9e42e4289994b07b2a0039e60888115cc.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\classes_b8055fc216a8eb86450b50466066c1960737277b.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\parceler-api-1.1.8_63d56b028e1b1695ca7293e1f2a92c9d9a0991f3.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\junit-4.12_70f50e338c3877b7b2e790ad100069bbc42c9c8f.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\classes_76cd03c23e8d9d3dc8f3c881ebccac6befa92db3.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\classes_f4edc33f0197e9449d5db5d862506bf37fe1c439.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\classes_25f2307ba0685e5e9569cd17ea92c99bb4649923.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\hamcrest-library-1.3_cc5997a988d1cb5dc61f56c2702d0209d499e271.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\prov-1.51.0.0_3793358556a1c80f36dba381de93ced0fc398087.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\classes_c06d6884053b63859253ba8751a5baef40098a74.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\support-annotations-25.1.0_8b16b00a90b3b73a336a7720f92a8e291227f223.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\core-1.51.0.0_f14b10294d4c741e145544e7806a68f73f58fe16.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\hamcrest-integration-1.3_77e530f0dc04d8547a01b2579bac749e0613dfdd.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\classes_4dc0d5d6487e5aaa62d200676a7d8d359cfdc7f6.jar C:\Projects\DGDEditor\javadgd\build\intermediates\pre-dexed\androidTest\debug\javawriter-2.1.1_cf45787cd93f38660e910b988d882dd18609ad89.jar}

I read about the 64k limit in android but I don't think it makes sense in my case. I don't have 64k method references in my app, nor do I have dependecies on libraries with 64k methods references(I think).

Here is my module-level build.gradle:

    apply plugin: 'com.android.library'

    android {
        compileSdkVersion 25
        buildToolsVersion "25.0.2"

        defaultConfig {
            minSdkVersion 19
            targetSdkVersion 25
            versionCode 1
            versionName "1.0"

            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }

    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        compile 'com.android.support:appcompat-v7:25.1.0'
        compile 'com.tom_roush:pdfbox-android:1.8.9.1'
        compile 'org.parceler:parceler:1.1.8'
        testCompile 'junit:junit:4.12'
        testCompile 'org.mockito:mockito-core:2.8.9'
    }

Project-level build.gradle file:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {

    repositories {
        maven { url "http://dl.bintray.com/shivasurya/materialsteppers" }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven { url "http://dl.bintray.com/populov/maven" }
        maven { url "https://jitpack.io" }
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

I'm trying run instrumentation test inside the androidTest directory, due to a need for the Context of the target application.

Does anybody have a clue why do I get this error?


Solution

  • Is caused because when you runt tests you need to use other dependencies, such as JUnit, Mockito, etc. Those are used only when you run tests and methods count are added with your project mothods count.

    In your gradle add this

    default config {
      ....
      multiDexEnabled true
    }
    

    And if minSdkVersion < 20 you need to support

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

    Create an application class

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

    And in your manifest you need to declare it

    <application
        android:name=".MyApplication" >
        ...
    </application>