Development of JNI Engineering for Android Studio

Source: Internet
Author: User

After you create a new project using Android Sutdio, you then record the basic steps for creating the NDK project.
This article will reach: 1. Create NDK Project 2. Output the LOG statement 3 in JNI. Specifies the ABI version 4 of the compiled so library. Solve problems in creating NDK projects
step:1. Adding native interfacesNote that the native interface and the System.loadlibrary () can be written, and there is no special place. P.s:oncreate () executes SetText () in R.id.txt, so you need to modify the XML layout file as normal development steps.
Give the code directly as follows:
public class Mainactivity extends activity{    static {        system.loadlibrary ("Jnitest");    }       Public native String getstringfromnative ();    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        TextView Txtview = (TextView) Findviewbyid (r.id.txt);        Txtview.settext (Getstringfromnative ());}    }



Step:2. Performing build->make Project


This step executes, verifying that there are no other errors in the project and compiling the project, resulting in a. class file. The generation path of the class file is under App_path/build/intermediates/classes/debug. For example:


Step:3.javah Generating C header files

Click "View->tool windows->terminal", which is the terminal command line tool in Studio. Execute the following command to generate the C language header file. It is important to note that the Javah command is executed to enter the <project>\app\src\main directory, so that the generated. h file is also under the <project>\app\src\main path. Can be seen directly in the engineering structure of studio.
Operation Command:javah-d jni-classpath <SDK_android.jar>;<APP_classes> lab.sodino.jnitest.MainActivityThe detailed operation diagram is as follows:

javah-d Jni-classpath C:\Users\sodinochen\AppData\Local\Android\sdk\platforms\android-16\android.jar ... \.. \build\intermediates\classes\debug lab.sodino.jnitest.MainActivity


For "Major version 51:50 new, this compiler supports the latest major version" is caused by the installation of two versions of the JDK on the computer, and the current use of the old JDK. When the old JDK is removed and the Java version command is executed to show that the current JDK is the latest 1.7, there is no longer a hint. Such as:

The result of the final build:




Step:4. editing C files

Implement the method in the header file in the Main.c file, specifically to return a string directly, and print out the related log using Android_log. The code is as follows:
/* Don't EDIT this file-it are machine generated */#include <jni.h> #include <android/log.h> #ifndef log_tag#d Efine Log_tag "Android_lab" #define LOGE (...) __android_log_print (Android_log_error, Log_tag, __va_args__) #endif/* Header for Class lab_sodino_jnitest_mainactivity */#ifndef _included_lab_sodino_jnitest_mainactivity#define _ Included_lab_sodino_jnitest_mainactivity#ifdef __cplusplusextern "C" {#endif/* * Class:lab_sodino_jnitest_ mainactivity * method:getstringfromnative * Signature: () ljava/lang/string; */jniexport jstring jnicall java_lab_sodino_jnitest_mainactivity_getstringfromnative  (JNIENV * env, Jobject JOBJ) {      LOGE ("Log string from NDK.");      Return (*ENV)->newstringutf (env, "Hello from jni!");  } #ifdef __cplusplus} #endif #endif

When we get here, we execute a "build->make Project" and find that "Messages Gradle Build" gives the following hints:
Error:execution failed for task ': App:compiledebugndk '. > NDK Not Configured. Download the NDK from Http://developer.android.com/tools/sdk/ndk/.Then add Ndk.dir=path/to/ndk in Local.properties. (on Windows, make sure you escape backslashes, e.g. C:\\NDK rather than C:\NDK)
This indicates that the NDK is not configured and requires the NDK path to be configured in the Local.properties file in the project. Well, the hints are clear, so let's go to the next step.

step:5. Configuring the NDKThis step consists of two actions: 1. Specify the NDK path
2. Modify the Build.gradle configuration project in a total of two build.gradle configuration files, we want to modify is in <project>\app\build.gradle this file. Added to the Defaultconfig branch.
    NDK {        modulename "jnitest"        ldlibs "log", "Z", "M"        abifilters "Armeabi", "armeabi-v7a", "x86"    }
The above configuration code specifies the so library name is jnitest, the library to which the link is used, the local_ldlibs in the corresponding android.mk file, and the final output specifies the so library under three ABI architectures. After adding as:
At this point, you can compile the so file by executing "Build->rebuild Project". However, there is a problem on the window platform:
Error:execution failed for task ': App:compiledebugndk ' .> com.android.ide.common.internal.LoggedErrorException: Failed to run Command:d:\mission\adt-bundle-windows\ndk-r10b\ndk-build.cmd ndk_project_path=null APP_BUILD_SCRIPT=C : \users\sodinochen\androidstudioprojects\jnitest2\app\build\intermediates\ndk\debug\android.mk APP_PLATFORM= Android-21 Ndk_out=c:\users\sodinochen\androidstudioprojects\jnitest2\app\build\intermediates\ndk\debug\obj NDK_ Libs_out=c:\users\sodinochen\androidstudioprojects\jnitest2\app\build\intermediates\ndk\debug\lib APP_ABI= Armeabi,armeabi-v7a,x86error Code:2output:make.exe: * * * No rule to make target ' C:\Users\sodinochen\ Androidstudioprojects\jnitest2\app\build\intermediates\ndk\debug\obj/local/armeabi/objs/jnitest/c_\users\ Sodinochen\androidstudioprojects\jnitest2\app\src\main\jni ', needed by ' C:\Users\sodinochen\AndroidstudioProjects \jnitest2\app\build\intermediates\ndk\debug\obj/local/armeabi/objs/jnitest/c_\users\sodinochen\ Androidstudioprojects\jnitest2\APP\SRC\MAIN\JNI\MAIN.O '. Stop.
This error is baffling. A few times toss, find a video out of the probable cause and solution: The source see YouTube video 02:50 start: https://www.youtube.com/watch?v=okLKfxfbz40#t= 362 a bug in the NDK under Windows, this problem occurs when only one file is compiled, and the workaround is to add an empty util.c file.The compiled library file is exported to the path of studio.




Step:6. Installation Run

Interface:


To view Log printing:


Development of JNI Engineering for Android Studio

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.