Does there exist a universal or "fat" class file for Java?

644 Views Asked by At

A fat binary is a binary that can be run on more than one architecture. Basically, it consists of a program compiled twice, once for each architecture, then written to the same file. Probably the best known example is Apple's "universal" binaries, allowing programs to be compiled for both Intel and Power PC architectures, and run from the same executable file.

This was never an issue for Java, since Java runs on the JVM, allowing it to be run from any JVM-supported computer. However, Android is now very popular, and Android's VM (Dalvik), is not compatible with the JVM. Is there some way of compiling the code twice, and creating a class file that can be executed by both the JVM and Dalvik? And if not, is this even possible?

1

There are 1 best solutions below

0
On BEST ANSWER

Answer: Yes.

You can create a universal .jar file that contains both JVM-friendly .class files and an Android-friendly classes.dex file. The dx tool included in the Android SDK will emit such files if you use the --keep-classes command line option.

Note that although such .jar files can be consumed on JVMs and on Android, packaging code in this way is not very useful. Android applications are packaged as .apk files include an Android manifest XML file. They use Android-specific APIs like Activity that are not available on the JVM.

A universal .jar file would mostly be useful if you wanted to do runtime class loading of a library.