Main method of Android application
Hello! I'm Sasha, senior Android developer at Avito, previously worked at Yandex and Kaspersky. I write about development and everything connected with it in on your telegram channel. Now let's talk a little about the main method and the process of the Android application.
If you've ever written “Hello World” in Java (or any other C-like language), you've probably done it in a static main method. This method is called in the JVM and is the entry point of any Java program.
public static void main(String[] args)
But when developing applications for Android, you may, like me until recently, never encountered the main method. It feels like it doesn’t exist at all, and when you click on the icon in the launcher, our Application and Activity are created by some magical gnomes hidden in the device.
In fact, there is still a main method in Android, and it is contained in the ActivityThread class. But let's take things in order.
ActivityThread
What kind of animal is this? Does each Activity really run on a special thread? Of course not, the Android SDK developers simply decided to call the main thread of the application ActivityThread. Yes, the naming is misleading, but that's the way it is ¯\_(ツ)_/¯
Now, what does the main method do in the ActivityThread? Everything is prosaic here: it sets up the main thread and initializes its Looper. Simply put, an endless loop is launched on the main thread, in which the application code is executed – Activities are created, Views are displayed, etc. Many articles have already been written about Looper, I won’t retell them – you can read more, for example, here and here.
Actually, that’s how I came across declarations of the main method – I was looking through Looper’s sources and decided to find the place where MainLooper is initialized. But then the question may arise – who calls the ActivityThread.main method when the application starts? And this is where it comes in…
Zygote
You've definitely come across this name when looking at the application logs. Zygote is a process that is the base for all applications on the system. The name itself very clearly reflects the essence: in biology, a zygote is a cell formed as a result of fertilization, and capable of giving birth to any other by copying itself.
Our Zygote starts at system startup, loads system libraries and frameworks and enters an endless loop that waits for commands to launch applications.
When you launch the next application, Zygote creates a complete copy of itself, which will be the process of this application. He does this using a system call fork. At the same time, the original Zygote continues to spin an endless loop, but its copy exits the loop and calls the same ActivityThread.main, thereby launching the application.
The fork call in the Linux kernel has a lightweight implementation – it does not create a copy of memory for a new process, but simply reuses existing. This mechanism allows applications to run in separate processes without wasting time loading or copying classes each time.
Conclusion
So, the main method in Android applications, although hidden from our eyes, plays a very important role in the system. It is in the ActivityThread class, runs Zygote and itself runs the application's MainLooper.
The Zygote operating mechanism allows you to optimize the launch of applications, but it also has disadvantages. For example, since this process is basic for all applications, it is possible to infect it with a Trojan, which will then grow into each application. This is how one of the worst Trojans in Android works – Triada. And he can do anything – for example, intercept and read your SMS. The conclusion is banal – you should not install applications from dubious sources on your device.