Where is main() in Android?
Android uses the java language, but executes using a modified runtime model. As others have said, there is a manifest included in each package. The launchpoint is specified in this manifest. Go to the android site and do the basic tutorials. This will get you up and running with an understanding of create/deploy/run process and the basic app life cycle.
Actually, the main()
method is the Android framework class android.app.ActivityThread
. This method creates the Main (UI) Thread
for an OS process, sets up the Looper
on it and starts the event loop.
The Android framework is responsible for creating and destroying OS processes, launching applications, starting activites, services and other components. The ActivityManager
is part of the Android framework and it is responsible for coordinating and managing different components.
The architecture of Android is a bit different than you may be used to from stand-alone Java applications. The biggest difference is that all of your components (Activity
, Service
, BroadcastReceiver
, etc.) do not necessarily run in the same OS process or in the same virtual machine (VM). It is possible to have components from a single application running in different OS processes and it is also possible to have components from different applications running in the same OS process. In traditional Java, the main()
method is the method that is called by virtual machine after the OS process has been created and the virtual machine has completed its initialization.
In core Java programs we need a main()
method, because while executing the byte code the JVM will search for the main()
method in the class and start executing there.
In the case of Android, the Dalvik Virtual Machine (After android 5.0 DVM is replaced by Android Runtime) is designed to find a class which is a subclass of Activity
and which is set as a LAUNCHER to start the execution of the application from its onCreate()
method, so there is no need of a main()
method.
For more information see the life cycle of Activity
.
Read this blog entry to understand how an Android application starts:
During startup of the Android system the Linux kernel first calls the process "init". init reads the files "/init.rc" and "init.device.rc". "init.device.rc" is device specific, on the virtual device this file is called "init.goldfish.rc".
init.rc starts the process "Zygote" via the program "/system/bin/app_process". Zygote loads the core Java classes and performs initial processing of them. These classes can be reused by Android application and therefore this step makes them faster to start. Once the initial work of Zygote is done, the process listens to a socket and waits for requests.
If you look in the ZygoteInit class, you'll find the main method.
As others have mentioned, this main()
method is involved in setting up the Android app environment. As far as a developer is concerned, the starting point is the onCreate()
method of the Launcher activity.