1. 程式人生 > >android實現app通過jni呼叫C/C++方法

android實現app通過jni呼叫C/C++方法

本文實現在android app中使用呼叫jni庫呼叫本地C/C++方法。

1.新建android工程

2.新建java上層方法

本例子在工程中新建 cn.landsem.jnistudy 包,在其中新建TestManager類用於呼叫本地C/C++方法,該類的程式碼如下:

package cn.landsem.jnistudy;

import android.util.Log;

public class TestManager {
	public static String TAG = "TestManager";
	static {
        try {  
            System.loadLibrary("lstest");  
        } catch (Exception e) {
        	e.printStackTrace();
        	Log.d(TAG,e.getMessage());
        }  		
		Log.d(TAG, "native_init");
		nativeInit();
	}
	
	public int add(int i,int j) {
		Log.d(TAG,"add");
		return nativeAdd(i,j);
	}

	private static native void nativeInit();
	private static native int nativeAdd(int i,int j);
}

3.建立jni標頭檔案

開啟dos命令視窗,切換到工程目錄下的“bin\classes”目錄,輸入javah -jni cn.landsem.jnistudy.TestManager命令,命令執行成功後會在該目錄下生成對應的jni標頭檔案,如本文中完成上述命令會生成 cn_landsem_jnistudy_TestManager.h 檔案,檔案內容如下:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class cn_landsem_jnistudy_TestManager */

#ifndef _Included_cn_landsem_jnistudy_TestManager
#define _Included_cn_landsem_jnistudy_TestManager
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     cn_landsem_jnistudy_TestManager
 * Method:    nativeInit
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_cn_landsem_jnistudy_TestManager_nativeInit(JNIEnv *, jclass);

/*
 * Class:     cn_landsem_jnistudy_TestManager
 * Method:    nativeAdd
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_cn_landsem_jnistudy_TestManager_nativeAdd(JNIEnv *, jclass, jint, jint);

#ifdef __cplusplus
}
#endif
#endif

4.新建jni實現

在工程中新建jni目錄,將上部操作中生成的jni標頭檔案拷貝到該目錄,新建一個c++原始檔實現jni中定義的方法,如本文在jni目錄下新建cn_landsem_jnistudy_TestManager.cpp檔案用於實現上述方法,該方法實現程式碼如下:

#include "android_runtime/AndroidRuntime.h"
#include "JNIHelp.h"
#include "jni.h"
#include "utils/Log.h"
#include "utils/misc.h"
#include "cn_landsem_jnistudy_TestManager.h"
#include <android/log.h> 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>

#define LOG_TAG "Test_JNI"

JNIEXPORT void JNICALL Java_cn_landsem_jnistudy_TestManager_nativeInit
  (JNIEnv *, jclass)  {
	ALOGD("call Java_cn_landsem_jnistudy_TestManager_nativeInit");
}

/*
 * Class:     cn_landsem_jnistudy_TestManager
 * Method:    native_add
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_cn_landsem_jnistudy_TestManager_nativeAdd
  (JNIEnv *, jclass, jint, jint) {
	ALOGD("call Java_cn_landsem_jnistudy_TestManager_nativeAdd");
	return 0;
}

5、生成jni so庫

在jni目錄下新建Android.mk檔案用於編譯生成jni庫,該檔案內容如下:

# Copyright (C) 2010 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)
LOCAL_SRC_FILES := cn_landsem_jnistudy_TestManager.cpp

LOCAL_SHARED_LIBRARIES := \
	libcutils \
	libutils \
    liblog  \

LOCAL_MODULE:= liblstest

include $(BUILD_SHARED_LIBRARY)

6、包含jni庫到工程中

在工程libs目錄下新建armeabi目錄和armeabi-v7a目錄,將生成的jni庫放到該目錄下。

7、呼叫

在工程中呼叫 TestManager 類即可測試。

8、原始碼下載