简体   繁体   中英

What compiler does Android use and can I change it?

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 (eg the interning of string literals). Am I correct in assuming this?

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

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

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM