先说环境win10,android studio3.5.3,ndk-r18b,assimp-4.1.0
为啥是assimp-4.1.0,因为最开始去网上找例子的时候是4.1.0版本,就没改了,我想5.0.1也差不多,只是我只编译成功了arm64-v8a的,其他的编译失败了,也不知道是不是参数不对,先这样吧。关于ndk最低版本我没测试,我试过几个版本都没问题,r18b是我最开始用的就没改
首先创建个项目,选c++项目
我创建的项目的CMakeLists.txt文件默认在cpp文件夹下,如果不同根据自己的情况进行修改
下载assimp-4.1.0,再下载AssimpAndroid的代码,AssimpAndroid是一个显示模型的demo,我还不太会assimp,所以先用别人的代码来测试
将assimp-4.1.0整个文件夹复制到cpp文件夹下,修改文件夹名
将AssimpAndroid\app\src\main\externals\glm-0.9.7.5\内的glm整个文件夹复制到cpp文件夹下
在cpp的CMakeLists.txt内添加代码
aux_source_directory (${CMAKE_SOURCE_DIR}/glm/ GLM)
aux_source_directory (${GLM}/gtc GTC)
aux_source_directory (${GLM}/gtx GTX)
aux_source_directory (${GLM}/detail DETAIL)
target_include_directories(native-lib PRIVATE ${GLM} ${GTC} ${DETAIL} ${GTX})
Ran'app'一下,就是跑一下app,没报错,继续
再在cpp的CMakeLists.txt内添加代码
add_subdirectory(${CMAKE_SOURCE_DIR}/Assimp)
include_directories(${CMAKE_SOURCE_DIR}/Assimp/include)
set(ATLAS_ENGINE_LIBS assimp)
set(ATLAS_ENGINE_SYSTEM_LIBS android EGL GLESv2 GLESv3 m)
target_link_libraries(
native-lib
${ATLAS_ENGINE_LIBS}
${ATLAS_ENGINE_SYSTEM_LIBS}
${log-lib} )
再Ran'app'一下,嗯,报错了
Error while executing process X:\sdk\cmake\3.10.2.4988404\bin\ninja.exe with arguments {-C Y:\hyq\TestAssimp\app\.cxx\cmake\debug\arm64-v8a assimp assimp_cmd native-lib unit}
也不知道是啥错误,只看到了'arm64-v8a',然后我在build.gradle的defaultConfig内添加了
ndk{
abiFilters 'arm64-v8a'
}
还是报错,对ndk实在是太不熟悉了,东找找西找找碰运气把其他demo里的东西复制到build.gradle居然成功了
externalNativeBuild {
cmake {
arguments '-DASSIMP_BUILD_ZLIB=ON', '-DASSIMP_BUILD_TESTS=OFF',
'-DASSIMP_NO_EXPORT=ON', '-DASSIMP_BUILD_ASSIMP_TOOLS=OFF',
'-DANDROID_ARM_NEON=TRUE', '-DCMAKE_BUILD_TYPE=Release'
cppFlags "-fexceptions -frtti -std=c++11 -O3"
}
}
ndk{
abiFilters 'arm64-v8a'
}
Ran'app'一下,不报错,那就继续,在native-lib.cpp内添加代码
#include
#include
#include
Ran'app'一下,报错,还是上边的那个错误,不过把错误拉到最底下看到一条
fatal error: 'assimp/config.h' file not found
在Assimp/include/assimp文件夹内确实没有config.h文件,但是在app/.cxx/cmake/debug/arm64-v8a/Assimp/include/assimp文件夹内有复制到Assimp/include/assimp内,Ran'app'一下,成功,自此Assimp已经编译成功了,现在要试着运行demo了
AssimpAndroid这个demo有用到opencv,只是用到opencv的读取图片功能,可以自己试着去掉,我为了方便就加上了opencv
复制opencv的libopencv_java3.so到app/libs/arm64-v8a文件夹下,没有arm64-v8a文件夹自己创建,复制opencv的include到app/src/main/cpp/opencv文件夹下,没有opencv文件夹自己创建,在CMakeLists.txt内添加代码
include_directories(${CMAKE_SOURCE_DIR}/Assimp/include ${CMAKE_SOURCE_DIR}/opencv/include)
set(distribution_DIR ../../../../libs)
add_library(opencv_java3 SHARED IMPORTED )
set_target_properties(opencv_java3
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/${ANDROID_ABI}/libopencv_java3.so
)
target_link_libraries(
native-lib
opencv_java3
${ATLAS_ENGINE_LIBS}
${ATLAS_ENGINE_SYSTEM_LIBS}
${log-lib} )
再在build.gradle的defaultConfig内添加
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
复制AssimpAndroid/app/src/main/jni文件夹下的jniCalls文件夹和nativeCode文件夹,在CMakeLists.txt内添加代码
add_library(
native-lib
SHARED
native-lib.cpp
jniCalls/assimpActivity.cpp
jniCalls/gestureClass.cpp
jniCalls/myGLRenderer.cpp
nativeCode/common/assimpLoader.cpp
nativeCode/common/assimpLoader.h
nativeCode/common/misc.cpp
nativeCode/common/misc.h
nativeCode/common/myGLCamera.cpp
nativeCode/common/myGLCamera.h
nativeCode/common/myGLFunctions.cpp
nativeCode/common/myGLFunctions.h
nativeCode/common/myGLM.h
nativeCode/common/myJNIHelper.cpp
nativeCode/common/myJNIHelper.h
nativeCode/common/myLogger.h
nativeCode/common/myShader.cpp
nativeCode/common/myShader.h
nativeCode/modelAssimp/modelAssimp.cpp
nativeCode/modelAssimp/modelAssimp.h
)
Ran'app'一下,报错
fatal error: 'modelAssimp.h' file not found
打开modelAssimp.h,右上角有个Sync Now,点击
再修改#include的路径,先修改myGLM.h的
#include
#include
#include
#include
改成
#include <../../glm/glm.hpp>
#include <../../glm/gtc/matrix_transform.hpp>
#include <../../glm/gtc/quaternion.hpp>
#include <../../glm/gtx/quaternion.hpp>
这个问题应该是native-lib.cpp是主体,所以其他文件夹下的引入要进行文件夹跳转吧,jni半路出家,脑壳疼
#include "modelAssimp.h"
#include "myJNIHelper.h"
改成
#include "../nativeCode/modelAssimp/modelAssimp.h"
#include "../nativeCode/common/myJNIHelper.h"
路径要一个一个改成对应的文件夹,就不多说了
fatal error: 'gl3stub.h' file not found
把#include "gl3stub.h"删掉,再把myGLFunctions.cpp内的&& gl3stubInit()删掉,这段if内的代码是判读opengles版本的,gl3stub.h也就这里用了下,也不知道是gl3stub.h是哪个库的
implicit instantiation of undefined template 'std::__ndk1::vector >'
添加#include
复制AssimpAndroid的Java代码到项目,复制assimp_layout.xml到layout文件夹,复制assets到项目,由于我的项目是androidx的所以要把android.support.v4.view改成androidx,最后把AndroidManifest.xml的.MainActivity改成com.anandmuralidhar.assimpandroid.AssimpActivity,记得把AssimpActivity的System.loadLibrary改成自己的库,我的是native-lib
最后效果
GitHub