Building a native shared library as dependency in gradle

强颜欢笑 提交于 2019-12-07 19:09:44

问题


I have an Android Studio project which depends on a native shared library. I have created a cmake file to compile the library and I have added a soft link to the shared library inside the android project (in src/main/jniLibs/armeabi). That way when the android project is built, the shared library is included in the package.

Here is the relevant part of build.gradle:

android {
    ...
    externalNativeBuild {
        cmake {
            path "../cpp/CMakeLists.txt"
        }
    }
}

The problem is that gradle tries to open the shared library before invoking the instructions to build it.

Information:Gradle tasks [:app:assembleDebug]
Error:Could not list contents of 'app/src/main/jniLibs/armeabi/libfoo.so'. Couldn't follow symbolic link.

How can I invoke the cmake from inside the project and include the library in the project at the same time?

--

EDIT

In the cmake the shared library is built with ExternalProject_Add. Unfortunately gradle doesn't see that target, nor does it see imported shared libraries as targets. So this does not work:

add_library(libfoo SHARED IMPORTED GLOBAL)
add_dependencies(libfoo libactual)

I tried to invoke building the particular target with a gradle config:

defaultConfig {
    ...
    externalNativeBuild {
        cmake {
            targets "libfoo"
        }
    }
}

But gradle still doesn't see it and fails with:

Unexpected native build target libfoo. Valid values are:

The valid values are basically an empty list.

Currently I work around this by creating a fictional executable depending on the library.

add_executable(libfoo a.c)
add_dependencies(libfoo libactual)

来源:https://stackoverflow.com/questions/48265508/building-a-native-shared-library-as-dependency-in-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!