STEP1: Create Native method
Very easy, just need to define a good way to add native keyword can
Note: Because the detailed implementation of this method is implemented in detail in C + +. Therefore, similar to the interface method does not need to add {}.
STEP2: Generate class file (most cases can be ignored)
In most cases, the class file is already generated. However, in the event of a new change or error in the code, it is possible to affect the generation of the class file, this time just need to generate again. Step Build==>make Project for the menu bar, if there is an error. Fix it again and try this operation again)
STEP3: Generating. h files
Click View->tool windows->terminal to open the command line form, which is equivalent to the cmd form under Windows.
CD App/src/main/java
To switch the path to App/src/main/java
Javah-d. /jni Collector.com.indoorun.orientionsensorcollector.JNI
Connect the class compilation that includes the native method to the. h file
The JNI directory and. h files have been generated in the project (the remaining files were subsequently added. Please ignore it first)
STEP4: Implementing A/C + + file
Copy the. h file that you generated in the previous step, and change the suffix name to. cpp (Personal love C + +, c if you can), remove the tail and stare parts. The method is implemented in detail, and this step requires a certain base of C + +.
STEP5: Configuring Gradle Files
Change the Build.gradle file of the appropriate module (configure NDK)
Change the Local.properties file (it is worth the NDK path, do not download it yourself)
Change the Gradle.properties file (enable NDK compile permission)
STEP6: generating. So files
Results after execution:
STEP7: Using JNI
Use load so libraries in Java code (recommended in application or static blocks of code in JNI classes)
System.loadlibrary ("Inertialjni");//The name you took when you generated the so file.
You can then call it the same way you need it as a generic Java method.
(Ensure load before use)
Above ~ everything is done ~ Direct Run is good ~
Use Android Studio for NDK development