safin hoque
safin hoque

Reputation: 1

How to execute NDK/Clang in Android 11 using Java to compile C++ code within app environment?

I'm working on an Android app that needs to compile C++ code using the NDK and Clang(linux). In Android 11, I'm unable to execute Clang directly from internal storage due to storage and execution restrictions. Even though I have the NDK and Clang binaries placed in internal storage, when I try to execute them, I get permission errors.

I am currently trying to use ProcessBuilder to execute Clang like this:

ProcessBuilder processBuilder = new ProcessBuilder("/data/data/<my-app>/files/clang", "<source-file>.cpp", "-o", "<output>");
processBuilder.directory(new File("/data/data/<my-app>/files/"));
processBuilder.start();

However, I keep running into permission issues.

The error everytime shows like:

/system/bin/sh: ./clang++: can't execute: Permission denied

I would like to know:

  1. How can I create a custom environment using Java within my app to execute Clang and compile C++ files?

  2. Is there a way to bypass the restrictions of executing binaries in internal storage on Android 11?

  3. Would using a chroot or similar environment inside my app help resolve this, and how can I set it up without requiring root access?

  4. Are there any Android APIs or workarounds for invoking Clang or other native binaries securely in the app's private directory (/data/data//files/)?

Any suggestions or alternative approaches to achieve this in Android 11+?

Upvotes: 0

Views: 54

Answers (0)

Related Questions