I am new to Android, and was studying the framework and it compelled me to ask this question. Since we are extending Activity in Android, there has to be main
s
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.
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.
When you start a new process to run a App/Service, finally in ActivityManagerService.java, there is:
final String entryPoint = "android.app.ActivityThread";
return startProcessLocked(hostingType, hostingNameStr, entryPoint, app, uid, gids,
runtimeFlags, mountExternal, seInfo, requiredAbi, instructionSet, invokeWith,
startTime);
Which finally calls Process.start. This is the same with the standard java, you pass in a class then the VM create a new process and execute main() of ActivityThread.java:
public static void main(String[] args) {
......
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}
The main function will trigger some action that send IPC message to notify Activity Manager that the process has started successfully, then after notifying the process that initiate the start of the new process of this event, Activity Manager will notify the new process to do the real activity startup process, which create the Activity class according to the manifest then call OnCreate etc.
There is a few answer here that is totally wrong which get a lot of votes, hope a moderate etc can check this.
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 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
.
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 Android, the OS uses Dalvik virtual machine. The main entry point to the execution of the application is encapsulated within the framework. You might want to take a look at "What is Android?"
In fact, each Activity in Android can be thought to be a single Application on its own with a lifecycle of its own.