App Development: Calling another binary executable

I want to call another binary executable file when I start my app. This bin file will generate a .txt file with content "hello, world!".
Below is my code.

void checkFilePermissions(const char* filePath) {
        if (access(filePath, X_OK) != -1) {
            ALOGI( "File has execute permission: %s" , filePath);
        } else {
            ALOGI( "File does not have execute permission: %s" , filePath);
        }
}
int executeBinary(const char *filePath) {
        char *args[] = {strdup(filePath), nullptr};
        if (execvp(args[0], args) == -1) {
        ALOGI( "execvp failed: %s" , strerror(errno));
        return -1;
         }
        return 0;
}

void OnStart() override {
        const char *filePath = "/data/data/com.magicleap.capi.sample.eye_tracking/files/bin/hello";
        checkFilePermissions(filePath);
        int result = executeBinary(filePath);
        if (result == -1) {
            ALOGI("Failed to execute binary.");
        } else {
            ALOGI("Binary executed successfully with exit code: %d", result);
        }
... // other code
}

Below is the Log:

*File has execute permission: /data/data/com.magicleap.capi.sample.eye_tracking/files/bin/hello*
*execvp failed: Permission denied*
*Failed to execute binary.*

How can I realize my idea?

Hi @Xinyu,

Even when your file has execute permissions, Android's security model prevents apps from directly executing binaries in this way for security purposes.

If you want to execute native code on Android, we recommend using the Android NDK (Native Development Kit). This allows you to write C/C++ code that can be compiled into native libraries (.so files) and then called from your Android app.

Note: We specialize in Magic Leap 2 development, and this question seems to be more about general Android development capabilities. For more help with general Android development topics, we recommend posting your questions on platforms like Stack Overflow (https://stackoverflow.com/), where Android developers can help.

Let me know if you have any further questions.

Best,

El

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.