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、原始碼下載
原始碼可以從如下路徑下載:http://download.csdn.net/detail/yxtouch/9620436
相關文章
- android使用JNI呼叫C,C++程式AndroidC++
- C++庫封裝JNI介面——實現java呼叫c++C++封裝Java
- Android JNI實現Java與C/C++互相呼叫,以及so庫的生成和呼叫(JNI方式呼叫美圖秀秀so)AndroidJavaC++
- Android 通過JNI實現守護程式Android
- Music Studio專案心得--JNI實現C++呼叫JAVAC++Java
- 通過JNI對C++進行封裝C++封裝
- Cocos2dx之通過JNI實現c/c++和Android的java層函式互調C++AndroidJava函式
- java呼叫c++動態庫之jni呼叫JavaC++
- 利用swig實現python呼叫C/C++的方法PythonC++
- PHP通過反射實現物件的成員方法呼叫PHP反射物件
- Android - JNI加入標準C++檔案AndroidC++
- (轉)Linux下 C++呼叫C 實現socket網路通訊程式設計LinuxC++程式設計
- Android Studio NDK開發-JNI呼叫Java方法AndroidJava
- android使用C/C++呼叫SO庫AndroidC++
- Android C++層使用Binder通訊的方法AndroidC++
- Android JNI開發系列之Java與C相互呼叫AndroidJava
- 模板方法模式(c++實現)模式C++
- Android Binder實現示例(C/C++層)AndroidC++
- HTTPS通訊的C++實現HTTPC++
- Android C++層列印呼叫棧AndroidC++
- WebAssembly實踐指南——C++和Rust透過wasmtime實現相互呼叫例項WebC++RustASM
- JAVA JNI 呼叫C語言 DemoJavaC語言
- C語言 JNI 呼叫JAVA DemoC語言Java
- C# 呼叫 C++ 生成的 dll 關鍵實現部分C#C++
- 簡單實現Android NDK編譯jni呼叫動態庫開發Android編譯
- C++非同步呼叫利器future/promise實現原理C++非同步Promise
- C# 通過socket實現UDP 通訊C#UDP
- C/C++封裝庫ocicpplib呼叫Oracle儲存過程的方法C++封裝Oracle儲存過程
- 通過 App Groups 實現程式間通訊APP
- JNI開發系列③C語言呼叫Java欄位與方法C語言Java
- android binder c++層-客戶端(c++) 呼叫 服務端(c++) 例子AndroidC++客戶端服務端
- 通訊錄管理系統(C++實現)C++
- 實現通過COM元件方式實現java呼叫C#寫的DLL檔案的完整demo元件JavaC#
- 27.移除元素(c++方法實現)C++
- QT:用QWebSocket實現webchannel,實現C++與HTML通訊QTWebC++HTML
- Python呼叫C++編寫的方法PythonC++
- C++呼叫C介面C++
- JNI/NDK開發指南(6):C/C++訪問Java例項方法和靜態方法C++Java