Android system----Write system services and compile them into the system source code

Source: Internet
Author: User
Tags manual writing

Before the introduction of an article on how to write a simple driver and access to the driver's applet, and finally compile the program into the Android kernel source code through the Program Access driver verification is possible, then this article continues this knowledge point, the driver through the JNI connection to create a system service, Provide to the upper-level application Access Service function, you can see the previous introduction of the driver function is the kernel layer, and this article describes the framework layer of knowledge.


Disclaimer: This article refers to the Luo Shenyang book: "Android system source code scenario Analysis " If you want to know more details, it is very recommended to buy this book

Thank Juno for this book, Bring me a lot of unknown knowledge, the great God's blog address: Http://blog.csdn.net/luoshengyang


First, write the JNI layer service code

First step: Create a JNI directory

Enter the system's JNI directory:frameworks/base/services/jni contains all the JNI implementations of the system service in this directory:



Step Two: Write JNI code

The implementation code is also relatively simple, direct access to the previously compiled driver, and then in the provision of an external read-write method, and finally in the JNI method of manual registration:



Step Three: Modify the compilation script

In the same directory there is a android.mk file, you need to add our service, follow-up to compile this service into the source code:



Fourth step: Add the load configuration of the service JNI feature

The above has written our system service function, but also manually registered some reading and writing methods, Then also need to add this registration function to the system's Onload.cpp file is called, or the system is not found after compiling the Jni method, and this Onload.cpp program is the time to run the system startup, the internal is specifically registered system services JNI method:



Fifth step: Compiling the framework layer source code

The above steps have been completed coding and script configuration, the following can be directly compiled source code, the JNI function of the service compiled into the source code, you can directly use the MMM command to compile a separate module:

Mmm Frameworks/base/services/jni
Make Snod

So the compiled system.img contains the system JNI service implementation logic we defined, and then we can write Java code to access this JNI exposed read-write method.


Second, write Java Layer Service code

First step: Create a service aidl file

Enter the Aidl file directory for system services:Frameworks/base/core/java/android/os The aidl definition of all the system's services is stored here


About Aidl content is also very simple, providing read and write methods:



Step two: Compile the Aidl file

When we use Aidl, we know that after we have finished defining the files, we have to compile and generate the corresponding Java code so that we can use similar xxx.stub classes in order to implement specific functions in the remote service. The same is true here, so we have to compile this aidl file first:

mmm frameworks/base

Compile the framework module code separately so that the corresponding Ifregservice.stub class is generated.


Step three: Implement specific service functions

The above has compiled the Aidl file, generated the corresponding Java code, the following if you want to implement specific functions, you must inherit the stub class, this file is stored in the directory:Frameworks/base/services/java/com/android/ all of the specific service implementations in the server system are in this directory:


The code implementation is also relatively straightforward:


The native method for reading and writing is defined here, corresponding to the method of the previous JNI layer implementation:



Fourth Step: Add the service to the ServiceManager

In order to make the last access to this service, and other services like the system, we have to register the service to ServiceManager, and this registration function is in the Systemserver class, because at the time of the system startup, the zygote process generated by the first process is system_ Server, in this process to do the registration of system services, we found the Systemserver.java class in the same directory of services, registered in the Serverthread::run function:


After this registration, the upper-level application can be directly obtained through the ServiceManager to the service, you can directly access the specific functions.


Sixth step: Compiling the framework source code

The implementation of the Java Layer Service code has already been completed, and here we seem to have seen familiar code, such as the AIDL definition of the service, the implementation and registration of service functions, and the previous introduction of the binder mechanism and the remote service invocation mechanism of knowledge more and more close, the following in the last step, Compile the source code:

Mmm Frameworks/base/services/java
Make Snod

After compiling the system.img file contains the Fregservice service we defined in the framework, and the name of this service is Freg, the following continues to describe how to write a program to access this service feature.


third, write system Application Access Service function

Earlier, we introduced the service functions in the framework, implemented the access-driven native code in the JNI layer, then implemented the Java layer code to call these native methods to realize the read and write functions of the driver, and defined a system service to wrap these functions. Finally, this service is registered to the system, here we will write a simple Android system program to access the service features:

First step: Create a program project

The creation of the System program project is in this directory:packages/experimental, we define a Freg project:


This project structure and normal Android program structure, nothing to say, because this is not dependent on the IDE compiled, so we have to write a compiled script android.mk file:


About the Android program code is also relatively simple, directly through the ServiceManager to access the service can:



Step Two: Compiling the application

After the above program is created, then compile the source code again and package the application into the system:

Mmm Packages/experimental/freg
Make Snod

Once the compilation is complete, the generated system.img file contains the system application


Step three: Start the emulator validator

Then we can start the simulator to run this applet:

Emulator-kernel Kernel/common/arch/arm/boot/zimage &

After we find this program, open the effect:


After opening this program, we can read and write the following:




Iv. Summary of the process

Here we go. Complete the manual writing a simple system service and add to the system, combined with a previous article content, the following to summarize the whole process, first, a picture of the pressure yajing:


With this picture, let's summarize:

1, write the driver
1> creating Freg drivers in the Kernel/driver directory
2>make Menuconfig compiling the driver into the kernel source code

2. Write the JNI program of the framework Layer service
1> created the JNI code for the service under the Frameworks/base/services/jni directory
2> Adding the registration logic for the service Jni method in Onload.cpp
3>mmm Frameworks/base/services/jni compiling the JNI program into the system source code

3. Writing Java programs for the Framework Layer service
1> the Aidl file for the service was created under the Frameworks/base/core/java/android/os directory
2>mmm Frameworks/base Compile the Aidl file, generate the corresponding Java code
3> to write service-specific implementations in the Frameworks/base/services/java/com/android/server directory
4> finally registers the service (Servicemanager.addservice method) in the Systemserver.java file found in the same directory
5>mmm Frameworks/base/services/java compiling Java layer Services

4. Write System Android App
1> Create an Android project in the Packages/experimental directory and use the ServiceManager directly in the program to get the remote service.
2>mmm Packages/experimental/freg compiling the program into the system
3> Boot Simulator validation results: Emulator-kernel Kernel/common/arch/arm/boot/zimage &


After all, in fact, some of the students may be very interested in the experiment, may want to experiment immediately, but here is the biggest problem now is that you have to compile the Android source code, this is the core is the most basic, and then in accordance with these processes to go is very simple, So as an Android developer for life or to compile one time Android source code. So interested students should quickly move hands to compile Android source code.


Project Address Download: http://download.csdn.net/detail/jiangwei0910410003/9642835


v. Summary

It's over here. How to manually write system services and add to the system work, but also temporarily ended the time of the introduction of the Android System series of knowledge, in fact, I just want to introduce how to hook off the System AMS service Interception application launch knowledge, But who can not think of the introduction of such a large string of knowledge out, no way I can only slowly an article introduced.


Click here for more information:

Focus on the public, the latest technology dry real-time push



Android system----Write system services and compile them into the system source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.