2

When I compile an Android project within Eclipse, it doesn't generate any Ant scripts or anything, so I assume it's using the Eclipse compiler to generate the .class files, and using dx to translate those into .dex. So that's fine, and I'm aware that the Eclipse compiler is different from javac and can't be changed.

But when I use android to generate a build.xml file, I see that it references other Ant files, and eventually I am able to track down the actual invocation to javac. So I have a few questions about this.

  1. I'm an Ant newbie. Does the <javac> Ant command correspond to the javac referenced by my JAVA_HOME env variable? Or is it something else?
  2. When you use the 'Export Application' option in Eclipse, does it still use the Eclipse compiler? Will I always be forced to use a different compiler in Ant vs. in Eclipse?
  3. Can I specify any compiler I want if I alter the build.xml file and change the "compile" target?

I don't really care that much what compiler I'm using, I'm just curious. The main thing I'm after is to make sure that I'm using a modern compiler and I can expect all the usual optimizations (e.g. the interning of string literals). Am I correct in assuming this?

P.S. Speaking of optimizations, does dx do anything more fancy than simply translating one bytecode to another?

Neil Traft
  • 18,367
  • 15
  • 63
  • 70

3 Answers3

3

To answer question 1, Yes it must be using the one referenced by JAVA_HOME. The evidence being: I can successfully build projects with Ant, my JAVA_HOME is set to c:\dev\tools\JDK6_20. My path includes c:\dev\tools\JDK6_20\bin.

I temporarily set JAVA_HOME to the non existant c:\dev\tools\JDK6_201. When I try an ANt build, I get:

BUILD FAILED C:\dev\projects\Eclipse\AndroidWorkspace\MapProject48\build.xml:390: Unable to find a javac compiler; com.sun.tools.javac.Main is not on the classpath. Perhaps JAVA_HOME does not point to the JDK. It is currently set to "C:\dev\tools\JRE\6_20_64bit"

I don't know where it gets the last line from, I guess it must be my JRE as it appears in the registry.

I think that in answer to your bold question : the compiler is as modern as the javac in your JDK

NickT
  • 23,844
  • 11
  • 78
  • 121
2

With regard to your question about dx, it's not the end of the process. When the application is installed on the device, a lot of local lookups are done to create an optimized dex (ODEX) file unique to the run environment of that device.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • Interesting! I think I remember Dan Bornstein mentioning that in his Google I/O Dalvik talk. Do you know any details about what it does? – Neil Traft Nov 23 '10 at 23:59
  • 1
    There's a lot of documentation in the Android source tree: http://android.git.kernel.org/?p=platform/dalvik.git;a=tree;f=docs;h=b0d3023109f548626cce1a9a586c95e82fb012ac;hb=HEAD . Try "dex-format.html" and "dexopt.html" (the former looks better if you grab it and the corresponding CSS file and view them locally). – fadden Nov 24 '10 at 00:06
1

I'm not 100% sure what you are getting at. However, Android uses a modified form of Java called Dalvik.

Dalvik is register based, which is better for mobile devices.

http://en.wikipedia.org/wiki/Dalvik_(software)

Knossos
  • 15,802
  • 10
  • 54
  • 91
  • 2
    The source files are first compiled to java byte code. As far as I know that's done with whatever JDK you installed, not by eclipse itself. The java byte code is then converted to Dalvik byte code by android sdk tools (probably dx) – Chris Stratton Nov 23 '10 at 18:25
  • It's usual practice to set environment variables of %JAVA_HOME% ant %ANT_HOME% and add %JAVA_HOME%\bin and %ANT_HOME%\bin to your path. As far as I know there's no separate 'javac' for Eclipse. It's just an IDE that supports many languages. You set %JAVA_HOME% to your JDK not the JRE – NickT Nov 23 '10 at 18:31
  • @NickT: Wrong. Eclipse has its own compiler. See [this SO question](http://stackoverflow.com/questions/3061654/what-is-the-difference-between-javac-and-the-eclipse-compiler). – Neil Traft Nov 23 '10 at 19:37
  • @Neil, Thank you for correcting me. So I guess it must keep up to date with the JDK features when additions like generics are added? – NickT Nov 23 '10 at 19:52
  • Yes, it currently has compliance with the Java Language Specification up to 1.6, and you can configure it to use an older specification (down to 1.3) if need be. – Neil Traft Nov 23 '10 at 20:00