FORUMS
Remove All Ads from XDA

[Newbies][Documenation]What's an apk and how does it work

922 posts
Thanks Meter: 554
 
By RaiZProduction, Senior Member on 14th July 2019, 02:44 PM
Post Reply Email Thread
Quick Intro :
Let me answer that question, an apk, first of all, is the format of Android apps, as .exe would be for Windows, apk means Android Package by the way.
But what’s the big deal?
Well an apk contain a lot of things into it.


When you open it as an archive, you might get those files:


AndroidManifest.xml
assets
res
META-INF
resources.arsc
classes.dex

AndroidManifest.xml is the file that contains the information about the app itself: the list of all activities, all permissions, the name of the app’s package, and the software/hardware requirements. To be able to read it, you’ll need to decompile the apk file.

Assets and res folders are meant for resources. Most often, you won’t see the assets folder because its usage is limited, (explanations here). We’ll focus on res since asset isn’t used so often.

META-INF contains CERT.RSA and CERT.SF (certificate) as well as MANIFEST.MF (a manifest)
Those files are the app signature, and META-INF is the folder that contains signature.

The resources.arsc file is the file holding most of the resources that were originally in the res folder. After compiling the code, Android Studio (or others software) compile resources into this file. You won’t be able to open it with an archive explorer. We’ll need to decompile the apk to get the resources back into the res folder.

And, finally, the classes.dex file contains the every smali files of the app. A smali file contains the java code for a specific part of the app. To be able to have the smali files (into the smali folder), you’ll have to baksmali your app.
Hopefully, apktool (a reverse engineering tool) does it for you.

Let’s decompile the apk using apktool (a popular tool used to decompile apps):

Once apktool is installed (see the apktool web page to know how to install), you’ll need to use the command prompt to decompile the apk:
Code:
java -jar apktool.jar d [name of the apk].apk
(I advise to create a folder and to put every file needed in it: the apk, the apktool.jar file, and the apktool script).

You’ll have an out folder, and when you browse it, you’ll have most of the time:
AndroidManifest.xml
original (a folder)
res (a folder)
smali (a folder)

The AndroidManifest.xml that you see here is the translated one.
In the “original” folder rest the META-INF folder and the original AndroidManifest.xml file (untranslated). As you can guess by its name, this folder contains unchanged original files.

The res folder is now full, because every resource from the resources.arsc file are now back into the res folder

Same for the smali folder, this folder contains every piece of code contained into the classes.dex file.

We will now focus on the res folder. Now that it’s complete, we can see those folders (it depends on your apk, but here is the list of the most common ones):
anim (other animation stuff, see the links at the end of the thread)
animator (animation properties)
color (color state list: in which condition this color is used for a particular element)
drawable (can be pictures or xml files, it contains a lot of things)
layout (contains the files that define the layouts of the app)
mipmap (icons for different screen densities)
values (xml files that contains values, that will be referenced in other xml files in other folders)
xml (every other unclassified xml files)

Those folders contain files that will be named following their resource ID.

So how does all of this work together?

Well, the java code written in the smali files will call resources with their IDs. This way, the app will show you the write thing at the right moment, following the code. The layouts are already described in the layout folder. What the code really do is analyzing the action of the end user and calling other resources in consequence.
For example, I’m using the XDA Labs app on my phone. I’m in my phone’s section, and I’m about to click on a thread to open it.

I clicked on the thread, so the code loaded the thread layout with every post in it, and other resources such as the little arrows at the top to jump to the end/beginning of the thread, the reply button, its color, … That’s how to work if I want to keep it simple.
I tried, in this guide, to simplify android app basic for beginners. Feel free to ask any question, I’ll be glad to answer.
If you want to go further into it, you can read a very complete guide about app basics by google (it’s a bit complicated to read, assuming you’re just starting to understand).

Here are the links:
https://developer.android.com/guide

If you have any question about that guide, you can also ask me (with the quote you didn’t understood).
Keep at mind that mastering app basics is really important if you ever want to theme/create an app. So, this guide and the google one is a good place to start.
Have fun reading
The Following 2 Users Say Thank You to RaiZProduction For This Useful Post: [ View ] Gift RaiZProduction Ad-Free
 
 
Post Reply Subscribe to Thread

Tags
apks, apktool, how does an apk work

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes