JNI(2) Android NDK 环境搭建与使用
Place your native sources under <project>/jni/...Create <project>/jni/Android.mk to describe your native sources to the NDK build systemOptional: Create <project>/jni/Application.mk.Build your native code by running the ‘ndk-build‘ script from your project‘s directory. It is located in the top-level NDK directory:cd <project><ndk>/ndk-buildThe build tools copy the stripped, shared libraries needed by your application to the proper location in the application‘s project directory.Finally, compile your application using the SDK tools in the usual way. The SDK build tools will package the shared libraries in the application‘s deployable .apk file.
package com.xcl.jnidemo5; /** * 演示NDK开发 * * author:xcl * date:2014-3-20 */ import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); TextView tv = new TextView(this); tv.setText(stringFromJNI() ); setContentView(tv); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public native String stringFromJNI(); static { System.out.println(System.getProperty("java.library.path")); System.loadLibrary("jnicplus5"); } }2. 与Jni相关的处理
D:\AppWork\XExample\workspace\jnidemo5\src>javah com.xcl.jnidemo5.MainActivity D:\AppWork\XExample\workspace\jnidemo5\src>dir 驱动器 D 中的卷是 Data 卷的序列号是 0EC2-012C D:\AppWork\XExample\workspace\jnidemo5\src 的目录 2014/03/29 21:23 <DIR> . 2014/03/29 21:23 <DIR> .. 2014/03/29 21:19 <DIR> com 2014/03/29 21:23 529 com_xcl_jnidemo5_MainActivity.h 1 个文件 529 字节 3 个目录 18,683,146,240 可用字节
/** * 演示NDK开发 * JNI的C实现 * author:xcl * date:2014-3-20 */ #include <string.h> #include <jni.h> /* * Class: com_xcl_jnidemo5_MainActivity * Method: stringFromJNI * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_xcl_jnidemo5_MainActivity_stringFromJNI (JNIEnv * env, jobject jobj) { #if defined(__arm__) #if defined(__ARM_ARCH_7A__) #if defined(__ARM_NEON__) #define ABI "armeabi-v7a/NEON" #else #define ABI "armeabi-v7a" #endif #else #define ABI "armeabi" #endif #elif defined(__i386__) #define ABI "x86" #elif defined(__mips__) #define ABI "mips" #else #define ABI "unknown" #endif // return (*env)->NewStringUTF(env, "Hello from JNI ! Compiled with ABI " ABI "."); return (*env)->NewStringUTF(env, "Hello from JNI !"); }
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := jnicplus5 LOCAL_SRC_FILES := jnicplus5.c include $(BUILD_SHARED_LIBRARY)
APP_ABI := all
D:\AppWork\XExample\workspace\jnidemo5\jni>C:\Ndk\ndk\ndk-build [armeabi-v7a] Compile thumb : jnicplus5 <= jnicplus5.c [armeabi-v7a] SharedLibrary : libjnicplus5.so [armeabi-v7a] Install : libjnicplus5.so => libs/armeabi-v7a/libjnicplus5.so [armeabi] Compile thumb : jnicplus5 <= jnicplus5.c [armeabi] SharedLibrary : libjnicplus5.so [armeabi] Install : libjnicplus5.so => libs/armeabi/libjnicplus5.so [x86] Compile : jnicplus5 <= jnicplus5.c [x86] SharedLibrary : libjnicplus5.so [x86] Install : libjnicplus5.so => libs/x86/libjnicplus5.so [mips] Compile : jnicplus5 <= jnicplus5.c [mips] SharedLibrary : libjnicplus5.so [mips] Install : libjnicplus5.so => libs/mips/libjnicplus5.so
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。