Wednesday, May 16, 2012

3. Android Development Tools

3.1. What are the Android Development Tools?

Google provides the Android Development Tools (ADT) to develop Android applications with Eclipse. ADT is a set of components (plug-ins) which extend the Eclipse IDE with Android development capabilities.
ADT contains all required functionalities to create, compile, debug and deploy Android applications from the Eclipse IDE and from the command line. Other IDE's, e.g. IntellJ, are also reusing components of ADT.
ADT also provides an Android device emulator, so that Android applications can be tested without a real Android phone.

3.2. Dalvik Virtual Machine

The Android system uses a special virtual machine, i.e. the Dalvik Virtual Machine to run Java based applications. Dalvik uses an own bytecode format which is different from Java bytecode.
Therefore you cannot directly run Java class files on Android, they need to get converted in the Dalvik bytecode format.

3.3. How to develop Android Applications

Android applications are primarily written in the Java programming language. The Java source files are converted to Java class files by the Java compiler.
Android provides a tool called "dx"" which converts Java class files into a dex (Dalvik Executable) file. All class files of one application are placed in one compressed .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 once 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. The program aapt (Android Asset Packaging Tool) performs this packaging.
The resulting .apk file contains all necessary data to run the Android application and can be deployed to an Android device via the "adb" tool.
The Android Development Tools (ADT) allows that all these steps are performed transparently to the user; either within Eclipse or via the command line.
If you use the ADT tooling you press a button or run a script and the whole Android application (.apk file) will be created and deployed.

3.4. Resource editors

The ADT allows the developer to define certain artifacts, e.g. Strings and layout files, in two ways: via a rich editor, and directly via XML. This is done via multi-page editors in Eclipse. In these editors you can switch between both representations by clicking on the tab on the lower part of the screen.
For example if you open the "res/layout/main.xml" file in the Package Explorer, you can switch between the two representations as depicted in the following screenshot.
ADT Resource Editor

3.5. Logging

Android uses the android.util.Log class for logging with the Log.i(), Log.w() and Log.e() and Log.wtf() methods for logging. This list is sorted by severence.
The first parameter of these method is the category and the second is the message.
Typically you create a Constants class in your Android application and provide your log flag as a public static final field.
    
package de.vogella.android.first;

public class Constants {
 public static final String LOG = "de.vogella.android.first";
}

   
Android advises that a deployed application should not contain logging code. The Android development tools provide the BuildConfig.DEBUG flag for this purpose. This flag will be automatically set to false, if you export the Android application for deployment. During development it will be set to true, therefore allow you to see your logging statements during development.
The following example show how to write an error log message. This message is visible in the "LogCat" view in Eclipse.
    
if (BuildConfig.DEBUG) {
 Log.e(Constants.LOG, "onCreate called");
}
   

No comments:

Post a Comment