Use of NDK in Android Studio
When developing android programs, we sometimes need applications to call underlying hardware. Android officially provides us with NDK so that we can call the C, C ++ library in our program. Ndk is a set of tools developed based on java jni technology for android programming. It can help us quickly develop dynamic libraries of C or C ++ and package and generate APK together with the application.
For details about how JNI works, refer to [JAVA basics to understand JNI principles]
To download and install NDK, go to http://developer.android.com/ndk/downloads/index.html.
Or many domestic image websites
After the download is complete, decompress the package and use it.
Use of NDK in Android Studio
1. Create an Android Studio Project
2. Configure the NDK path in the project
File-> project structure
Click "OK". What is your local. prZ license? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> coding + DQo8cD48aW1nIGFsdD0 =" here write picture description "src =" http://www.bkjia.com/uploads/allimg/160412/0411155916-2.png "title =" \ "/>
3. Create an NDKTest class and write the following code:
public class NDKTest { static { System.loadLibrary("ndk_test"); } public native static String hello();}
4. rebuild the project and change it to the project directory.
The. class file of the NDKTest file just compiled has been generated under the bebug directory.
Then we enter the Directory through terminal.
Use javah to generate c. h header files
javah -jni com.hehao.ndkdemo.ndk.NDKTest
After the execution is complete, a C file of com_hehao_ndkdemo_ndk_NDKTest.h will be generated.
Next, go to the main directory. Create a jni directory
Copy or cut the. h header file generated by javah to the jni directory.
Create a C file to implement the function declared in the header file (the name is random), but we usually use a name like java + package name + class name
Add the following code to the file:
# Include "inline"/** Class: Java_com_hehao_ndkdemo_ndk_Test * Method: test * Signature: () Ljava/lang/String; */JNIEXPORT jstring JNICALL inline (JNIEnv * env, jobject obj) {return env-> NewStringUTF ("this is from c: hello world! ");}
Then we can use the rebuild operation we just made to re-Build the Project Build-> Rebuild Project
PS: Then I found that the project reported an error.
I don't know why. Restart the AS and Rebuild it again.
Then this error is prompted during running.
Add a line of code at the end of gradle. properties as he said.
android.useDeprecatedNdk=true
Again, an error is reported.
Baidu has a lot of information about this error, which is said to be a bug of NDK in windows. I did not make any research. I quoted other people's statements.
The solution is to create an empty c file under the jni directory.
Then, the operation is successful. Then we try to call the NDK class code in mainActivity.
package com.hehao.ndkdemo;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.TextView;import com.hehao.ndkdemo.ndk.NDKTest;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = (TextView) findViewById(R.id.ndk_test); textView.setText(NDKTest.hello()); }}
Then an error occurs.
Yes, I found a problem when loading the class.
In this case, you need to add
Ndk {moduleName "ndk_test" // generated so name abiFilters "armeabi", "armeabi-v7a", "x86" // output so libraries under the specified three abi architectures, currently available}
The generated. so file must be consistent with the one introduced in the previously created Java file.
Successful