When we develop an Android application, we always start from the onCreate()
method of the main activity. It is obvious that there are some initializations that should be done before calling onCreate()
. My question is: what is the entrypoint point (or the main
method) of an Android application? What does the Dalvik VM invoke in the very beginning (i.e., when it finishes initialization of its own, and is about to transfer control to the application)? Where can I find the code of this main
?

- 2,504
- 3
- 30
- 48
4 Answers
The first "entry" point is the application class as Kingston pointed out.
However, the easiest thing to get the very first starting point is to check the stack when debugging onCreate.
You may check Instrumentation, this sound somewhat like what you want.
http://developer.android.com/reference/android/app/Instrumentation.html
MainActivity.onCreate(Bundle) line: 12
Instrumentation.callActivityOnCreate(Activity, Bundle) line: 1047
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2627
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2679
ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 125
ActivityThread$H.handleMessage(Message) line: 2033
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626
NativeStart.main(String[]) line: not available [native method]

- 11,053
- 14
- 62
- 116

- 9,413
- 4
- 33
- 40
-
I did try that before. I'm here looking for more "officially documented" kind of answer to this. Also, is the code for Android framework available (I mean, those classes from android.*)? – dacongy Apr 07 '12 at 19:18
-
all the android code is available. It takes a while to download it and build it but you can follow the documentation here:http://source.android.com/source/initializing.html – kingston Apr 07 '12 at 19:20
-
BTW if you want to downloaded follow the instruction strictly in all the cases but about installing the Java sdk. You can also check this: http://stackoverflow.com/questions/9741293/error-installing-java-on-ubuntu-10-64bit – kingston Apr 07 '12 at 19:35
-
@stefan @kingston Won't the starting of `process` be considered the first entry point of an app? – ansh sachdeva Sep 13 '20 at 18:38
You should extend the Application class and override the onCreate method.
For reference:Application class

- 11,053
- 14
- 62
- 116
-
2You should probably add that you need to register your extended application class name in the manifest with the
tag – Jems Apr 07 '12 at 19:11 -
-
1From the doc: "onCreate() Called when the application is starting, before any other application objects have been created." So there isn't a "main" but you have a point where to do things before any other objects are created – kingston Apr 07 '12 at 19:15
I don't know it myself, but it sounds an interesting question. This is the code that fires a new Activity
and following the code, you'll end up in JNI code
public void startActivityForResult(Intent intent, int requestCode) {
if (mParent == null) {
Instrumentation.ActivityResult ar =
mInstrumentation.execStartActivity(
this, mMainThread.getApplicationThread(), mToken, this,
intent, requestCode);
if (ar != null) {
mMainThread.sendActivityResult(
mToken, mEmbeddedID, requestCode, ar.getResultCode(),
ar.getResultData());
}
if (requestCode >= 0) {
// If this start is requesting a result, we can avoid making
// the activity visible until the result is received. Setting
// this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
// activity hidden during this time, to avoid flickering.
// This can only be done when a result is requested because
// that guarantees we will get information back when the
// activity is finished, no matter what happens to it.
mStartedActivity = true;
}
} else {
mParent.startActivityFromChild(this, intent, requestCode);
}
}
Android source code is available, but it's a bit tricky to get it because it's poorly documented. You'll have to install repo
and then download the framework/base
project

- 20,627
- 6
- 47
- 86
-
1Yes. Besides that, you can look at `./frameworks/base/core/java/android/app/ActivityThread.java`, `./frameworks/base/core/java/android/os/Looper.java`, `./frameworks/base/core/java/android/app/ActivityThread.java`, `./frameworks/base/core/java/android/os/Message.java`, `./frameworks/base/core/java/android/os/Handler.java`, and etc. Just to name a few. – dacongy Apr 07 '12 at 19:39
-
@dacongy yes :) I don't know JNI unfortunately so I don't know where to search – Raffaele Apr 07 '12 at 19:44
-
actually, i get the class names from the answer from the stacktrace provided by "@stefan bachert" below :) – dacongy Apr 07 '12 at 19:47
In core Java programs we need a main() method, because while executing the byte code the JVM(Java Virtual Machine) will search for the main() method in the class and start executing there.
In Android, the (DVM)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.

- 1,073
- 12
- 14