Android JNI learning practices (1)

Source: Internet
Author: User

I recently learned about JNI and learned how to do it. I changed the 2D engine of the game "ant financial" to JNI, encapsulated the code, and improved the running speed.

The preliminary steps are as follows: 1. Write the Java file and determine the JNI Interface Name:

package com.howfun.android.antguide.hf2d_jni;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.widget.TextView;public class hf2d extends Activity{      public void onCreate(Bundle savedInstanceState) {      Log.d("jni", "oncreate()");      super.onCreate(savedInstanceState);      /*       * Create a TextView and set its content. the text is retrieved by calling       * a native function.       */      TextView tv = new TextView(this);      tv.setText(stringFromJNI());      setContentView(tv);   }      public native String stringFromJNI() ;   static {      System.loadLibrary("hf2d");   }}

Important:

public native String stringFromJNI() ;

Confirm the interface.

2. compile the project and generate hf2d. Class to $ myproject/bin/($ myproject refers to my android project directory). Run:

$ Javah-JNI com. howfun. Android. antguide. hf2d_jni.hf2d

Generate hf2d. h In the current directory. Note that the directory should not be run. Just run it in Bin.

Test hf2d. h to $ myproject/JNI/(create this JNI directory) and write hf2d. C as follows:

#include <string.h>#include <jni.h>#include "hf2d.h"JNIEXPORT jstring JNICALL Java_com_howfun_android_antguide_hf2d_1jni_hf2d_stringFromJNI  (JNIEnv *env, jobject thiz){    return (*env)->NewStringUTF(env, "Hello from JNI !");}

3. Write the Android. mk file and place it in $ myproject/JNI/, as shown below:

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := hf2d LOCAL_SRC_FILES := hf2d.cinclude $(BUILD_SHARED_LIBRARY)

What do I mean by specific label reference: $ ndk/android-ndk-r4b/docs/ANDROID-MK.TXT, very detailed.

4. Download the android ndk package (My R4 version ),

$ Export ndk = My/ndk/path

$ CD $ ndk/android-ndk-r4b /,

$ Export ndk_project_path = "~ /Workspace/antguide"

$./Ndk-build

The libhf2d. So file is generated here.

In step 1, call:

static {      System.loadLibrary("hf2d");   }

Load the dynamic library. Note that the preceding "lib" is removed.

Running APK will display greetings from the remote JNI.

Note: Do not underline the class path. Otherwise, you need to add "1" after the underline Of the function name, as the opposite is typical:

Package name: Package com. howfun. Android. antguide. hf2d_jni;

The result is generated as follows:

Java_com_howfun_android_antguide_hf2d_1jni_hf2d_stringfromjn has an "1", FT...

This chapter is complete. To be continued: Port hf2d Java engine to JNI

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.