如何在Android cmake外部构建系统中链接第三方库?

如何在Android cmake外部构建系统中链接第三方库?

问题描述:

Android Studio 2.2引入了cmake外部构建系统.问题是确实缺少文档,我不知道该如何链接第三方库?我已经尝试了cmake指令target_link_libraries:

Android Studio 2.2 introduces cmake external build system. The problem is that documentation is really lacking and I do not know how should I link third party libraries? I've tried cmake directive target_link_libraries:

target_link_libraries(native-lib libs/libSomething.so)

它在该应用程序中编译",但随后在运行时出现dlopen错误,因为libSomething.so尚未与应用程序打包在一起.如果可以更改任何内容,那么libs目录位于"app"下,并且我已开始使用Android Studio 2.2生成的默认JNI项目...

And it "works" in that app compiles but then I get dlopen error at runtime because libSomething.so has not been packaged with application. The libs directory is under "app" if that changes anything and I've started with default JNI project generated by Android Studio 2.2...

[更新]

我尝试将libSomething.so放在app/src/main/jniLibs/armeabi-v7a下,但是现在未打包"main"(本地lib)库.

I've tried putting libSomething.so under app/src/main/jniLibs/armeabi-v7a but now the "main" (native-lib) library is not packaged.

[Update2]

[Update2]

我添加了包含cmake输出dir的源集配置,它可以工作,但是像地狱一样丑陋,并不是真正的永久解决方案...

I've added source set config that includes cmake output dir and this works but is ugly as hell and is not really a permanent solution...

sourceSet
{
    main
    {
        jniLibs.srcDirs = [ "libs", ".externalNativeBuild/cmake/debug/obj"]
    }
}

现在,我最终在构建后的步骤中将libSomething.so复制到cmake库的输出目录.之所以有效,是因为事实证明Android Studio会将该目录中的所有内容复制到apk中.

For now I ended up copying libSomething.so to cmake library output directory in a post build step. This works because it turns out that Android Studio copies into apk EVERYTHING that is in that directory.

cmake中的命令如下:

add_custom_command(TARGET native-lib POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy
        ${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/libSomething.so
        ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libSomething.so
    )