Build Tools in Android Studio 1.0

Introduction

A build is a major part in the development of any project. Depending on the wiki, a build has often referred to either as the process of converting source code files into a standalone software artifact(s) that can be run on a computer, or the result of doing so.

However, this is not the case with technologies such as Perl, Ruby or Python that are interpreted languages. Therefore, it is better to define build management as the facilitation of the build process. Basically a build is a combination of processes that includes precompilation, compilation, cleaning and linking and loading as well.

However in Android Studio there are very rich and intelligent build tools such as:

  • JOBB
  • APK file expansion 
  • ProGuard 
JOBB

The job tool does the job of security that allows you to build encrypted and unencrypted APK expansion files in Opaque Binary Blob (OBB) format. We can download and mount these expansion files in the application using StorageManager on devices with Android 2.3 (API Level 9) or higher. OBB files are used to provide additional file assets for Android applications (such as graphics, sounds and video), separate from an application's APK file.

Usage

The syntax is somehow different. Look at the following command:
  1. jobb [-d <directory>][-o <filename>][-pn <package>][-pv <version>] \  
  2.         [-k <key>][-ov][-dump <filename>][-v][-about]  
using command prompt

Type this command on the command prompt and press Enter.
  1. $ jobb -d /temp/assets/ -o my-app-assets.obb -k secret-key -pn com.my.app.package -pv 11 
This example shows how to dump (extract) the contents of an existing OBB file:

$ jobb -d /temp/obb-output/ -o my-app-assets.obb -k secret-key

ProGuard

This tools is mainly responsible for optimization. It does various useful tasks, such as optimizing, shrinking and obfuscating the code by removing unused code and renaming classes. it also helps to optimize unused fields and methods with obscured names but semantically. However in the Android system the output and the file we get after running the application is an .apk that is very difficult to reverse-engineer because Proguard makes it more secure.

Proguard is integrated into Android build system such as Gradle, the new Android build system. So we don't need to invoke it explicitly. It works only in release mode.

Enabling ProGuard

When we create the project and build it then an auto-generated file is generated with the name Proguard.cfg generated in the root folder or directory.

To enable the ProGuard so that it could run as part of an Ant tool or Eclipse build, set the proguard.config property in the <project_root>project.properties file. The path can be an absolute path or a path relative to the project's root regarding the project.

For the Android Studio1.0 add Proguard to your gradle.build file's build types.

Giving an absolute path to the  proguard file:

proguard.config=/path/to/proguard.cfg

When a build of the project takes place in the Android using an IDE it could be run as the "ant release" or by using the EXPORT WIZARD in the Eclipse IDE however the build system using these config files are automatic and during the bytecode verification these files are embedded into the apk file.
  • dump.txt 

    describes the internal structure of all the class files in the .apk file.
  • mapping.txt

    it lists the mapping between the original and obfuscated class, method and field names. This file is important when you receive a bug report from a release build, because it translates the obfuscated stack trace back to the original class, method and member names.
  • seeds.txt

    it lists the classes and members that are not obfuscated.
  • usage.txt

    it lists the code that was stripped from the .apk.
Decoding the Obfuscated Stack Trace

When your obfuscated code is generated and creates a stack trace, these method names are obfuscated that makes project debugging laboureous to the framework. It can be regarded as being fortunate because whenever ProGuard runs, it outputs a <project_root>/bin/proguard/mapping.txt file that shows you the original classes, methods and field names mapped to their obfuscated names.

Let us consider the retrace.bat script on Windows or the retrace.sh script on the Linux or Mac operating systems that can convert an obfuscated stack trace to a readable one. It is located in the <sdk_root>/tools/proguard/ directory. The syntax for executing the retrace tool is:

retrace.bat|retrace.sh [-verbose] mapping.txt [<stacktrace_file>]

 

For example: retrace.bat -verbose mapping.txt obfuscated_trace.txt

If we do not specify a value for <stacktrace_file>, the retrace tool reads from standard input that is quite automatic.

Zipalign

The tool zipalign is an archive alignment tool that provides important optimization to Android application (.apk) files. Basically the purpose is to ensure that all uncompressed data starts with a specific alignment relative to the start of the file. Moreover, it causes all unzipped or uncompressed  text within the .apk, such as pictures or raw files, to be tied with the 4 byte addressable memory. This allows all portions to be accessed directly with the method mmap() executing at the core of Android even if they contain binary data with alignment restrictions. The major achievement for this is reducing the RAM consumption.

However this tool manages the distribution of APK files to the end user. When using the Eclipse IDE the Export wizards do this thing, zapling, to your apk after it signs it with your private key. Basically the zipalign must be done after the .apk file has been signed with your private key.  

Usage:

To align infile.apk and save it as outfile.apk:

zipalign [-f] [-v] <alignment> infile.apk outfile.apk

To confirm the alignment of existing.apk:

zipalign -c -v <alignment> existing.apk

APK Expansion Files

Google Play currently requires that an apk file be more than 50MB. However we can see that many applications on the Play Store is more than 50 MB because the size increases due to the graphics and other graphical properties that gives the user a nice user experience.

So in these cases Google suggests that the user must host and download the additional resources yourself when the user opens the application but the hosting and and servicing generates extra costs to it.

Each time you upload an APK using the developer's Google console you have the option to add one or two expansion files.

  • The main expansion file is the primary expansion file for additional resources required by the application.

  • The patch expansion file is optional and intended for small updates to the main expansion file. 

Summary

This article explains the basic tools mainly related to the Builds in the Android Studio 1.0 that are very necessary to understand the type of processing executing in the background.

Up Next
    Ebook Download
    View all
    Learn
    View all