Android Studio JNI Development Project

Source: Internet
Author: User

Android Studio JNI Development Project

After creating a new project using Android sudio, record the basic steps for creating the NDK project.
This article will reach: 1. Create an NDK project 2. Output a Log statement in JNI 3. Specify the abi version of The so library to be compiled 4. Solve the Problem in creating an NDK Project
Step: 1. Add the native Interface and write the native Interface and System. loadLibrary. P.S: In oncreate(example, setText () is executed for r.id.txt. Therefore, you need to modify the xml layout file according to the normal development steps.
The Code is 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. Execute Build-> Make Project


Perform this step, verify that there are no other errors in the project, compile the project, and generate. class file .. the path for generating the class file is under app_path/build/intermediates/classes/debug. for example:


Step: 3. Generate the c header file using javah

Click "View-> Tool Windows-> Terminal" to run the Terminal command line Tool in Studio. Run the following command to generate the C Language header file. Note that Run the javah command in the \ app \ src \ main directory to generate the. h file. Under the \ app \ src \ main path, you can see it directly in the Studio project structure.
Command: javah-d jni-classpath The specific operation diagram of lab. sodino. jnitest. MainActivity 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 "The main version is 51 to 50, this compiler supports the latest main version" is caused by the installation of two jdk versions on the computer, and the old jdk is currently used. Delete the old jdk and run the java version command to display that the current jdk is the latest 1.7. For example:

The final result is as follows:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD48cD48aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20141216/2014121608565552.png" alt = "\"/>



Step: 4. Edit the c file

The method in the header file is implemented in the main. c file. The specific function is to return a String directly and print related logs using android_log. The Code is as follows:
/* DO NOT EDIT THIS FILE - it is machine generated */#include 
    
     #include #ifndef LOG_TAG#define 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
    

After that, we execute another "Build-> Make Project" and find that "Messages Gradle Build" will prompt the following:
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 prompt shows that the NDK is not configured, and you need to configure the NDK path in the local. properties file of the project. Okay. The prompt is clear. Let's proceed to the next step.

Step: 5. Configure NDK. This Step includes two actions: 1. Specify the ndk path
2. Modify the build. gradle configuration file in the build. gradle configuration project. \ App \ build. gradle. Add it to the ultconfig Branch
    ndk {        moduleName "JniTest"        ldLibs "log", "z", "m"        abiFilters "armeabi", "armeabi-v7a", "x86"    }
The so database name specified in the preceding configuration code is called JniTest. The library used for linking corresponds to LOCAL_LDLIBS In the android. mk file, and the so database under the specified three abi architectures. For example:
Then, execute "Build-> Rebuild Project" to compile the so file. However, a problem occurs 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 inexplicable .. a few times tossing, find a video out of the approximate cause and solution: source see Youtube video start: https://www.youtube.com/watch? V = okLKfxfbz40 # t = 362 an NDK bug in Windows. This problem occurs when only one file is compiled. The solution is to add an empty util. c file. The compiled library file is output to the path by Studio.




Step: 6. Install and run

Interface:


View Log printing:


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.