40 likes | 195 Views
C/C++ programming on Android. Console C/C++ programming on Android without NDK. Get compile for the ARM tool chain (get the one targeted at "GNU/Linux"): http://www.codesourcery.com/gnu_toolchains/arm/download.html This only runs on linux Make new dir Create empty doc Save doc as hello.cpp
E N D
Console C/C++ programming on Android without NDK • Get compile for the ARM tool chain (get the one targeted at "GNU/Linux"): http://www.codesourcery.com/gnu_toolchains/arm/download.html • This only runs on linux • Make new dir • Create empty doc • Save doc as hello.cpp • Open doc and enter • #include “stdio.h” • int main(intargc, char **argv) { • printf("Hello, world!\n"); • return 0; • } • Save • arm-none-linux-gnueabi-gcc -static -o hello hello.c • (move executable on window drive) • adb push hello /data/misc/hello • This will most likely fail. If so, then • adb shell mount -o remount,rw -t yaffs2 /dev/block/mtdblock0 /data • Then adb shell chmod 777 /data/misc/hello • adb shell chmod 777 /data/misc/hello • Then run it adb shell /data/misc/hello
Starting a console program from java • The only way for an android app to execute something that needs root access is to make a separate c/c++ program and run that program from your app. • You could write the program in another language, but you would need to arm compiler for that language • Of course, this requires that the phone is rooted • The path to the su program. • Sometimes just “su” works, other times you need /system/bin/su • The su program is installed when you root the phone • To test you have root, • C:\android\android-sdk\platform-tools\adb shell • $: su • ls /data/data • Once you have root, this listing will work and not give an error • Path to program • String programPath = “/mnt/sdcard/hello”; • //ProcessBuilderpb = new ProcessBuilder(new String[]{"/system/bin/sh", "-c", programPath}); • ProcessBuilderpb = new ProcessBuilder(new String[]{“su", "-c", programPath}); • pb.redirectErrorStream(true); • Process process; • process = pb.start(); • BufferedReader reader =new BufferedReader(new InputStreamReader(process.getInputStream())); • String rline; • while ((rline = reader.readLine()) != null) {} • process.waitFor();