android 调用jni
(用jni,jna速度差百倍)
1 、 android 里 Hello.java
package com.example.administrator.t1; public class Hello { static { System.loadLibrary("sayhello"); } public native void say(); public native String getStringFromJNI(); }
2、用javah生成头文件 到classes目录下
E:\andprogram\andpro\player\JnitestRgb\app\build\intermediates\javac\debug\classes>set classpath=E:/andprogram/program/newsdk/platforms/android-30/android.jar;.
后面的;.重要 ,不然把以前的覆盖了
E:\andprogram\andpro\player\JnitestRgb\app\build\intermediates\javac\debug\classes>javah com.example.administrator.t1.Hello
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_example_administrator_t1_Hello */ #ifndef _Included_com_example_administrator_t1_Hello #define _Included_com_example_administrator_t1_Hello #ifdef __cplusplus extern "C" { #endif /* * Class: com_example_administrator_t1_Hello * Method: say * Signature: ()V */ JNIEXPORT void JNICALL Java_com_example_administrator_t1_Hello_say (JNIEnv *, jobject); /* * Class: com_example_administrator_t1_Hello * Method: getStringFromJNI * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_example_administrator_t1_Hello_getStringFromJNI (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif
3、下载ndk ,在ubuntu解压
vim /etc/profile,添加
export ANDROID_NDK=/home/wang/program/android-ndk-r21d
export PATH=$ANDROID_NDK:$PATH
source
ndk-build -v
安装好了
4、ubuntu上建立jnitest1目录
Application.mk
jni //目录
Android.mk
test.c
test.c
#include <stdio.h> #include <jni.h> JNIEXPORT void JNICALL Java_com_example_administrator_t1_Hello_say (JNIEnv * env, jobject obj){ // LOGI("Hello World~~~"); return; } JNIEXPORT jstring JNICALL Java_com_example_administrator_t1_Hello_getStringFromJNI (JNIEnv * env, jobject obj){ //LOGI("here will return a String 'Hello World'"); jstring newstring = (*env)->NewStringUTF(env,"Hello World"); const char* str= (*env)->GetStringUTFChars(env, newstring ,NULL);//使用 GetStringUTFChars方法将"Hello World"转成UTF-8格式的string的对象str (*env)->ReleaseStringUTFChars(env, newstring, str);//释放str对象的空间>,如果不显示的调用的话,JVM中会一直保存该对象,不会被垃圾回收器回收,因此就 会导致内存溢出 return newstring; }
Android.mk
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) #LOCAL_MODULE表示生成的动态库名为sayhello LOCAL_MODULE := sayhello #LOCAL_SRC_FILES表示使用到的类 LOCAL_SRC_FILES := test.c #LOCAL_SRC_FILES := sayhello.cpp LOCAL_LDLIBS := -lm -llog -ljnigraphics include $(BUILD_SHARED_LIBRARY)
Application.mk
# Uncomment this if you're using STL in your project # See CPLUSPLUS-SUPPORT.html in the NDK documentation for more information # APP_STL := stlport_static #APP_BUILD_SCRIPT := Android.mk #APP_ABI := armeabi-v7a arm64-v8a x86 x86_64 APP_ABI := all # Min runtime API level APP_PLATFORM=android-16
ndk-build 生成 libs,拷贝到android studio工程目录下
5、
、build.gradle(Module:app)里在buildTypes 后面添加 sourceSets 部分
buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } sourceSets { main { jniLibs.srcDirs = ['libs'] } }
6、android studio调用
new Hello().getStringFromJNI()
注意test.c里函数名要和包路径对应