Android 'dx' tool Android 'dx' tool android android

Android 'dx' tool


What is the 'dx' tool?

The dx tool converts Java class files into a *.dex (Dalvik executable)* file.

Where is it?

The dx.jar file was original located under android-sdk/platforms/android-X/tools/lib/ before (especially in Android 3 and Android 4) and was moved to android-sdk/platform-tools/lib/ later.

How does it fit in Android?

The Java source files are converted to Java class files by the Java compiler.

The dx tool converts Java class files into a *.dex (Dalvik executable)* file. All class files of the application are placed in this .dex file. During this conversion process redundant information in the class files are optimized in the .dex file.

For example, if the same String is found in different class files, the .dex file contains only one reference of this String.

These .dex files are therefore much smaller in size than the corresponding class files.

The .dex file and the resources of an Android project, e.g., the images and XML files, are packed into an .apk (Android Package) file.

To understand better, look at the Android build process:

Build process

FYI:

The program AAPT (Android Asset Packaging Tool) performs APK creation.The resulting .apk file contains all necessary data to run the Android application and can be deployed to an Android device via the ADB (Android device bridge) tool.

Reference


This is a special purpose flag that is only used when building some of the framework JAR files (core.jar, framework.jar, etc.). Normally, dx will refuse to process any java.* or javax.* classes. So this option is used for core.jar, where all those classes are actually defined.

Here's a relevant blurb from the dx source (dalvik/dx/src/com/android/dx/command/dexer/Main.java), that gets printed if you try to include a java.* or javax.* class in an application.

Ill-advised or mistaken usage of a core class (java.* or javax.*)when not building a core library.This is often due to inadvertently including a core library filein your application's project, when using an IDE (such asEclipse). If you are sure you're not intentionally defining acore class, then this is the most likely explanation of what'sgoing on.

However, you might actually be trying to define a class in a corenamespace, the source of which you may have taken, for example,from a non-Android virtual machine project. This will mostassuredly not work. At a minimum, it jeopardizes thecompatibility of your app with future versions of the platform.It is also often of questionable legality.

If you really intend to build a core library -- which is onlyappropriate as part of creating a full virtual machinedistribution, as opposed to compiling an application -- then usethe "--core-library" option to suppress this error message.If you go ahead and use "--core-library" but are in factbuilding an application, then be forewarned that your applicationwill still fail to build or run, at some point. Please beprepared for angry customers who find, for example, that yourapplication ceases to function once they upgrade their operatingsystem. You will be to blame for this problem.

If you are legitimately using some code that happens to be in acore package, then the easiest safe alternative you have is torepackage that code. That is, move the classes in question intoyour own package namespace. This means that they will never be inconflict with core system classes. JarJar is a tool that may helpyou in this endeavor. If you find that you cannot do this, thenthat is an indication that the path you are on will ultimatelylead to pain, suffering, grief, and lamentation.


The --core-library option on Dx will bypass the stupidity check that prevents you from accidentally including Java core libraries in your Android app.

Dx will barf if you try to include a library that contains packages in the java.* or javax.* namespace. The thinking is that classes in that namespace are likely to depend on other JDK "core" classes, which will break your app since they (may) not be present on Android.

Now, of course, just because a Java package starts with java.* or javax.* does not necessarily mean that it depends on the JDK proper. It may work perfectly fine in Android. The recommendation, if you know what you are doing, if you know that your java/x.* classes don't depend on JDK core classes, is to use a tool like JarJar to repackage the JAR file under a different namespace.

That being said, to get around the stupidity check, add the --core-library option to dx. Change the last line of $ANDROID_HOME/platform-tools/dx from,

exec java $javaOpts -jar "$jarpath" "$@"

to,

exec java $javaOpts -jar "$jarpath" --core-library "$@"

In my case, I was including a library that depended on Jackson, which depends on JAXB. For me, overriding the stupidity check was acceptable because the library's use of Jackson was only for JSON and not for XML serialization (I only include the JAXB API library, not the implementation). of course I wish there was a cleaner way to go about this, but rewriting the top-level library to avoid using Jackson was not an option.