Android NDK Development
When it comes to NDK development, it is actually to call some underlying C/C ++ things for project needs in some cases, and to make it more efficient. Google promotes Android Studio. This is a good tool. At least we can see that Android Studio is available in Eclipse, but some Eclipse may not. But for review, record the two ides.
Preparations
Go to the official website to download the NDK, which needs to be turned over and the benefits will be given below.
Http://yunpan.cn/cdFTKe3Ty4D9e access password 6776 (Android-ndk-r10e0) http://yunpan.cn/cdFTQAmSAI9sY access password f594 (I downloaded the wall flip tool, barely allowed to go)
After downloading the ndk, double-click it and decompress it to the current directory. It seems that the time is a bit confusing. Please wait patiently. Now we can start NDK development. Previously, we can go to the ndk directory to find samples and find the example provided by the official website. Learning ing ...... Start with Eclipse.
I. Eclipse DNK Development
1. Create a project HelloNDK
Write an native method in the Activity.
public class MainActivity extends Activity { static{ System.loadLibrary(hello); } /** * native * @return */ public static native String getStringFromC(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView text = (TextView) findViewById(R.id.textView); text.setText(getStringFromC()); }}
After writing the native method. Go to the java directory and run the javah command to generate the header file xxx. h required by c/c ++.
-Classpath is the class file under the bin directory of the project.
-D output directory (jni, The jni folder I just created under the project directory) <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;"> ** Xxx/xxx. h file ** extern C {/** Class: com_example_jnindk_MainActivity * Method: getStringFromC * Signature: () Ljava/lang/String; */JNIEXPORT jstring JNICALL struct (JNIEnv *, jclass); # ifdef _ cplusplus}
Go to the example jni folder on the official website and copy Android. mk and application. mk to the current project jni folder.
Android. mk
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := helloLOCAL_SRC_FILES := hello.cinclude $(BUILD_SHARED_LIBRARY)
Note the local moduleName of LOCAL_MODULE. LOCAL_SRC_FILES: A local c file.
2. Write a hello. c file in the jni folder.
#include
#include
#include com_example_jnindk_MainActivity.hJNIEXPORT jstring JNICALL Java_com_example_jnindk_MainActivity_getStringFromC (JNIEnv * env, jclass jclass){ return (*env)->NewStringUTF(env, Hello from the world !);};
Returns a string. Take a closer look at the class name. The first is Java, followed by the package name + class name + method name. Here, the package name is different from the class name written in my cmd. If it is incorrect, I wrote it casually. Just pay attention to it.
3. Generate the so file
A obj folder is generated in the project directory. The so file can be found here.
I am very kind to anyone who has done Baidu map to see so. Next we will call it in Maintivity.
static{ System.loadLibrary(hello); }
Test passed.
Ii. Switch NDK development to Android studio.
The previous steps are similar, but we don't need to run the command to generate the so file. We can configure it in build. gradle.
Pay attention to the ndk position, under defaconfig config.
Ndk position
Just in Eclipse, we manually run the command to generate the so file. as only requires configuration re-build, where is the so file generated in Chengdu. See.
Check whether the lib and so files are transplanted to so many APP_ABI files. How is this done? We found the configuration in application. mk. And the ndk configuration in build. gradle.
APP_ABI := APP_ABI := armeabi armeabi-v7a arm64-v8a x86 x86_64 mips mips64APP_PLATFORM := android-
Iii. Summary of errors
1. 'error: Execution failed for task': app: compileDebugNdk '.
Com. android. ide. common. process. ProcessException: org. gradle. process. internal. ExecException: Process 'COMMAND 'd: Usershtj-123AppDataLocalAndroidandroid-ndk-r10b dk-build.cmd "finished with non-zero exit value 2'
This is caused by space in my project directory.
2,Process: com.eddie.ndk, PID: 23210
java.lang.UnsatisfiedLinkError: Native method not found: com.eddie.ndk.MainActivity.getStringFromC:()Ljava/lang/String;
1) Android. mk:
Last generated
LOCAL_MODULE: = testcmmb_jni cannot be written as libtestcmmb_jni !!!! Although the name of the compiled file seems to be the same
2) C ++ code:
Extern "C "{
...
}
This is because the symbol tables of C and C ++ are different in the generated binary file. Jni is used to find the function according to the generation rules of C. Therefore, the compiler must add extern C to compile the function according to the rules of C so that it can be called by JAVA.