Android NDK-helloJNI
生活随笔
收集整理的這篇文章主要介紹了
Android NDK-helloJNI
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
項(xiàng)目目錄結(jié)構(gòu)
CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)add_library(hello-jni SHAREDhello-jni.c)# Include libraries needed for hello-jni lib target_link_libraries(hello-jniandroidlog)編譯一個(gè)shared library,hello-jin.so.
target_link_libraries中鏈接需要庫(kù)
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" #endifreturn (*env)->NewStringUTF(env, "Hello from JNI ! Compiled with ABI " ABI ".");//把C的字符串,轉(zhuǎn)換為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
總結(jié)
以上是生活随笔為你收集整理的Android NDK-helloJNI的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Cmake-5
- 下一篇: Android-NDK-hello-jn