Android calls C file to generate so library and call

Source: Internet
Author: User

Companies need to do an SDK, which need to use someone else to write the C file, the first time to do, all kinds of Baidu, a variety of learning, now do a small summary:

First, create a new project: Example Exampledemo, the package is named: com.demo.example;

Second, configure the NDK environment for the project:

1, download the Android NDK, and unzip, my storage directory is: D:\sl\android-develop\android-ndk-r14b;

2. Click into Project Structure

     

3. In the pop-up box, select the SDK location tag and choose the NDK path in the Android NDK location:

    

4. View Local.properties

5. Add this line of code at the end of the Gradle.properties project: android.usedeprecatedndk=true

Third, start the creation of operations;

1. Create a new class and add the native method:

    

2. Execute build--"Make Project:

    

When you're done, you can see that we've created his class file.

Iv. command line generation. h file

1. Open Terminal

    

2, enter in the command line, enter the main file: CD App/src/main

Execute the Compile command: javah-d jni-classpath [Your SDK path]\platforms\android-22\android.jar;....\build\intermediates\classes\debug [ Your package name + class containing the native method]

Mine is:

g:\demo\qrreinrein\app\src\main>javah-d Jni-classpath D:\sl\android-develop\android-sdk-windows\platforms\ Android-22\android.jar, .... \.. \build\intermediates\classes\debug Com.cirein.qrcode.QRCodeManager

When finished, a JNI file is generated under the Mian folder, and the following is the. h file that we generated:

    

The contents of the file are:

      

Now start implementing the method inside the. h file: Copy the. h file to JNI and rename it as: Main.c

    

Note: The other C files can also be copied to the Jni folder, in the hit so library will be scored;

To edit a main.c file:

    

Open the App module's Build.gradle file, add the following code to the Defaultconfig node, and perform a synchronous operation

    

V. Generating so files

Perform:

    

When you are done, you can see the resulting so file:

    

Vi. using the resulting so file:

1, a new module, named Test, the package name can be customized,

2. Create a new folder Jnilibs under \main and copy the resulting so file to the folder

2. In Main\java new package, the package name must be the same as the class package name of the C file when you create so: Com.cirein.qrcode, and copy the Qrcodemanager just below the package, and the class name must be the same as the class name that started the call;

3. Make the call output in mainactivity:

    

    

Note: After you create a new test in the same project, the previously generated so file disappears, and you only need to Rebuild project again.

    

Android calls C file to generate so library and call

Related Article

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.