I am creating an Android native application, where I have many cpp files and few kt files. With the use of cpp files, I am compiling static libraries which gives me .a extension files and as a whole I am combining these into an .so file.
As, its a native application System.loadLibrary("LibName") will be called during startup which calls JNI_onLoad (JavaVM * pJavaVM, void * pReserved).
Now problem occurs here, In one of the Cpp files, I am only having two functions JNI_onLoad and JNI_OnUnload. Now, when it is in this way while dumping .so file I don't see the symbols of these two functions but while dumping .a file the symbols are present.
But if I create a static variable and use it in JNI_onLoad then in my .so file I am able to see the symbols.
I have also gone through one documentation of linker flags where "-E" flag refers to the linker to add all the symbols into the table. Tried adding this too but didn't work.
Am I missing something here? Not able to figure this out why this happens. Thanks in advance.
extern "C" JNIEXPORT jint JNICALL
JNI_OnLoad (JavaVM * pJavaVM, [[maybe_unused]] void * pReserved)
{
JNIEnv * env;
int ret_val;
if (!pJavaVM)
return JNI_ERR;
if (pJavaVM->GetEnv ((void **) &env, JNI_VERSION_1_6) != JNI_OK)
return JNI_ERR;
// We are doing our own operations
return env->GetVersion ();
}
Build steps:
We compile our cpp files with Clang compiler and using CMake we generate our .a library files and using linker we create our resultant .so file.
# Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1)
# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
# Since this is the top level CMakeLists.txt, the project name is also accessible
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
# build script scope).
project("MyProject")
#Please add path of cpp files in below add_library command
add_library(Lib1 STATIC JNI.cpp)
add_library(Lib2 STATIC AnotherFile.cpp)
add_library(${CMAKE_PROJECT_NAME} SHARED native-lib.cpp)
target_link_libraries (${CMAKE_PROJECT_NAME} PUBLIC Lib1 Lib2 log android)