Android 'dx' tool
Asked Answered
U

3

24

Is there any documentation for 'dx'?

In particular, I am interested in knowing what the --core-library option does.

Umbles answered 13/12, 2011 at 10:2 Comment(0)
B
10

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 file in your application's project, when using an IDE (such as Eclipse). If you are sure you're not intentionally defining a core class, then this is the most likely explanation of what's going on.

However, you might actually be trying to define a class in a core namespace, the source of which you may have taken, for example, from a non-Android virtual machine project. This will most assuredly not work. At a minimum, it jeopardizes the compatibility 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 only appropriate as part of creating a full virtual machine distribution, as opposed to compiling an application -- then use the "--core-library" option to suppress this error message. If you go ahead and use "--core-library" but are in fact building an application, then be forewarned that your application will still fail to build or run, at some point. Please be prepared for angry customers who find, for example, that your application ceases to function once they upgrade their operating system. You will be to blame for this problem.

If you are legitimately using some code that happens to be in a core package, then the easiest safe alternative you have is to repackage that code. That is, move the classes in question into your own package namespace. This means that they will never be in conflict with core system classes. JarJar is a tool that may help you in this endeavor. If you find that you cannot do this, then that is an indication that the path you are on will ultimately lead to pain, suffering, grief, and lamentation.

Berns answered 13/12, 2011 at 18:35 Comment(0)
C
39

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

Consequently answered 29/5, 2014 at 15:24 Comment(2)
So, finally is it apkbuilder or aapt that creates the APK file? If it is aapt, the diagram would be wrong.Kenlay
But it seems to be "dex" in the picture. Perhaps address the discrepancy in your answer? For instance, is "Dex" or "dex" the real name while "dx" is the name of the executable? Perhaps also provide an authoritative link to "dx", like a home page for it (or similar)?Mal
B
10

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 file in your application's project, when using an IDE (such as Eclipse). If you are sure you're not intentionally defining a core class, then this is the most likely explanation of what's going on.

However, you might actually be trying to define a class in a core namespace, the source of which you may have taken, for example, from a non-Android virtual machine project. This will most assuredly not work. At a minimum, it jeopardizes the compatibility 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 only appropriate as part of creating a full virtual machine distribution, as opposed to compiling an application -- then use the "--core-library" option to suppress this error message. If you go ahead and use "--core-library" but are in fact building an application, then be forewarned that your application will still fail to build or run, at some point. Please be prepared for angry customers who find, for example, that your application ceases to function once they upgrade their operating system. You will be to blame for this problem.

If you are legitimately using some code that happens to be in a core package, then the easiest safe alternative you have is to repackage that code. That is, move the classes in question into your own package namespace. This means that they will never be in conflict with core system classes. JarJar is a tool that may help you in this endeavor. If you find that you cannot do this, then that is an indication that the path you are on will ultimately lead to pain, suffering, grief, and lamentation.

Berns answered 13/12, 2011 at 18:35 Comment(0)
T
4

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.

Topnotch answered 18/6, 2012 at 20:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.