Android Internals Updated: Kernel
Android Internals Updated: Kernel
Vikash Kumar
Kernel
What is a kernel? A kernel is a computer program that is the core of a
computer’s operating system. It has complete control of everything inside of
the system. It’s the first program to be loaded on startup and it handles the
rest of the startup process. It handles the CPU, memory, manages input-
output devices, and communicates with other processes in the operating
system through system calls. System calls occur when other processes
want to do something that is hardware related.
Comments
The modifications on Android from the original kernel came from the fact
that Android runs on phones, which have limited memory, and is not always
connected to a power source.
Out of memory killer: this was created to help kill apps that users
leave running. Because Android also run on low-end devices, it must
make the effort clean up unused apps before it runs out of memory to
a catastrophic level.
Init
The next step, in our list of architecture is the init. The init process is a
binary that’s picked up by the kernel run on startup. It is also the root of all
other processes, which will be created from this process, spawning
everything else.
Daemons are processes that will run in the background. Some that might
look familiar to you are adbd or installd that’s being used to install apps.
1
Zygote
The Zygote is a special process; a special daemon. It’s the base of all other
Java or Kotlin based applications. It contains a readily available runtime
environment/virtual machine that’s ready to run the app.
When apps need to be able to interact with each other, they use Binder.
Binder
Binder allows inter-app communication in a safe way.
MyApplication.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
The result of the network information is coming from the system service.
You may wonder why the call cannot simply be in the app.
2
The reason is that it’s doing complex things such as asking the Wi-Fi driver
or asking other hardware components about their situation. If this was to be
handled internally, it would lack many of the permissions, so putting this in
a centralized service makes sense.
Suppose I encounter a bug, and want to see the source code of the above
method:
...
try {
return mService.getActiveNetworkInfo();
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
...
interface IConnectivityManager
3
{
Network getActiveNetwork();
NetworkInfo getActiveNetworkInfo();
NetworkInfo[] getAllNetorkInfo();
Network[] getAllNetworks();
NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int
userId);
We find an interface - it’s not actually not speaking directly to the activity
manager, but going through a Binder.
4
Our app is not directly talking to the other app. Instead, it’s going through a
journey down to the kernel, then up again to send our message with the
result.
public boolean transact(int code, Parcel data, Parcel reply, int flags)
The sender will call transact and the recipient will get onTransact, which
passes a code, a Parcel, which is all the data, and maybe the method
arguments that we wanted to send.
5
On the recipient side, we open these up to see what we got and figure out
what we want to do with it.
The AIDL tool comes in and it serves as the bridge between the actual
method and a transaction and it generates. It auto-generates a lot of code.
One is the proxy which is the sender and one is the stub which is the
recipient. This is an example of how the proxy would look.
android.net.Network _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getActiveNetwork, _data,
_reply, 0);
_reply.readException();
if ((0!=_reply.readInt())) {
_result =
android.net.Network.CREATOR.createFromParcel(_reply);
6
else {
_result = null;
finally {
_reply.recycle();
_data.recycle();
return _result;
In the middle of there is the transact method which is the actual Binder.
This is the real implementation:
7
System Server
The system server is the heart of Android, and it’s responsible for all things
Android. It’s one process, and it’s written in Java. It tells Zygote, to fork,
create the system server, and run the Java code on a new instance.
Activity Manager
Window Manager
Package Manager
8
The activities manager is the heart of the heart of Android. It is responsible
for everything: all the activities that we create along with the services, and
content providers are managed by the activities manager.
The main function of the activity manager service is to manage the lifecycle
of all our apps. The one top rule that they have could only be one resumed
app up at all times.
The activity manager is also responsible for telling the window manager to
create a surface for an app or remove a surface for an activity.