Chapter 3. Fundamental Building Blocks of Android Apps

This chapter gives an overview of Android app internals. It is essential to understand how apps are being built under the hood, what it looks like when it is installed on the device, how they are run, and so on. We make use of this knowledge in other chapters, where we discuss topics such as reverse engineering and pentesting Android apps. This chapter covers the following topics:

Every app that we download and install from the Play Store or any other source has the extension .apk. These APK files are compressed archive files, which contain other files and folders that we will discuss in a moment. Typically, the end users download these apps and install them by accepting the required permissions and then use them. Let's dive into the technical details such as what these apps contain and how they are actually packaged, what happens when we install them, and so on.

First let's start with the final binary that we use as an end user. As mentioned earlier, Android apps have the extension .APK (short for Android Application Package), which is an archive of various files and folders. This is typically what an end user or a penetration tester would get. Since an Android app is an archive file, we can uncompress it using any traditional extraction tool. The following diagram shows the folder structure of an uncompressed APK file. Universally, this is the same with any APK with some minor differences such as having an extra lib folder when there are additional libraries included in the app:

Android app structure

Steps to uncompress an APK file:

Let's see what each of these files/folders contain:

Depending upon who installed the app and what extra options are provided during the installation, there are different storage locations on Android devices. Let's look at each of them.

Apps that come with system image will be placed under this location. Let's look at the file permissions of the apps installed under this folder. The following excerpt shows that all these files are world readable and that anyone can copy them out without requiring additional privileges:

root@android:/system/app # ls -l *.apk

-rw-r--r-- root     root      1147434 2013-02-01 01:52 ATSFunctionTest.apk
-rw-r--r-- root     root         4675 2013-02-01 01:52 AccessoryKeyDispatcher.apk
-rw-r--r-- root     root        51595 2013-02-01 01:52 AddWidget.apk
-rw-r--r-- root     root        21568 2013-02-01 01:52 ApplicationsProvider.apk
-rw-r--r-- root     root         2856 2013-02-01 01:52 ArimaIllumination.apk
-rw-r--r-- root     root         7372 2013-02-01 01:52 AudioEffectService.apk
-rw-r--r-- root     root       147655 2013-02-01 01:52 BackupRestoreConfirmation.apk
-rw-r--r-- root     root       619609 2013-02-01 01:52 Bluetooth.apk
-rw-r--r-- root     root      5735427 2013-02-01 01:52 Books.apk
-rw-r--r-- root     root      2441128 2013-02-01 01:52 Browser.apk
-rw-r--r-- root     root        11847 2013-02-01 01:52 CABLService.apk
-rw-r--r-- root     root       200199 2013-02-01 01:52 Calculator.apk
-rw-r--r-- root     root        92263 2013-02-01 01:52 CalendarProvider.apk
-rw-r--r-- root     root         3345 2013-02-01 01:52 CameraExtensionPermission.apk
-rw-r--r-- root     root       141003 2013-02-01 01:52 CertInstaller.apk
-rw-r--r-- root     root       215780 2013-02-01 01:52 ChromeBookmarksSyncAdapter.apk
-rw-r--r-- root     root      7645090 2013-02-01 01:52 ChromeWithBrowser.apk
-rw-r--r-- root     root      1034453 2013-02-01 01:52 ClockWidgets.apk
-rw-r--r-- root     root      1213839 2013-02-01 01:52 ContactsImport.apk
-rw-r--r-- root     root      2100200 2013-02-01 01:52 Conversations.apk
-rw-r--r-- root     root       182403 2013-02-01 01:52 CredentialManagerService.apk
-rw-r--r-- root     root        12255 2013-02-01 01:52 CustomizationProvider.apk
-rw-r--r-- root     root        18081 2013-02-01 01:52 CustomizedApplicationInstaller.apk
-rw-r--r-- root     root        66178 2013-02-01 01:52 CustomizedSettings.apk
-rw-r--r-- root     root        11816 2013-02-01 01:52 DefaultCapabilities.apk
-rw-r--r-- root     root        10989 2013-02-01 01:52 DefaultContainerService.apk
-rw-r--r-- root     root       731338 2013-02-01 01:52 DeskClockGoogle.apk

Apps that require special copy protection on the device usually are under this folder. Users who do not have sufficient privileges cannot copy apps installed under this location. But, it is still possible to extract these APKs if we have root access on the device.

Now, let's see how we can extract an app of our choice from the device. This is essentially a three-step process:

Let's see it in action. The following examples are shown on a real Android device running Android 4.1.1.

Similar to the process with preinstalled apps, if we know the name of the app, we can use the following command to find the package name of the application installed by the user:

This time, I am looking for an app called heartrate that is installed from the Play Store. This can be downloaded from the following link in case you want to install it on your device:

https://play.google.com/store/apps/details?id=si.modula.android.instantheartrate&hl=en

Example of extracting user installed apps

Well, as we can see in the previous screenshot, we have got the package name. We can use the following command to find its APK path:

adb –d shell pm path [package name]
Example of extracting user installed apps

This APK is under the /data/app/ directory since it is a user installed application.

Finally, we can pull this app from the device using the following command similar to how we did previously with preinstalled apps:

adb –d pull /data/app/[file.apk]
Example of extracting user installed apps

Apart from the APK files, you may also notice .odex files if you navigate to the /system/app/ directory using the adb shell. These .odex files are optimized .dex files that are usually created on an apps first run. Creation of these .odex files is internally done using a tool called dexopt. This process improves app performance and it is usually done during the first start up process of Android OS.

When you do the preceding mentioned process on the latest version of an Android device, the location of these APK files are slightly different from what we have seen. The following is the specification of the emulator used to test this:

Example of extracting user installed apps

Each APK has got its own directory inside the path /data/app/ and /system/app/ for user installed apps and preinstalled apps respectively.

A sample location of a preinstalled app:

Example of extracting user installed apps

A sample location of a user installed app:

Example of extracting user installed apps

In this case, if you explore the file system using the adb shell, each .odex file that is associated with the app is placed inside the app's own directory shown in the previous screenshot rather than /system/app/.