在Windows上使用CMake在Android Studio中构建+链接libpng [英] Building+linking libpng in Android Studio with CMake on Windows

查看:201
本文介绍了在Windows上使用CMake在Android Studio中构建+链接libpng的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Windows 10上使用Android Studio和CMake启动本机应用程序项目,但是我坚持要包含libpng.

I'm trying to start a native app project with Android Studio and CMake on Windows 10, but I'm stuck at including libpng.

对于初学者来说,这是我第一次看到CMakeLists.txt文件.我花了一天的时间才弄清target_link_libraries(native-activity ... png)不可能是target_link_libraries(png native-activity ...),因为所有错误消息都是关于文件未创建以及由于缺少工具链要求而导致命令失败的原因(为什么在列表末尾出现基本错误) ?不酷!).

For starters it's the first time I've seen a CMakeLists.txt file. It took me a day to figure out target_link_libraries(native-activity ... png) could not be target_link_libraries(png native-activity ...) since all the error messages were about files not being created and commands failing due to missing requirements from the toolchain (why were the essential errors at the end of the list? not cool!).

最终设法在项目中包含libpng之后,我现在得到一个构建错误:

After finally managing to include libpng in the project I now get a build error:

Error:Execution failed for task ':app:externalNativeBuildDebug'.
...
error: unknown target CPU 'armv5te'
CMake Error at scripts/genout.cmake:78 (message):
    Failed to generate
    C:/APP_PATH/app/libpng-1.6.28/build/scripts/symbols.out.tf1
ninja: build stopped: subcommand failed.

我已经递归地复制了我的项目.android,.AndroidStudio2.2目录以及文件名,除了genout.cmake之外,armv5te完全找不到任何东西.我的abiFilters行是abiFilters 'x86'.

I've recursively grep'ed my project, .android, .AndroidStudio2.2 directories, as well as filenames, and found absolutely nothing with armv5te except for the genout.cmake. My abiFilters line is abiFilters 'x86'.

如何构建libpng以链接到本机应用程序?另外,在Android Studio中,它显示该项目现在包括libpng源文件(专用于它的项目不少于9个!).有什么办法可以删除它?

How do I build libpng to link to my native app? Also, in Android Studio it shows the project now includes the libpng source files (with no less than 9 projects dedicated to it!). Is there any way to remove it?

这是我的CMakeLists.txt:

Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.4.1)

# build native_app_glue as a static lib
add_library(app-glue STATIC ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)

set(png_src_dir ../../../../libpng-1.6.28)
set(PNG_STATIC ON)
add_subdirectory(${png_src_dir} ${png_src_dir}/build)

# now build app's shared lib
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++14")

add_library(native-activity SHARED
            main.cpp logger.cpp logger.h game.cpp game.h
            shaders.cpp shaders.h assets.cpp assets.h)

target_include_directories(native-activity PRIVATE ${ANDROID_NDK}/sources/android/native_app_glue
                           C:/devlibs/include
                           ${png_src_dir})

# add lib dependencies
target_link_libraries(native-activity app-glue android log EGL GLESv2 png)

推荐答案

我已管理libpng使其与android NDK应用程序(CMake构建系统而不是Android.mk)一起用作静态库.我使用了 libpng-android重新包装.这是我已经完成的事情:

I've managed libpng to work with android NDK app (CMake build system instead of Android.mk) as static library. I used libpng-android repackaging. Here are things I've done:

  1. git clone https://github.com/julienr/libpng-android.git转换为${YOUR_LIBS_FOLDER}(我用过${ANDROID_NDK_ROOT_DIRECTORY}/sources/android).
  2. 在构建脚本所需的全局$PATH变量中添加${ANDROID_NDK_ROOT_DIRECTORY}(对我来说是home/username/Android/sdk/ndk-bundle).
  3. 使用ndk-build构建lib(具有lib的目录中存在./build.sh).库将针对不同的 ABI 目标(arm64-v8aarmeabix86_64等)构建).
  4. 这时,您在${YOUR_LIBS_FOLDER}/libpng-android/jnilibpng.a${YOUR_LIBS_FOLDER}/libpng-android/obj/local/${ANDROID_ABI}/有库头,其中${ANDROID_ABI}是目标平台.
  5. 最后,您可以在CMakeLists.txt处包含lib. libpng需要 zlib压缩库,因此您也需要对其进行链接(zlib由android studio提供,因此只需添加-lz标志).
  1. git clone https://github.com/julienr/libpng-android.git into ${YOUR_LIBS_FOLDER} (I used ${ANDROID_NDK_ROOT_DIRECTORY}/sources/android).
  2. Add ${ANDROID_NDK_ROOT_DIRECTORY} (home/username/Android/sdk/ndk-bundle for me) to global $PATH variable which is needed for build script).
  3. Build lib with ndk-build (there is ./build.sh for that in directory with lib). Library will be built for different ABI targets ( arm64-v8a, armeabi, x86_64 etc).
  4. At this point you have library headers at ${YOUR_LIBS_FOLDER}/libpng-android/jni and libpng.a at ${YOUR_LIBS_FOLDER}/libpng-android/obj/local/${ANDROID_ABI}/, where ${ANDROID_ABI} is target platform.
  5. Finally you could include lib at CMakeLists.txt. libpng requires zlib compression library so you need to link against it aswell (zlib is provided by android studio so just add -lz flag).

这是我CMakeLists.txt的相关文章:

add_library(libpng STATIC IMPORTED)
set_target_properties(libpng PROPERTIES IMPORTED_LOCATION
    ${YOUR_LIBS_FOLDER}/libpng-android/obj/local/${ANDROID_ABI}/libpng.a)

add_library(appManager SHARED src/main/cpp/appManager.cpp)

target_include_directories(appManager PRIVATE ${YOUR_LIBS_FOLDER}/libpng-android/jni/)

target_link_libraries(appManager
                       android
                       libpng
                       z)

这里没有什么要注意的:

Few things to note there:

  • ${ANDROID_ABI}是Android Studio构建系统设置的变量.
  • 再次:您需要链接到 zlib ,这就是我们拥有libpng z的原因而不是target_link_libraries中的libpng.
  • ${ANDROID_ABI} is a variable set by Android Studio build system.
  • Once again: you need to link against zlib, that is why we have libpng z instead of libpng in target_link_libraries.

这篇关于在Windows上使用CMake在Android Studio中构建+链接libpng的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆