Solution: Access to the dex task is now impossible, starting with 1.4.0

Home / Android / Solution: Access to the dex task is now impossible, starting with 1.4.0

Everybody* who wants to upgrade to and above 1.4.0 build tool version is getting this error.

Error:Access to the dex task is now impossible, starting with 1.4.0
1.4.0 introduces a new Transform API allowing manipulation of the .class files.
See more information: http://tools.android.com/tech-docs/new-build-system/transform-api

Error:Access to the dex task is now impossible, starting with 1.4.0
1.4.0 introduces a new Transform API allowing manipulation of the .class files.
See more information: http://tools.android.com/tech-docs/new-build-system/transform-api

As you can see by visiting the new build system: transform api that there is no solution provided.

Follow the steps to solve the problem Access to the dex task is now impossible:

1. Update all the libraries (dependencies from Google Play Services and Support libraries along with other SDKs used) to the max available version. If you are using NewRelic, then update to the latest version.
2. Add multiDexEnabled true to the defaultConfig in /app/build.gradle.

defaultConfig {
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 1
    multiDexEnabled true
}




3. Remove all the code you have previously written to get MultiDexing to work.
For instance,

afterEvaluate {
    tasks.matching {
        it.name.startsWith('dex')
    }.each { dx ->
        if (dx.additionalParameters == null) {
            dx.additionalParameters = []
        }
        dx.additionalParameters += '--multi-dex'

        // this is optional
        //  dx.additionalParameters += "--main-dex-list=$projectDir/multidex-classes.txt".toString()
    }
}
dependencies {
  ...
  compile 'com.android.support:multidex:1.0.1'
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pcsalt.multidex">
    <application
        ...
        android:name="android.support.multidex.MultiDexApplication">
        ...
    </application>
</manifest>

If you are using custom Application class by extending android.app.Application, then remove MultiDex.install(base);

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

4. Update the build tool version to 2.1.2 in <project>/build.gradle

classpath 'com.android.tools.build:gradle:2.1.2'

5. Gradle Sync the project, by clicking Sync now

*Those who have crossed the 64K method limit for dex compilation. Solution is known as MultiDex.