Overview
Before Android Studio did not support JNI development, everyone usually used Eclipse to develop JNI, and various configurations made people feel very painful. After supporting JNI development from Andorid Studio, it has become so easy for us to develop JNI.
Introduction to NDK and JNI
JNI (Java Native Interface) is a set of programming interfaces used to implement Java code interaction with other languages (c, C++ or assembly). What you need to note here is that JNI is the JAVA language's own feature, which means that JNI has nothing to do with Android. JNI is often used for development under Windows, such as read and write system registry, etc.
NDK (Native Development Kit) is a set of tools provided by Google that allows you to develop Android's JNI in other languages (C, C++ or assembly). NDK can compile multi-platform so. Developers only need to simply modify the mk file to indicate the required platform. No need to change any code, NDK can help you compile the required so.
Using JNI for application development is much more difficult than JAVA, and the threshold is much higher. If you have poor grasp of C/C++, you will have difficult-to-discover bugs! Therefore, it is usually only used when the performance requirements are relatively high. The game engine is an example of extremely high performance requirements. In addition, if you want to protect some core algorithms or processing logic, choosing JNI is also a good solution.
Here are three ways of developing Android studio JNI, the specific content is as follows:
The first type
first step: createnaticeLocal method Step 2: In engineeringApp模块目录下createjniFolders Step 3: existjinFolders中create.cThe endingcdocument,writecCode,existc语言中定义existJavaCode中create的Local method(naticemethod) ,To followjni的规范定义method(Java_Package name_Class Name_naticemethod名,其中Package name中的点用_replace) Step 4: existjniFolders中createdocument,documentThe content is as follows LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := hello #Specify the generated library name lib library name.so LOCAL_SRC_FILES := #Specify the C language source file to be compiled. Multiple spaces are arranged include $(BUILD_SHARED_LIBRARY) #Specify the generated library type (static library dynamic library independent program) dynamic library Step 5: existsrcFolders下create,The content is as follows APP_ABI := all #Specify the generation library platform Step 6: CompilationcCode生成soLibrary,arrivecCode所exist的目录里面按住shiftRight-click, 选中exist此处打开命令窗口(First of all, you have tondk-build配置arrive环境变量中),implementndk-build, The configuration will be automatically generatedsoLibrary Step 7: existAppModule configurationjinLibs的路径implementlibs sourceSets{ main{ =[“libs”] } } Step 8: 静态Code块加载 Static{ (“hello”) } Step 9:run
The second type
first step: createnaticeLocal method Step 2: SelectAPPClick the mouse to select the modulenew->folder->JNI folder->finish Will be heremainGenerate a directoryjniFolders Step 3: existjinFolders中create.cThe endingcdocument,writecCode,existc语言中定义existJavaCode中create的Local method(naticemethod), To followjni的规范定义method(Java_Package name_Class Name_naticemethod名,其中Package name中的点用_replace) Step 4: exist模块的 deflautConfigConfigure library name in the tag and specify the generated platform ndk{ moduleName “hello”//The generated library name abiFilters “armeabi”,”armeabi-v7a”,”x86”//The generated platform } Step 5: 静态Code块加载 Static{ (“hello”) } Step 6:run
The third type (Cmake)as version 2.2 can only be used
first step: createnaticeLocal method Step 2:existmain下面createcppFolders,再existcppFolders中穿件cSource File according tojniStandardized writingjavaCalled method Step 3:create文件,The generated library name and connected library name are configured in it.cThe path to the code(Generate method ,To create a new project, you must selectinclude c++ suppot,Select之后create的工程中就会有文件, And there isGoogleExample given) Step 4: Right click on the mouse when selecting the module,Selectlink c++ progect with gradlePopup laterCmakeDialog Box,chooseCMakeWay, After finding the generated moduleok If an error is reported, check whether the configuration is wrong.,If there is no error, it will be added successfully,会exist生成externalNativeBuildLabel Step 5:Configuration generatedsoLibrary Platform,existdeflautConfigLabel下面createexternalNativeBuildLabel externalNativeBuild{ cmake{ abiFilters “armeabi”,”armeabi-v7a”,”x86”//The generated platform } } 配置完成用静态代码块的Way加载soLibrary Static{ (“hello”) }
Possible errors
1, No implementation found for It means that the function is not implemented, 1,examinecIs the method correct?(according tojnispecification) 2,Is the library loaded statically,Is the library name written incorrectly 2, > Error: Your project contains C++ files but it is not using a supported native build system. Consider using CMake or ndk-build integration with the stable Android Gradle plugin: It probably means yoursndkNot the latest 1,Download the latest versionndk 2,Configure in =true(Permitted to use outdatedndk)
Summarize
The above are the three ways to record the development of Android studio JNI that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!