AS中JNI項(xiàng)目創(chuàng)建以及項(xiàng)目中遇到的坑

1、首先先創(chuàng)建一個(gè)普通的Android項(xiàng)目,


2、創(chuàng)建一個(gè)JniTest類,并且在類中創(chuàng)建native方法;


3、使用javaH生成.h文件,步驟如下:

打開(kāi)Terminal,進(jìn)入到工程的main目錄下 輸入一下命令


然后就會(huì)在java目錄下面生成com_yyz_test_jnidemo_JniTest.h

文件,然后在main目錄下面創(chuàng)建一個(gè)jni文件夾,把此文件拷貝到j(luò)ni目錄下面,可以重新命名JniTest.h(可以根據(jù)個(gè)人愛(ài)好命名),如圖


4、在jni目錄下面根據(jù)JniTest.h創(chuàng)建對(duì)應(yīng)的C文件,c文件中的內(nèi)容如下:

#include

#include

#include "JniTest.h"

JNIEXPORT jstring JNICALL Java_com_yyz_test_jnidemo_JniTest_getHello

(JNIEnv *env,jobject object){

return (*env)->NewStringUTF(env,"hello world From C");

}

5、創(chuàng)建Android.mk文件,內(nèi)容如下:

前兩項(xiàng)是固定寫(xiě)法,LOCAL_MODULE必須和JniTest中的sayJni保持一致,LOCAL_SRC_FILES 代表c文件的名稱 ,include $(BUILD_SHARED_LIBRARY)固定寫(xiě)法


LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := sayJni

LOCAL_SRC_FILES := c_test.c

include $(BUILD_SHARED_LIBRARY)

6、在項(xiàng)目App根目錄下面創(chuàng)建CMakeLists.txt,文件內(nèi)容如下:

cmake_minimum_required(VERSION 3.4.1)

add_library(# Sets the name of the library.

# 設(shè)置so文件名稱.

sayJni

# Sets the library as a shared library.

SHARED

# 設(shè)置這個(gè)so文件為共享.

# Provides a relative path to your source file(s).

# 設(shè)置這個(gè)so文件為共享.

src/main/jni/c_test.c)

find_library(# Sets the name of the path variable.

? ? log-lib

? ? # Specifies the name of the NDK library that

# you want CMake to locate.

? ? log )

target_link_libraries(# Specifies the target library.

? ? # 制定目標(biāo)庫(kù).

? ? sayJni

# Links the target library to the log library

# included in the NDK.

? ? ${log-lib} )

7,在App中的build.gradle文件中defaultConfig添加


ndk{

moduleName"sayJni"

}

externalNativeBuild {

cmake {

cppFlags""

? ? ? ? //生成多個(gè)版本的so文件

? ? ? ? abiFilters'arm64-v8a','armeabi-v7a'

? ? }

}

在android下面添加


externalNativeBuild {

cmake {

path"CMakeLists.txt" //編譯后so文件的名字

? ? }

}

具體如下圖所示:


6,rebuild項(xiàng)目,就會(huì)在app/build/intermediates/cmake/debug/obj下面有對(duì)應(yīng)的.so文件。

7,項(xiàng)目中可能遇到的坑。

坑1、javah不是內(nèi)部或者外部命令? 解決比較簡(jiǎn)單,檢查環(huán)境變量配置。

坑2,編譯時(shí)候,報(bào)下面的錯(cuò)誤:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

CMake Error in CMakeLists.txt:? The CMAKE_CXX_COMPILER:? D:/Android/SDK/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe? is not a full path to an existing compiler tool.? Error:executing external native build for cmake D:/Android/Projects/Join/app/CMakeLists.txt?

或者 Cause: executing external native build for cmake

此錯(cuò)誤網(wǎng)上的解決方法,基本沒(méi)什么用處,唯一正確的方法是,AS自帶的NDK開(kāi)發(fā)包里面的東西不全,需要去官網(wǎng)下載一個(gè)最新的NDK包,然后去重新配置環(huán)境變量,重啟,然后rebuild一下,就搞定了。

7希望此文章對(duì)你有所幫助。下面是代碼的鏈接:GitHub - yyz1218/JniDemo: As中創(chuàng)建JNI項(xiàng)目,并且聲稱.so文件,調(diào)用c

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容