Previously, I briefly described the android startup process at http://blog.csdn.net/codectq/article/details/7383231,but it is not perfect.
Now I have encountered some practical problems in the project. In turn, I will read the code and review the android startup process after the kernel is started.
The systemserver. cpp file in the frameworks/base/services/Java/COM/Android/Server folder. The init1 () function is called in this file. The sub-function is described as follows: This method is called from zygote to initialize the system. This will cause the local service (surfaceflinger, audioflinger, etc.) to be enabled. After completing these steps, the system calls back init2 () to start the android service.
From the above description, we can know two aspects: first, in the android service, we are divided into two types of services, one is the native service, and the other is the android service. Second, our local service is completed in the init1 () process, while the android service is completed in the init2 () process.
We can also see from the printing information of the actual Startup Process of the real machine that we will first start surfaceflinger, audioflinger and then start dalikvm, to start zygote and system, and start Android runtime to start native service by calling system_init () and sysproc. Call back init2 () and Android service \ thread pool to enter systemserver, run the systemserver thread, and start the service registered to systemserver.
If we want to use the android architecture to add our services, we may wish to use them.
By using the JNI mechanism, the upper-layer Java applications can call the native service. The specific implementation is to put the. cpp file under the JNI folder and the. Java file under the Java folder. The procedure is as follows:
In the above article, we have said that we can use Android's own architecture to complete our JNI service, without having to complete every detail on our own. We also mentioned that we will call androidruntime, so we can register the service to the androidruntime. cpp file. Below is a classic example, here to do an excerpt address for http://dongyulong.blog.51cto.com/1451604/545496 (but do not know if it is the original author)
Create the example android_mytest_hellojni.cpp file in the frameworks/base/CORE/JNI path. This file implements the interface at the JNI layer. The file content is as follows: (you can refer to the android_debug_jnitest.cpp file in the same directory)
#define LOG_TAG "HelloJNI" #include "jni.h" #include "nativehelper/JNIHelp.h" #include "utils/Log.h" #include "utils/misc.h"
namespace android { static jstring android_mytest_hellojni_displayString(JNIEnv *env, jclass clazz) { return env->NewStringUTF("Hello from JNI!"); }
/* * JNI registration. */ Static jninativemethod gmethods [] = { /* Name, signature, funcptr */ {"Displaystring", "() ljava/lang/string ;", (Void *) android_mytest_hellojni_displaystring },
}; Int register_android_mytest_hellojni (jnienv * env) {// The directory structure is the file directory at the javaframework layer and must be consistent
Return jniregisternativemethods (ENV, "android/mytest/hellojni ", Gmethods, nelem (gmethods )); } };
|
2.JNI layer: Modify the compilation Configuration
2.1 modify the Android. mk file under the/Android/android-1.6_r2/frameworks/base/CORE/JNI directory, add
Android_mytest_hellojni.cpp \
2.2 Add the androidruntime. cpp file under the/Android/android-1.6_r2/frameworks/base/CORE/JNI directory after the extern int
Extern int register_android_mytest_hellojni (jnienv * env );
Add the following content under static const regjnirec gregjni [] = {
Reg_jni (register_android_mytest_hellojni ),
In this way, the modification of the JNI layer ends here.
Frameworks/base/CORE/Java/Android/create the file directory mytest, and create the file hellojni. Java declaration interface under this directory. The content is as follows :( you can refer to the jnitest. Java file under the android-1.6_r2/frameworks/base/CORE/Java/Android/DEBUG directory)
Package Android. mytest; Public class hellojni { Public hellojni (){} // This is declared as public, so it can be called by the application.
Public static native string displaystring (); }
|
4. Next we will re-compile the libandroid_runtime.so and framework. jar that we have changed.
Enter make libandroid_runtime in the source code project directory to re-compile and generate libandroid_runtime.so
...
Target thumb C ++: libandroid_runtime <= frameworks/base/CORE/JNI/android_mytest_hellojni.cpp
Target thumb C ++: libandroid_runtime <= frameworks/base/CORE/JNI/androidruntime. cpp
Target sharedlib: libandroid_runtime (Out/target/product/generic/obj/shared_libraries/libandroid_runtime_intermediates/linked/libandroid_runtime.so)
Target prelink: libandroid_runtime (Out/target/product/generic/symbols/system/lib/libandroid_runtime.so)
Target Strip: libandroid_runtime (Out/target/product/generic/obj/lib/libandroid_runtime.so)
Install: Out/target/product/generic/system/lib/libandroid_runtime.so
Then input make framework to re-compile and generate framework. jar.
...
Install: Out/target/product/generic/system/framework. Jar