Android Internals For Developers : Part I

What is Android? What do you mean by rooting an Android phone? Does an Android App developer need to know about Android system to become a professional?
In this two part series we are gonna answer some of these questions and also look into the intricacies of Android Operating System. Do note, this blog post is written from a developer’s perspective which means this post won’t go to into details of Android Operating System but will only explain what is necessary for an Android Developer. Pre-requisites are basic knowledge and commands of Linux Operating System, Android app development. If you’re a beginner in Android, then you might wanna skip this post and learn about Android App Development.
Part I : Android Internals For Developers
Part II : Deep Dive into Android Internals
First off, What is Android?

The image above explains something about Android. We see that inside the mascot, tux i.e Linux mascot is hiding. Well this is because Android is based on Linux Kernel. Most of the developers know this but if you don’t then kudos, you’ve learnt a new thing to show off!!
To answer the question, Android is a mobile operating system that is based on Linux Kernel. Since its based on Linux Kernel, most of the operations that I will mention might be heard of if you have worked in a POSIX styled OS i.e Mac/Linux. Then you should wonder if Android is based on Linux why should I read this post? That’s because Android Kernel is not exactly same as Linux Kernel. It was modified by developers to make it a mobile OS rather than a desktop one.
Why do you need to modify? That’s because in a desktop you(user) can kill processes easily or can use virtual memory and many other things. But in a mobile OS the memory is limited. Without proper memory management, mobiles will get hanged and you won’t even get important things like calls or reminders because of that.
Android Kernel

The latest Android 8.0 Oreo was built on Linux Kernel 4.4. But I want to emphasise that Android is not a Linux distribution. In this section we are gonna look into some of the modifications Google put into Linux Kernel.
OOM Killer
(Out Of Memory) : This is not a new feature introduced by Google. Out of memory (OOM) is an often undesired state of computer operation where no additional memory can be allocated for use by programs or the operating system. This was modified by Google to make it more suitable for mobile Operating System. Let’s go the belly of the beast.
Android, as you know has many background processes. Each time you press the home button, the app is placed in background process. So how do you then kill the process when system is near to OOM?
Android modified the Linux OOM to assign importance to each process i.e visible, hidden etc. Each process gets aoom_adj
value according to its importance.
The updation ofoom_adj
is done by the Activity Manager(AM) dynamically. Then the kernel low memory killer will kill processes with regards to itsoom_adj
. A process with higheroom_adj
value is more likely to get killed. The low memory killer is configured with several threshold pairs: (<t1, oom_adj1>
,<t2, oom_adj2>
… ). These parameters mean that the killer will starts to kill processes withoom_adj
larger thanoom_adj1
when the RAM drops belowt1
.
The question is why do we need all of this whatever it is ? The answer is because with this knowledge we’re gonna look at who lives and who dies in Android.
- Foreground Processes : You’d think what the user is currently interacting with would be the most important thing to keep alive and you’d be completely right. Any process hosting a service bound to the foreground activity is given the same foreground priority.
That’s why use foreground service whenever its only necessary like I wouldn’t want my music playing to stop when I click home button. Cases like this are best examples for foreground services.
Note: Being a foreground service requires that your service include a notification to ensure that the user is completely aware that your service is running. If you don’t feel a notification is needed for your use case, then perhaps a foreground service isn’t right for you.
- Visible Processes : I am not kidding!! Its not same as Foreground process. Your activity can be visible but not in the foreground. A simple example is when the foreground activity starts a new activity with Dialog theme or a translucent activity. The following thing was the best thing I found on the web for developers about Visible Developers.
Keep in mind though, just because you are visible does not mean you can’t be killed. If there is enough memory pressure from foreground processes, it is still possible that your visible process will be killed. From a user perspective, this means the visible activity behind the current activity is replaced with a black screen. Of course, if you’re properly recreating your activity, your process and Activity will be restored as soon as the foreground Activity is closed without any loss of data.
- Service Process: If your process didn’t fall in anyone of the above process it means that you are using Service process. This is the case in many apps where they’re doing background tasks.This isn’t a bad place to be! For the vast majority of cases, this is the perfect place for background processing as it ensures that your process is killed only if there is so much happening in the above visible and foreground process categories that something has got to give. Take a note of constants that are used in
onStartCommand()
as it controls what happens to your service if it was killed due memory pressure from other processes. - Background Process : Let’s say your Activity was the foreground Activity, but the user just hit the home button causing your onStop() to be called. Assuming that was all that was keeping you higher priority categories, your process will fall into the background process category.
Remember Android doesn’t kill simply apps that are there in background. It kills when there is memory requirement. Thats the role ofOOM KILLER
that we talked above.However, same as the visible activities when they are killed, you should be able to recreate your activity at any time without losing the user’s state.
Wakelocks
This is the second major modification that Android has done on Linux Kernel. Wakelocks are power-managing software mechanisms, which make sure that your Android device doesn’t go into deep sleep.

Although wakelocks on Android are not necessarily a bad thing, minimising these should be the priority of any Android power user who’s on the hunt for better battery life.
Unfortunately, some poorly-coded, malicious, or simply buggy apps might create an abnormal amount of undesirable wakelocks. Other apps require constant Internet access in order to operate in a normal fashion — Facebook and Messenger are probably the most popular representatives. They persistently request information from the web , which is causing subsequent wakelocks and thats why tend to consume battery a lot when you keep the app running and click the power button to lock the screen.
Conclusion (Part I)
All right!! That was too much to take in!! Take time and read it again until it makes sense. If you have any suggestions or anything that needs to be clarified just drop a comment.
What we’ll be doing in next part???
In the next part we will learn about some important components of Android like init
and zygote
. Also next post will contain the answer to what do you mean by rooting the phone. So wait for it!!
PS: Clap if you like the post and bookmark it so that you can find the link for second part.
See ya in the next post!!