Build legacy android app DEX error problem solving

1. Introduction

When build a legacy android app, you may encounter this problem:

2.1 The problem

Error:Android Dex: [xxx] Failed to load dx.jar
Error:Android Dex: [xxx] java.lang.ClassNotFoundException: com.android.dx.command.DxConsole
Error:Android Dex: [xxx] at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
Error:Android Dex: [xxx] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
Error:Android Dex: [xxx] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
Error:Android Dex: [xxx] at org.jetbrains.android.compiler.tools.AndroidDxRunner.loadDex(AndroidDxRunner.java:80)
Error:Android Dex: [xxx] at org.jetbrains.android.compiler.tools.AndroidDxRunner.runDex(AndroidDxRunner.java:136)
Error:Android Dex: [xxx] at org.jetbrains.android.compiler.tools.AndroidDxRunner.main(AndroidDxRunner.java:336)
Error:Android Dex: [xxx] Exception in thread "main" java.lang.NullPointerException
Error:Android Dex: [xxx] at org.jetbrains.android.compiler.tools.AndroidDxRunner.runDex(AndroidDxRunner.java:139)

2.2 The reason of this problem

Your android SDK build tools is not compatible with your current android app codes.

2.3 Solutions

There are two solutions to solve this problem

2.3.1 Solution 1: Downgrade your android build SDK tools

  • Open your android studio
  • Click the SDK manager
  • Show package details of the SDK build tools
  • Uncheck the build tools which version’s greater then 25.0.3 downgrade_build_tools

2.3.2 Solution 2: Copy the dex jars to your build tool directory

For example, if you are using build-tools 27.0.1, you can do as follows:

  • goto android-sdk/build-tools/25.0.3/lib
  • copy the files in the lib to android-sdk/build-tools/27.0.1/lib

3. Summary

This problem only occurs when you are building an old APP, so it’s time to upgrade your app code.