Issue
If I want to add a custom executable to an Android app using CMake to compile it, I can use the following:
add_executable(hello_world.so hello_world.cpp)
This will bundle the compiled executable in the /lib/{ANDROID_ABI}
folder of the APK.
Despite this being an executable, it NEEDS to be named .so
, otherwise it won't be packed. There's probably a way to work around this, but this is not my issue at the moment, as the file is properly packed and executes properly with this setup.
However, I'm failing to bundle the executable in the case that I do not want CMake to compile it for me because I already have it. I tried the following:
add_executable(hello_world.so IMPORTED)
set_target_properties(
hello_world.so
PROPERTIES IMPORTED_LOCATION
${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/hello_world.so)
However, in this case, it won't be packed in the APK. The variables and paths are correct. In fact I can use a similar set_target_properties
(with same variables and path) to successfully include in the final APK a library added with add_library(lib.so SHARED IMPORTED)
.
What am I doing wrong?
Solution
I did not manage at the end to use add_executable
to directly include a pre-compiled executable.
I decided to use the following workaround: use add_library
to import the executable pretending it's a shared library, compile a dummy executable with add_executable
and then link the shared library to the dummy executable.
The step of linking the two is needed because otherwise the shared library will not be packed into the apk.
The following is an example CMakeLists.txt
:
project("myapp")
add_library(
myexe
SHARED
IMPORTED)
set_target_properties(
myexe
PROPERTIES IMPORTED_LOCATION
${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/libmyexe.so)
add_executable(
do_nothing.so
do_nothing.cpp)
target_link_libraries(
do_nothing.so
myexe)
The do_nothing.cpp
file is the following:
#include "stdio.h"
int main(int argc, char *argv[]) {
printf("This executable does nothing!\n");
}
With this, the executable will be unpacked in your app folder inside /data/app
. Names in this folder are randomized, search the right folder with:
ls -d /data/app/*/*/* | grep your.package.com | grep lib
From an Android application it's possible to retrieve this same path with:
getApplicationInfo().nativeLibraryDir
Answered By - Zagorax Answer Checked By - Cary Denson (WPSolving Admin)