Building the APK File

1. Code Compilation

The android application source files are written in either Java(*.java files) or Kotlin(*.kt files) programming languages. Syntax of writing the code in these 2 languages are different but their compilation process is almost the same. Both programming languages generate code that can be compiled to Java byte-code which is executable on JVM(Java Virtual Machine). In an android environment, the process begins with the compilation of Java/Kotlin source code into the Java class file. The class files have the extension *.class and it contains the java byte-code(represents Java assembly). This compilation task is carried out by the javac and kotlinc compilers for the Java and Kotlin language code respectively. 

2. Conversion into Dalvik bytecodes

The generated Java class(*.class) file in the previous step is executable on Java Virtual Machine(JVM) as it contains the standard Oracle JVM Java byte-codes. However, this code format is not suitable for Android devices and thus Android has its own unique byte-code format known as Dalvik byte-code. Dex compiler translates the Java byte-code into the Dalvik byte-code that are machine-code instructions for a theoretical processor. During the compilation process, dx command ties up all the .class files as well as .jar files together and creates a single classes.dex file that is written in Dalvik byte-code format. This file is now executable on the virtual machine in the Android operating system known as Android Runtime(or Dalvik Virtual Machine(DVM) for android version older than Kitkat(4.4)).

Java regular code:

public int addTwoNumbers(int a, int b) {

   return a+ b;

}

Equivalent Java byte-code: 

public int addTwoNumbers(int, int);

   Code:

      0: iload_1

      1: iload_2

      2: iadd

      3: ireturn

 Equivalent Dalvik byte-code:

.method public addTwoNumbers(II)I

   .registers 4

   .param p1, “a”    # I

   .param p2, “b”    # I

   .line 6

   add-int v0, p1, p2

   return v0

.end method

3. Generating .apk file

Resource files of the android application like images, fonts, XML layouts, etc. are transformed into a single compiled resource unit by the Android Asset Packaging Tool(aapt). The aapt tool is also responsible for creating the R.java file of an android application. Further, the compiled resource unit along with the classes.dex file is compressed by the apkbuilder tool and a zip-like file is created that is termed as Android Package(.apk file). The generated .apk file contains all necessary data to run the Android application. 

4. App Distribution

The .apk file generated in the previous step is a ready-to-use application package and developers can use this file for the purpose of app distribution. However, to distribute and publish the application through Google Play Store, developers need to sign it. Android applications are required to be digitally signed with a certificate so that they can be installed by the users. The certificate is self-signed, and the Android uses it to identify the author of the application. The app developer/author holds the private key of the certificate and these all details are stored as an additional file in the android package(.apk file). 

Oracle Java Development Kit(JDK) provides the jarsigner tool to sign the .jar files and .apk files. Further, the compressed parts of the signed .apk file are required to line up on byte-boundaries in such a manner so that Android OS can read them without uncompressing the file. The byte alignment of files is assured by running the signed .apk file through the zipalign tool.

How Does Android App Work?

Developing an android application involves several processes that happen in a sequential manner. After writing the source code files, when developers click the Run button on the Android studio, plenty of operations and process starts at the backend. Every operation happening in the background is a crucial step and are interdependent. The IDE builds all the application files, make them device compatible in order to deploy, and assure that the application runs successfully on the device. This article broadly explains each and every critical step involved in the journey of an android app, from the IDE files to a working device application.   

Similar Reads

Step 1: Building the APK File

1. Code Compilation...

Step 2: Deploy the Application

1. Establish the ADB Server...

Step 3: Run the Application

1. App launch request...

Contact Us