Use Android Studio for NDK development

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.