How does the Android compiler work? Does it remove the comments in Java code and Android XML files on compilation?
Comments in Java files are removed, or ignored, when they are compiled into class files. Class files are not for human-reading - they are optimized to run efficiently on a virtual machine. If the comments were to remain, they would just cause the class files to be larger than required, and for no additional benefit (ie, the compiler and virtual machines don't understand comments, so why include them)
As for XML files, the comments are usually retained. Whereas a compiled class file only has 1 purpose (to run on a virtual machine), an XML file could serve many purposes. For example, you can load an XML file into your application and manipulate it. Depending on the use of the XML file in your application, it might be required to keep the comments in there - in fact, your application might specifically look for comments. Because XML files could have a wide range of uses, the comments aren't removed from XML files in case they are required for some other purpose.
Comments are not present in the byte code/.class version of the file..comments are ignored by the compiler at the time of compilation..
A compiler works in a way that it converts all the High Level Language code to machine understandable Code..all at once..not line by line like a interpreter.. in case of Android .. the compiler converts the .java source files into its bytecode/.class files..and then that to .dex files (Dalvik Executables) .. All applications in Android run on top of a Virtual Machine called Dalvik Virtual Machine that needs .dex files ..
Try opening up a apk file like a zip .. using any archiver..you would see the classes.dex file in there..
And as far as the xml files are concerned .. they are used by the application only when you are displaying something on screen (i.e Activity being rendered)..so at the time of activity being displayed..its corresponding xml file is used to construct the UI.. this process is called inflating!
EDIT: [But 1 point i remember now regarding this is that..the system applications which are present in an Android Phone..such as all Google provided Apps(Gmail, Maps etc)..the Contacts, Phone.apk etc..all those are system apks..present in /system/app/ directory..they may or may not contain the .dex files in them..
if they contain the .dex inside the apk files (just like any normal app we develop)..then the ROM is called as de-odexed ROM (the firmware of Android OS) .. but this in turn results in slower booting up when the ROM is installed for 1st time due to copying of all the .dex files from system .apk files to a dalvik cache..since all the .dex files of system apks are needed at system startup/boot..
But if the ROM is odexed .. all the .dex files of the system .apk's are not present inside the .apk but as a seperate file in /system/app/ directory .. with an extension .odex
For all apps you and me develop (non-system apps)..just apps of play store..those do contain the .dex inside the .apk ]
the EDIT: i know its not very much related to working or the question .. but just wanted to mention the difference of placement of .dex files for sys and non-sys apps! :)
Hope it helped! :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With