2

I have an Android NDK project which builds libMyProject1.so and I am using:

set_target_properties(MyProject1
        PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../Client/libs/${ANDROID_ABI}")

to export the built library to the folder that I need.

I also have another external shared library that I link with:

MyExternal library

add_library(MyExternal SHARED IMPORTED)
set_target_properties(MyExternal PROPERTIES IMPORTED_LOCATION        ${CMAKE_CURRENT_SOURCE_DIR}/../MyExternal/libs/${ANDROID_ABI}/libMyExternal.so)

target_link_libraries( # Specifies the target library.
        MyProject1
        # Shared Dependencies
        MyExternal
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

libMyProject1.so is copied to Client/libs/${ANDROID_ABI} but libMyExternal.so is not copied. How to copy the external shared library to my client folder using cmake?

shizhen
  • 12,251
  • 9
  • 52
  • 88
ssk
  • 9,045
  • 26
  • 96
  • 169
  • 2
    Property `LIBRARY_OUTPUT_DIRECTORY` affects only on the library **produced** by your project. It doesn't affect on *IMPORTED* libraries which have already exist. You need to copy such library manually. Choose any way described in that question: https://stackoverflow.com/questions/34799916/copy-file-from-source-directory-to-binary-directory-using-cmake. – Tsyvarev Jan 24 '19 at 19:44
  • thanks @Tsyvarev, file(COPY ... ) worked. – ssk Jan 24 '19 at 20:05

2 Answers2

1

As suggested in the comments, the following worked for me:

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../MyExternal/libs/${ANDROID_ABI}/libMyExternal.so
        DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/../Client/libs/${ANDROID_ABI})
ssk
  • 9,045
  • 26
  • 96
  • 169
0

You should change the jniLibs.srcDirs, which will be packed by the gradle.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        ...
        ndk {
            abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
        }
        ...
    }
    ...
    sourceSets {
        main {
            // let gradle pack the shared library into apk
            jniLibs.srcDirs = ['Client/libs/']
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

dependencies {
   ...
}
Jerikc XIONG
  • 3,517
  • 5
  • 42
  • 71