Android NDK-helloJNI

项目目录结构

CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)

add_library(hello-jni SHARED
            hello-jni.c)

# Include libraries needed for hello-jni lib
target_link_libraries(hello-jni
                      android
                      log)

编译一个shared library,hello-jin.so.

target_link_libraries中链接需要库

hello-jni.c

#include <string.h>
#include <jni.h>

/* This is a trivial JNI example where we use a native method
 * to return a new VM String. See the corresponding Java source
 * file located at:
 *
 *   hello-jni/app/src/main/java/com/example/hellojni/HelloJni.java
 */
JNIEXPORT jstring JNICALL
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                  jobject thiz )
{
#if defined(__arm__)
    #if defined(__ARM_ARCH_7A__)
    #if defined(__ARM_NEON__)
      #if defined(__ARM_PCS_VFP)
        #define ABI "armeabi-v7a/NEON (hard-float)"
      #else
        #define ABI "armeabi-v7a/NEON"
      #endif
    #else
      #if defined(__ARM_PCS_VFP)
        #define ABI "armeabi-v7a (hard-float)"
      #else
        #define ABI "armeabi-v7a"
      #endif
    #endif
  #else
   #define ABI "armeabi"
  #endif
#elif defined(__i386__)
#define ABI "x86"
#elif defined(__x86_64__)
#define ABI "x86_64"
#elif defined(__mips64)  /* mips64el-* toolchain defines __mips__ too */
#define ABI "mips64"
#elif defined(__mips__)
#define ABI "mips"
#elif defined(__aarch64__)
#define ABI "arm64-v8a"
#else
#define ABI "unknown"
#endif

    return (*env)->NewStringUTF(env, "Hello from JNI !  Compiled with ABI " ABI ".");//把C的字符串,转换为Java的字符串
}

hello-jni.java

  TextView tv = (TextView)findViewById(R.id.hello_textview);
    tv.setText( stringFromJNI() );
}
/* A native method that is implemented by the
 * 'hello-jni' native library, which is packaged
 * with this application.
 */
public native String  stringFromJNI();

/* This is another native method declaration that is *not*
 * implemented by 'hello-jni'. This is simply to show that
 * you can declare as many native methods in your Java code
 * as you want, their implementation is searched in the
 * currently loaded native libraries only the first time
 * you call them.
 *
 * Trying to call this function will result in a
 * java.lang.UnsatisfiedLinkError exception !
 */
public native String  unimplementedStringFromJNI();

/* this is used to load the 'hello-jni' library on application
 * startup. The library has already been unpacked into
 * /data/data/com.example.hellojni/lib/libhello-jni.so at
 * installation time by the package manager.
 */
static {
    System.loadLibrary("hello-jni");
}

1)定义native的方法

2)loadLibray

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. linux下jni环境搭建 参考:http://blog.csdn.net/zhouyuanjing/article/details/7553706 2. 编写HelloJni工程,在主Activity(本例:HelloJni.java)里声明native函数: 如下: public native String stringFromJNI(); public native double add(double a, double b); public native double sub(double a, double b); public native double multi(double a, double b); public native double div(double a, double b); static { System.loadLibrary("hello-jni"); } 3. 在根目录下创建 jni 目录(mkdir jni). 4. 利用命令生成相应的头文件,在根目录下执行:javah -classpath bin/classes -d jni com.xxx.hello.HelloJni ————————————— ——————— ^ ^ 包名 类名 5. 编写相应的.c文件(hello-jni.c) #include<string.h> #include<jni.h> JNIEXPORT jstring JNICALL Java_com_xxx_hello_HelloJni_stringFromJNI(JNIEnv *env, jobject obj) { return(*env)->NewStringUTF(env, "Hello World from JNI !"); } JNIEXPORT jdouble JNICALL Java_com_xxx_hello_HelloJni_add(JNIEnv *env, jobject obj, jdouble a, jdouble b) { return a + b; } JNIEXPORT jdouble JNICALL Java_com_xxx_hello_HelloJni_sub(JNIEnv *env, jobject obj, jdouble a, jdouble b) { return a - b; } JNIEXPORT jdouble JNICALL Java_com_xxx_hello_HelloJni_multi(JNIEnv *env, jobject obj, jdouble a, jdouble b) { return a * b; } JNIEXPORT jdouble JNICALL Java_com_xxx_hello_HelloJni_div(JNIEnv *env, jobject obj, jdouble a, jdouble b) { return a / b; } 6. jni目录下编写Android.mk文件 LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := hello-jni LOCAL_SRC_FILES := hello-jni.c include $(BUILD_SHARED_LIBRARY) 在根目录下运行:ndk-build xxx@xion-driver:~/android_env/eclipse/Workspace/HelloJni$ndk-build Install : libhello-jni.so => libs/armeabi/libhello-jni.so 可以看到已经正确的生成了libhello-jni.so共享库了。 7. Eclipse运行该工程即可。 ~~完~~

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值