androidccmakeandroid-ndkandroid-native-library

Android NDK - execute .so returning 0 but not working


How to execute a .so file if process = new ProcessBuilder(new String[]{"su", "-c", "exec " + mylib.so).start() is returning 0 without any sign of execution happening.

I have compiled a .c file using cmake and storing it as a library (.so). However, when trying to execute the library (which has to run the main() function) nothing is happening while the process exit value is 0 (no error).

I tried to write to a file that I created to test if the library is being executed but nothing happened too. the file remained empty.

int main (int argc, char **argv)
{
LOGW("Running");
FILE* file = fopen("/data/local/tmp/log.txt","w+");

if (file != NULL)
{
    fputs("Running\n", file);
    fflush(file);
    fclose(file);
}}

Moreover, I couldn't find the cmake log file is it because the library has not executed? how can i access the log file then if i added the log library in my cmakelist.txt as follows:

find_library(
        log-lib
        log )

target_link_libraries(
        mylib
        ${log-lib} )

Solution

  • The execute command did not work instead a workaround was to develop a function that the JAVA class can call which in return call the main function. My Main function needed two arguments a char array (argv) and the length of the array (argc) to be passed.

    JNIEXPORT jint JNICALL
    yourapp.class.function_name
        (JNIEnv *env, jobject object, jobjectArray stringArray) {
    
    int argc = (*env)->GetArrayLength(env, stringArray);
    jsize size = (*env)->GetArrayLength(env, stringArray);
    
    char **argv = (char **) malloc(size);
    for (int i = 0; i < size; ++i) {
        jstring javaString = (jstring) (*env)->GetObjectArrayElement(env, stringArray, i);
        const char *nativeString = (*env)->GetStringUTFChars(env, javaString, 0);
    
        argv[i] = strdup(nativeString);
        (*env)->ReleaseStringUTFChars(env, javaString, nativeString);
        (*env)->DeleteLocalRef(env, javaString);
        }
    main(argc, argv);
    }
    

    As for the logs, they would be available in the debugger or Logcat.