Android NDK-helloJNI

本文详细介绍了如何使用CMake构建并链接JNI共享库hello-jni.so,包括添加库依赖、定义native方法和加载Library。还展示了HelloJni.java中的相关Java代码和C/C++接口调用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目目录结构

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值