0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

试用了下Android里的jni

Posted at

最近在做android开发,需要用到同事写的c++代码。
据说jni(Java Native Interface)可以做到。
简单写了个sample:
https://github.com/jus18tin/jniSample

步骤1 Android.mk
新建一个jni文件夹,里面放下面两个文件:
Android.mk <-- make用文件
jni_sample.cpp <-- 实际代码,用来连接java和你需要调用c++

Android.mk
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE   := native_sample
LOCAL_SRC_FILES := jni_sample.cpp

include $(BUILD_SHARED_LIBRARY)

步骤2 jni_sample.cpp

jni_sample.cpp
#include <jni.h>

extern "C" {
	JNIEXPORT jint JNICALL Java_com_example_jnisample_JniSampleActivity_testJni(JNIEnv* env, jobject, jint i)
	{
		return i+1;
	}
}

需要注意函数名字 包名_文件名_函数名

步骤3 Acivity
首先需要加载新建的这个模块,这次用模块名是Android.mk里写的native_sample,
定义函数

Activity
static {
    System.loadLibrary("native_sample");
}
private native int testJni(int i);

就好了- -
(各种复杂的东西日后有空补充)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?