以前在 Android Studio 3.2.1上vuh库使用的例子 中实现了一个使用 vuh
库的例子。 那个例子中的 vuh
库是我们编译好 libvuh.so
之后直接引用的,我们下面实现通过直接编译代码实现整合。
尝试过使用 ExternalProject_add
跟 include
的方式包含 vuh
库,但是都不是很成功。
其中 ExternalProject_add
导入的项目只能编译一次,即使指定 BUILD_ALWAYS 1
也没用,这个应该是 Ninja
导致的问题,导致当出现多个 ABI
或者 vuh
库代码变动之后,不能重新编译,出现各种编译错误。
使用 include
包含的项目会导致路径信息不正确,无法找到源代码文件。
最后使用 add_subdirectory
实现。
修改之后的几个关键文件如下:
注意: VUH_ROOT_DIR
这个变量中指定 vuh
库代码的位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# For more information about using CMake with Android Studio, read the # documentation: https://d.android.com/studio/projects/add-native-code.html # Sets the minimum version of CMake required to build the native library. cmake_minimum_required(VERSION 3.8) # for Vulkan SET(Vulkan_INCLUDE_DIR ${ANDROID_NDK}/sources/third_party/vulkan/src/include/) SET(Vulkan_LIBRARIES ${ANDROID_NDK}/platforms/${ANDROID_PLATFORM}/arch-${ANDROID_ARCH_NAME}/usr/lib) if(X86_64) SET(Vulkan_LIBRARIES ${Vulkan_LIBRARIES}64) endif() SET(Vulkan_LIBRARIES ${Vulkan_LIBRARIES}/libvulkan.so) add_library(vulkan SHARED IMPORTED) set_target_properties(vulkan PROPERTIES IMPORTED_LOCATION ${Vulkan_LIBRARIES}) # for vuh add_definitions(-DVK_USE_PLATFORM_ANDROID_KHR=1 -DVULKAN_HPP_TYPESAFE_CONVERSION=1) SET(VUH_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../vuh/) SET(VUH_BUILD_TESTS OFF) SET(VUH_BUILD_DOCS OFF) SET(VUH_BUILD_EXAMPLES OFF) add_subdirectory(${VUH_ROOT_DIR}src/ ${CMAKE_CURRENT_SOURCE_DIR}/build/) # for example SET(Vuh_INCLUDE_PATH ${VUH_ROOT_DIR}src/include) include_directories(${Vulkan_INCLUDE_DIR}) include_directories(${Vuh_INCLUDE_PATH}) # Creates and names a library, sets it as either STATIC # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. # Gradle automatically packages shared libraries with your APK. add_library( # Sets the name of the library. native-lib # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). src/main/cpp/native-lib.cpp) add_dependencies(native-lib vuh) # Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by # default, you only need to specify the name of the public NDK library # you want to add. CMake verifies that the library exists before # completing its build. 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 android) # Specifies libraries CMake should link to your target library. You # can link multiple libraries, such as libraries you define in this # build script, prebuilt third-party libraries, or system libraries. target_link_libraries( # Specifies the target library. native-lib vulkan vuh ${log-lib}) |
注意:由于 vuh
库需要 CMake 3.8
。因此,我们需要手工指定CMake
版本为3.10.2
。
如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
apply plugin: 'com.android.application' android { compileSdkVersion 28 defaultConfig { applicationId "com.mobibrw.vuhandroid" minSdkVersion 24 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { version "3.10.2" cppFlags "-std=c++14 -v -g" } } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } externalNativeBuild { cmake { version "3.10.2" path "CMakeLists.txt" } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' } |
如果出现如下错误:
1 2 |
CMake Error: CMake was unable to find a build program corresponding to "Ninja". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool. -- Configuring incomplete, errors occurred! |
则执行如下操作:
1 |
$ brew install ninja |
如果出现如下错误:
1 2 3 |
* What went wrong: Execution failed for task ':app:transformNativeLibsWithMergeJniLibsForDebug'. > More than one file was found with OS independent path 'lib/armeabi-v7a/libvuh.so' |
则删除代码中的 jniLibs/armeabi-v7a/libvuh.so
即可解决问题。
完整的例子点击此处下载 vuhAndroid
参考链接
- How to use CMake to add Third Party Libraries to your Project
- Use ExternalProject_Add to include Opus in Android
- Unknown CMake command “ExternalProject_Add”
- How to use CMake ExternalProject_Add or alternatives in a cross platform way?
- ExternalProject
- Custom Directory for CMake Library Output
- [CMake] Force rebuild of external project with Ninja
- Ninja does not always rebuild resource files (*.rc) when they were changed
- How to force an action target to run on every build?
- ExternalProject: Additional command specified by COMMAND stop working
- [CMake] External projects and make clean
- Android 上的 Vulkan 着色器编译器
- How to properly find and link NDK shaderc lib into your project in Android Studio?>
- OpenGL Programming/Android GLUT Wrapper