macOS Mojave (10.14.3) Android Studio 3.3.1 NDK 19.1.5304403 导入并构建Vuh项目

以前在 Android Studio 3.2.1上vuh库使用的例子 中实现了一个使用 vuh 库的例子。 那个例子中的 vuh 库是我们编译好 libvuh.so 之后直接引用的,我们下面实现通过直接编译代码实现整合。

尝试过使用 ExternalProject_addinclude 的方式包含 vuh 库,但是都不是很成功。

其中 ExternalProject_add 导入的项目只能编译一次,即使指定 BUILD_ALWAYS 1 也没用,这个应该是 Ninja 导致的问题,导致当出现多个 ABI 或者 vuh 库代码变动之后,不能重新编译,出现各种编译错误。

使用 include 包含的项目会导致路径信息不正确,无法找到源代码文件。

最后使用 add_subdirectory实现。

修改之后的几个关键文件如下:

注意: VUH_ROOT_DIR 这个变量中指定 vuh 库代码的位置

# 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 。

如下:

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'
}

如果出现如下错误:

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!

则执行如下操作:

$ brew install ninja

如果出现如下错误:

* 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

参考链接


macOS Mojave (10.14.3) Android Studio 3.3.1 指定使用CMake 3.10.2版本

目前最新版本的 Android Studio 3.3.1默认使用CMake 3.6版本,但是已经支持 CMake 3.10.2 版本了。

新版本的 CMake 3.10.2 新增了 FindVulkan.cmake 等一系列的改进,对于很多项目来说,会更友好。

目前默认依旧使用 CMake 3.6 版本,但是可以手工指定使用 CMake 3.10.2

继续阅读macOS Mojave (10.14.3) Android Studio 3.3.1 指定使用CMake 3.10.2版本

错误 程序包org.apache.http.conn.util不存在

开发的时候遇到了这个问题,记录下下次就知道了。这个是因为在 sdk22 以后的版本不在支持了!!!

程序包 org.apache.http.conn.util 不存在,然后搜索了下,在 appbuild.gradle 文件中加上useLibrary 'org.apache.http.legacy' 就可以了。

如下:

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.3'

    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "com.ah.woyi"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
}

参考链接


错误 程序包org.apache.http.conn.util不存在