The main purpose of this article is shown in title, and provides solutions for some of the problems that may be encountered during the creation process.
This article main reference: http://ph0b.com/android-studio-gradle-and-ndk-integration/, there is video explanation, too look at the document trouble
Can be poked straight in ...
main idea : Create the native method in a class, compile and build the class file of the classes, and then generate the header file of the class, and then write the specific implementation of the code in the C file after the header file, configure the SDK, NDK path, name the NDK module, and finally call the method.
The detailed steps are as follows: 1. Create a native method locally
Create a new empty Android project, declare the native method in the class
Recommendation: Do not declare the native method in the activity, one for the design of concise and functional separation, the second is to create a header file to avoid the trouble.
Public class Jniutil { publicnative String getstringfromnative ();}
2. Compiling
, mainly to get a. class file with the native method class.
After compiling the pass as shown
The Jniutil.class file will appear in this directory.
3, the production of Jniutil.class header file 3.1 Open terminal terminal, there are two main methods:
3.1.1. menu bar View, select Tool windows, then click Terminal to enter
3.1.2. Shortcut key entry
3.2 Jni Generation header file
terminal into the App\src\main file , and then enter the following instructions (Javah and later),
The main format is Javah–d–jni–classpath,
Friends who are unfamiliar with javah instructions can enter the JAVAH-HELP help.
-O <file> output file (only one with-D or-O)
-D <dir> Output Directory
-v-verbose Enable verbose output
-H--help-? Output this message
-version Output Version Information
-jni generating a JNI-style header file (default value)
-force always write to the output file
-classpath <path> the path from which the class is loaded
path Description: e:\android-sdks\platforms\android-23\android.jar The SDK version that this Android project relies on, Final lock to Android.jar file;. \.. \build\intermediates\classes\debug Com.example.hitman.final.JniUtil represents the. class file on which the header file is generated. Two paths in the middle of a semicolon separated
After success, you will see a-JNI file in the main folder (some may generate the Jni folder directly). There's a compiled header in it.
problems you may encounter :
1), some classes of files can not find more cases are directly declared in the activity native method , because the as engineering depends on the Android version is relatively high, which leads to e:\android-sdks\ Platforms\android-23\android.jar does not have the required class, that is, javah–d cannot find the desired path. Take the following example
Importpublicclassextends appcompatactivity {}
View Code
This situation can cause appcompatactivity to be found because there is no such class in the E:\android-sdks\platforms\android-23\android.jar Path, The actual path to the appcompatactivity is E:\android-sdks\extras\android\m2repository\com\android\support \appcompat-v7\23.1.1 ( I path)
Solution: Move the native method out of activity, this method is compiled by the author once or the public class Mainactivity extends appcompatactivity Change to public class Mainactivity extends Activity .
3.3 Create a. c file, be sure to in the Jni folder
At this point we have to note that the -jni folder is renamed to Jni, or the compile-time build file cannot generate the NDK library (the author has encountered this problem), renamed after the Jni folder under the new. c file, any name is MAIN.C. The contents are as follows:
#include <com_example_hitman_finaljni_JniUtil.h> jniexport jstring jnicall java_com_example_hitman_ Finaljni_jniutil_getstringfromnative *env, Jobject obj) { return (*env) Newstringutf (env, "HELLO from Nativejni");};
View Code
Note : Be sure to add the new header file, as shown in blue text.
You may encounter problems : Some as bugs must create two. c files to compile successfully.
Refer to http://blog.chinaunix.net/uid-26000296-id-5296362.html for syntax issues with. c files.
4. Configure SDK, NDK path, and compile 4.1 to open the Local.propertiesz file
Ndk.dir=e\:\\android-sdks\\ndk-bundle
Sdk.dir=e\:\\android-sdks
4.2 Set the path and compile it to see it.build the library in the NDK under the Ask Price
Problems that you may encounter
:
Error:execution failed for task ': App:compiledebugndk '.
> ERROR:NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin. For details, see Http://tools.android.com/tech-docs/new-build-system/gradle-experimental. Set "Android.usedeprecatedndk=true" in gradle.properties to continue using the current NDK integration.
This can be added in the Gradle.properties file android.usedeprecatedndk=true after the recompile can be
5. Name your library and call native method 5.1 in activity Add NDK to Build.gradle in app directory
5.2 Load the local library and call the native method
Just call the Textview.settext () method in the activity. The code is as follows
Public classMainactivityextendsappcompatactivity {Static{system.loadlibrary ("Mylib"); } @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.content_main); TextView txt=(TextView) Findviewbyid (r.id.txt); Jniutil util=NewJniutil ( This); Txt.settext (Util.getstringfromnative ()); } }); } }
At this point, make sure that the compilation is up and running, and the simulator is as follows:
If you have questions, comments or suggestions welcome message exchange.
New version of Android Studio creating JNI Tutorials