Naturjoghurt
Naturjoghurt

Reputation: 541

Compile C Code on Android Phone

I want to compile and run .c Files that are sent to my AndroidApp. I want to compile on ARM .c Files for ARM. I am using a Galaxy Nexus with Android 4.2. I have installed the Android SDK and the Android NDK I have done this with the Terminal IDE, but I don't want to compile it manually from the Terminal. Now I have downloaded the GCC for Android (android-gcc-4.4.0) and don't know how to add this properly to my Application and how to compile with it from inside the Java-Code.

I have read plenty things on the Internet about this, but none of them really helped me figuring out, how to do this

So my Problem is: How can I use the Android/NDK Toolchain from my Android App, or any other toolchain to compile and then run .c files.

EDIT: I now got the android-gcc unzipped right on my Phone in the files directory (data/data/.../files). I want to call the gcc compiler on the phone to compile a .c file. I tried to use:

String[] cmds={MainActivity.this.getFilesDir() + "/android-gcc-4.4.0/bin/arm-eabi-gcc-4.4.0", "-c", file_path};
String result = "";
Process p = Runtime.getRuntime().exec(cmds);
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = in.readLine()) != null) 
{
    result += line + "\n";
}
alert_dialog("Output", result); // Creates and displays an alert dialog
in.close();
}
catch (Exception e) 
{
    alert_dialog("Error", e.toString() + "\n" + e.getMessage());
}

This was my approach invoking gcc on Android, but i only get following Exception: java.io.IOException: Error running exec(). Command [...] Working directory: null Environment null.

Does anyone have a clue how to invoke the android-gcc compiler correct on the Phone?

Any help appreciated and thanks in advance

Upvotes: 6

Views: 7070

Answers (1)

Seva Alekseyev
Seva Alekseyev

Reputation: 61388

This not an officially supported scenario; no OS worth its salt would let non-privileged apps install executable code on the device willy nilly.

On a rooted phone, you might have some luck; but still, you'd need to put together a toolchain from scratch. NDK was never meant for this. There's no flavor of NDK that runs on ARM.

Upvotes: -2

Related Questions