Android Studio uses Ndk JNI for development projects
Ps: Recently idle, so. to sum up updating a few more blogs. by the way, I despise some programmer who have copied online articles and have not verified them by themselves. as a result, the number of blogs on the Internet is the same, as long as the people who first wrote the blog step on the back step all step on the pit. those who copy this copy without passing the actual test have a deep contempt
OK. Next we will go to the topic today, that is, how to correctly use and configure ndk in Android studio. if you do not know about jni, you are advised to study JNI technology first and then read this blog.
Old Rules: Let's first look at the implementation effect: click the button to call the c code method. Here, the C code method returns a string and we will display this string.
Preparations: First of all, let's take a look at what jni development needs: Android studio1.51 (official version), a android-ndk-r11b programmer. preparation is to download the ndk to configure the NDK-HOME (method and JAVA-HOME configuration is the same, so do not repeat, will not Baidu, it is said that you can also do not need to configure (⊙ )).
The following is a hands-on tutorial.
1. first, use Android studio to create an android project. and associate it with ndk. there are two associated methods: Configuring in the Project Settings, and configuring in the project directory.Local. propertiesYou can choose either of the two methods to configure the ndk path, as shown in:
(Solution 1)
(Solution 2)
2. Open the grade. properties file and add android. useDeprecatedNdk = true at the end.
3. Then, in the module'sBuild. gradleFile to configure the ndk parameters.
Apply plugin: 'com. android. application 'android {compileSdkVersion 23 buildToolsVersion "23.0.3" defaultConfig {applicationId "com. dapeng. ndkdemo "minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName" 1.0 "ndk {moduleName" MyJniLibName "// generated so name (can be specified by yourself) abiFilters" armeabi "," armeabi-v7a ", "x86" // output the so library under three abi architectures. Currently, it is optional (all options are selected by default ). // LdLibs "log" // if you want to use log, add} buildTypes {release {minifyEnabled false proguardFiles getdefadefaproguardfile('proguard-android.txt '), 'proguard-rules. pro' }}dependencies {compile fileTree (dir: 'libs', include :['*. jar ']) testCompile 'junit: junit: 4.12 'compile 'com. android. support: appcompat-v7: 23.3.0 '}
4. Create a class to compile the method for Calling c code. then rebuild the project. The name of the loaded library is shown inBuil. gradleTheModuleName. The method for Calling c code must be usedNative(Note that Chinese characters, including comments, cannot be written in the header file. Otherwise, an error will be reported when the header file is generated: Non- able characters encoded in GBK)
public class JniUtils { static { System.loadLibrary("MyJniLibName"); } public native String getStringFromC(); }5. After the project is built, go to
Debug folderCheck whether the. class file corresponding to the class we wrote is generated. The directory is as follows.
6. If the class file is correctly generatedHeader file. Open the android studioTerminal(In the console provided by android studio, you need to set it on win10 for normal input. For more information, see du Niang.) orCmdConsole. Use the jdk generation tool to generate the header file. first cd to ourModule javaFolder, and then useJavah-jni + name of the full path of the class that calls C code. The path under which the translation is generated must be viewed. after the file is generated, you can see it in the project directory. h format header file. (take a closer look at the posted path. The app is the module name)
7. Click projects one by oneNew-folder-jnifolderGenerate a jni folder (you can also directly create a jni folder under the same directory of the java package), and createAny file whose name ends in. c format.Copy the content in the. h header file to the. c file and edit it.
/* DO NOT EDIT THIS FILE - it is machine generated */#include
/* Header for class com_dapeng_ndkdemo_JniUtils */#ifndef _Included_com_dapeng_ndkdemo_JniUtils#define _Included_com_dapeng_ndkdemo_JniUtils#ifdef __cplusplusextern "C" {#endif/* * Class: com_dapeng_ndkdemo_JniUtils * Method: getStringFromC * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_dapeng_ndkdemo_JniUtils_getStringFromC (JNIEnv *env, jobject obj) { return (*env)->NewStringUTF(env, "hello java from c");} ;#ifdef __cplusplus}#endif#endif
8. Finally, we use
JniUtilsYou can call the c code method by writing the methods in it. Now all the code has been compiled.
public class MainActivity extends AppCompatActivity { private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.tv); findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mTextView.setText(new JniUtils().getStringFromC()); } }); }}
9. Click rebuild project to enter the builIntermediatesUnder the folder, check whether the ndk folder is generated, and then check whether the. so file exists in the ndk folder. If yes, the project is finished. that is all. plase enjory it.