Android calls the seven elements of the C program

Source: Internet
Author: User
Tags unix domain socket

The general language for developing applications on the Android platform is Java. For projects and products that are migrated from other platforms to Android, or for developers who are familiar with C/C ++ programming, you may want to reuse the existing C/C ++ code. Android provides a way to reuse Native code, as well as an environment and toolchain for compiling C code: NDK. NDK is a tool chain. With it, it is possible to use the C language on Android. In fact, Android originally set up a Java environment on Linux. It is incredible to say that C cannot be used, but Google is not fully open, why can't C programmers be more comfortable on Android? It is a man-made obstacle ).

There are two ways to use C code on the Android platform:

JNI native C executable program

JNI Mode

JNI was originally a framework provided by Java to reuse C code. Android has made some extensions, added AIDL to the service framework, and developed a set of tools to use Android. during mk compilation, the corresponding Java code can be automatically generated and compiled based on the AIDL file.

JNI is mainly used to compile C code into a dynamic library and call it in Java. The procedure is as follows:

Declare the native method in Java code in JNI (bridging this part of C code is called the JNI layer) the local method corresponding to the Java layer is implemented based on naming rules at the layer to load the C dynamic library at the Java layer.

Examples of the JNI method, such as Qt on Android, Vitamio, and some examples in the android framework itself, such as ServiceManager and Android. util. Log.

Native C executable program

Android is a Linux operating system. It is a natural way to call Native executable programs. Java also provides support at the language layer. The runtime.exe c () function does this. By starting the process through exec (), you can read the standard output of the Native process and write data to the standard input of the Native process.

Example of calling native executable programs. WifiManager connects to the wireless network through the control interface and wpa_supplicant (wpa_suppliacant for wireless connection, which is a native C executable program) send commands such as scanning access points, selecting networks, and connecting networks.

In addition, some of the built-in broadband dial-up (PPPoE) programs on the set-top boxes of Android systems are also C executable programs, the Java-layer PPPoEService finally calls programs such as pppd and ppppoe to execute the actual dialing process.

Also, we often say that root is actually implemented by calling a program named su.

This can also be used in actual development. For example, if you want to see what files are in a directory, you can directly call the ls command to read its standard output.

Java and C program communication problems

When calling C code through Java on Android, we may encounter communication between processes or threads.

Here we will talk about the communication between Java and C processes, Java processes, and C Shared libraries.

Sometimes our requirements are very simple. Blocking calls to C code can achieve the goal of getting the computing results. For example, you call a Hash function of the C Shared library through JNI, and for example, you call a Native executable program through Runtime.exe c () to calculate the Hash and read its standard output to obtain the result. These scenarios are simple enough. You don't need to consider the communication between Java and C Shared libraries or C Native executable programs. It's not worth the effort to maintain it.

However, there are still some complicated applications. We must establish a long-term communication mechanism between Java and C.

Let's take a wireless connection example. If you are interested, you can browse the source code of wpa_supplicant. It provides control Interfaces Based on two methods: timer and unix domain socket.

In fact, common inter-process communication mechanisms in Linux, such as pipeline pipe, socket, and signal, can also be used for communication between the Java layer and the C layer, and the android framework is used in this way. For example, we are familiar with the first process Zygote in the Android world (actually app_process, renamed Zygote after startup). One function is to start the Java Process, when we want to start an APK, the process of this APK is finally started by Zygote (Java class library and C Shared library can be shared among processes, and Zygote uses socket to receive commands.

For another example, we can see that many process management applications use android. OS. process to kill the Process, and Process. kill () is actually a very standard Linux mechanism that sends a signal to the target process.

There is also a common mechanism for communication between Java and C Native executable processes: standard input and output. We can write data to a standard input of a process, or read data from its standard output. Then we can define a set of control protocols for communication.

Pipelines such as pipe and socket can be used for both inter-process communication (Java and C Native executable programs) and intra-process communication (Java and C Shared libraries ). In what scenarios will we use this? The preceding wireless connection service program wpa_supplicant can give us some tips.

If we use C to implement a service, and the Java layer needs to frequently access the service and get feedback, we can do this.

In this scenario, we use C to implement a module that supports parallel download (single-thread select model ), while the Java layer frequently downloads images (the Java layer on Android does not seem to be easy to use and consume fewer http download libraries), such as a cloud album application or video application, we can then delegate the download action to the C process.

Inter-process communication. There is also a new Binder framework extended over the Linux IPC framework in the android framework. Many Native system services use the Binder framework, such as AudioFlinger, when we call AudioManager at the Java layer to set the volume, we finally use the Native System Service AudioFlinger function through the Binder framework, which is a typical cross-process call, but as a Java program, you don't need to worry about this.

To learn more about Binder. Here we mention the limitations of the Binder: not all C Programs can use the Binder to register services, only authorized services such as media. player, media. native system services such as camera can be used. If you are a system developer, you can modify the authorization list (controlled by UID) to allow your C program to register the service. As an application developer, don't think about it. Consider using pipelines such as pipe, socket, or standard input and output.

Compile the C executable program

There are several ways to compile a C executable program for the Android platform:

Use NDK and Android. mk manually uses pre-compiled gcc to compile and use Qt 5.2. for details, see Qt 5.2 for Android development in Windows and Qt for Android C language executable program in Windows.

Integrate and use C executable programs in APK

How can I integrate and use a C executable program in APK? Follow these steps:

You can put the executable program under the assets Directory. In this way, the resources in the assets folder will be accessed automatically when the APK is packaged into an APK, And the executable program will be released, run Runtime.exe c () to start the executable program.

Well, I finally made a rough remark, hoping to help you.

Related Article

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: 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.