Android Studio compile C build. so file

Source: Internet
Author: User

First, preface

A colleague left the office a while ago, The. So file compiled with C needs to be changed, results C file is he wrote, Rogue Ah, oneself is a small white, not ah, so oneself and good study of a day, will not, see what is difficult ah, pain ah, online to find a lot of, just find, and feel not very detailed, step said is not very clear, so oneself tidy up a bit, who then record, good , don't say much, start to the point.

Second, the compilation process1, build the environment

Just so-called: "工欲善其事 its prerequisite" Well, so first prepare the tool for preparation, because it is compiled under studio, so Ah, what studio, what SDK, what JDK, these are to get it, here to say the NDK this compiled C file tool How to download the installation;

Download Link: https://developer.android.com/ndk/downloads/index.html (generally need to turn over the wall, as for the reason, you know) to go in and download their corresponding version of the NDK platform;

After the download is directly a ZIP compression package, decompression can be;

Then configure the NDK as shown:



Then you will see in the local.properties file:



Don't worry, it's not over yet, the NDK environment is still in the final step, with the android.usedeprecatedndk=true code at the end of the gradle.properties file, You can't read the words directly:


Okay, here's the setting.

2, Java code and C code writing procedures and procedures

Create a new Java class first Jniutils.java

public class Jniutils {    static {        system.loadlibrary ("huazict");    }    The methods in Java Tune C need to be declared with native and the method name must be the same as the method name for C    native String getString ();}


And then, as shown, re-Make ProjectA little bit of engineering:


will see your compiled class file in the project catalog E:\work\MyApplication\stujni\build\intermediates\classes\debug\com\huazi\stujni\jni Jniutils.class as shown:



The second is to generate. h files.

In studio Open the Terminal command-line tool, the open step is View->tool windows->terminal (or press ALT+F12 directly) as shown in:



Then go to the main directory of the project in the command line

Input command: javah-d jni-classpath the absolute path of your compiled class file

Example: javah-d jni-classpath E:\work\MyApplication\stujni\build\intermediates\classes\debug Com.huazi.stujni.jni.JNIUtils ( Note the space after debug )



See, the command line in the diagram is directly into the project's main directory (under which directory the directory will be automatically generated JNI folder), press ENTER after the main directory will be generated in the JNI folder, while generating. h files, such as:



This file. h file does not need to be modified, by default.

Now let's write a test C file huazict.c The same as the. h file under the Jni folder, with the following code:

#include "com_huazi_stujni_jni_jniutils.h"/** * The reference tag above must be a. h file master suffix, the method name must be the same as the method name in the. h file */jniexport jstring Jnicall Java_com_huazi_stujni_jni_jniutils_getstring        (jnienv *env, Jobject obj) {    return (*env)->newstringutf ( ENV, "This is my test of JNI");}




Finally, in the default configuration in the build file, add:

NDK compilation generates. So file        ndk {            modulename "huazict"         //generated            so name abifilters "Armeabi", "armeabi-v7a", "x86"  The output specifies the so library under three ABI architectures.        }




Here, through the JNI C is done, now let's test, write a TextView show the call C:

public class Mainactivity extends Appcompatactivity {    @Override    protected void OnCreate (Bundle Savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        TextView TV = (TextView) Findviewbyid (r.id.tv);        Tv.settext (New Jniutils (). getString ());}    }


Look at the effect, directly:



OK, no problem, can be called, in fact, it is not so difficult to imagine, is it!

Third, call the compiled. So library

The above compilation is complete, some people will ask: I want to be compiled. So library, others use the time directly to use it, then the compiled. So library where? Don't worry, look:



According to this path, you can find the . So library files in the three architectures that specify the output, and then copy the. So file, as shown in the appropriate folder, OK:


Run again, the results are the same, with the top of the picture is the same as the display, the same can be called, code I do not upload, are posted on the top, and also can not upload, the company's all the files are encrypted, want to upload can not upload.


here, the entire JNI invocation process is over, including invoking the environment required by JNI and invoking the process, and finally the export of the. So file is on top, If there is any problem with the above JNI call process, please leave a message, thank you.


Android Studio compile C build. so file

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.