IsAndroid SDKAdding application case implementation is the content to be introduced in this article, mainly for understanding and learning.Android SDKFor more information, seeAndroid SDKFor more information, see this article.
In the process of developing Android applications, there are usually two methods: NDK and eclipse, or directly increase in the Android SDK. This section describes how to add an application in the Android SDK environment. It involves JNI and JAVA calls to JNI. The JNI code file is C ++.
The first step is to establish the JNI-Layer Code. Note that the JNI code path has a great relationship with the JAVA code path and must be consistent.
The Android SDK creates the jni C ++ file android_test.cpp in the frameworks/base/core/JNI directory. The content is as follows:
- # Define LOG_TAG "FMC"
- # Include "jni. h"
- # Include "android_runtime/AndroidRuntime. h"
- # Include <nativehelper/JNIHelp. h>
- # Include "utils/Log. h"
- Extern "C "{
- Int test (void); // this function can be from c code or c library
- };
- Namespace android // note that the namespace is closely related to all the paths of JNI called by JAVA, and The namespace cannot be used at will.
-
- // The original author's sentence is incorrect.
-
- // This is only the c ++ namespace and JNI. If android namespace is not used
-
- // AndroidRuntime. cpp should also be declared outside android namespace.
- {
- Static jint android_test (JNIEnv * env, jobject clazz)
- {
- Return test ();
- }
- Static JNINativeMethod method_table [] = {
- {"My_test", "() I", (void *) android_test}, // The first parameter is a string, which is the function name seen at the JAVA layer;
- // The second parameter is the parameter description of the function,
- // The JAVA code uses this parameter to know the parameters of the called function;
- // The third parameter is the C Function actually called in the JNI code.
- }
- Int register_android_test (JNIEnv * env) // This is the JNI registration function. android. test is the package path seen at the java call layer (Step 3,
- // If register_android_hardware_test is used,
- // The JAVA call layer needs to import the android. hardware. test package.
- {
- Return AndroidRuntime: registerNativeMethods (env, "android/test/Test", method_table, NELEM (method_table ));
- }
- };
Step 2: add the JNI code registration function to the AndroidRuntime Runtime Library. Edit the frameworks/base/core/jni/AndroidRuntime. cpp file:
Android SDK adds the following two lines according to the method on the file:
- ..........
- Extern int register_android_test (JNIEnv * env); // This is the registration function in the android_test.cpp file.
- ..........................
- Static const RegJNIRec gRegJNI [] = {
- .........
- .........
- REG_JNI (register_android_test ),
- .........
- .......
- };
Step 3: Create a JAVA call layer and create the file test. java in the frameworks/base/core/java/android/Test directory. The content is as follows:
- Package android. test;
- Public class Test {
- Private native int my_test (); // The final code that can be called in the Android application code
- Public Test () // Constructor
- {
- // Add Your Code Here .............
- }
- };
Step 4: Create your android app in the packages/apps/test directory. The specific creation method is not described here.) Suppose test_app.java is the main file of the app, the content is as follows:
- Package com. app;
- Import android. test; // import your own package
- Use this in the Code:
- Test;
- Test. my_test (); // The final call
Android sdk jni implementation, Android sdk jni c ++ code, Android call C ++, build the complete steps of JNI and JAVA applications under Android SDK, Android JAVA call C ++ code.
Summary:Android SDKThe content of application case implementation is introduced.Android SDKContent learning is helpful to you!