How to run my batch file in an Android app?

I have an Android app that needs to run its own binary application that I wrote. I already built the binary using ndk and packed it in apk under res / raw

I did something like this to start the su process first.

Process process; process = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(process.getOutputStream()); 

What should I do after this to run the binary from resources? There was another question that suggested using AssetManager, but I donโ€™t understand how to do it. I checked some open source applications (android-wifi-tether, etc.) that have binaries associated with them, and I donโ€™t see AssetManager anywhere in their source and donโ€™t understand how exactly they do it.

+4
source share
2 answers

There's an unpleasant hacker way to trick Android into doing this for you (which I am doing successfully).

When the application is installed, Android will copy the appropriate libraries for your architecture from libs/$ARCH/libfoo.so to /data/data/$PACKAGE/libs for you. The hard bit is that it only copies files of the form lib$SOMETHING.so . It will even save execution permissions.

So, if you rename your su executable to libsu.so , Android will do all the work for you. Then you can find it using the paths available from Activity.

However, be careful! Android is not designed to run stand-alone applications, and Zygote really can't handle branching. I found that Runtime.exec() has race conditions and may crash. The only reliable method I've found is to use JNI to implement a small library that uses fork and exec without touching other data between the fork and exec. I had a question about this with all the details, but I can not find it.

I donโ€™t have access to the code right now, but if you want, I can find part of it on Monday.

+3
source

Adding to David This answer, which is great, is what I use at the end of Android.mk to automatically rename the binary. May be useful for others trying to do the same thing:

 ... LOCAL_MODULE := yourbinarynamehere include $(BUILD_EXECUTABLE) all: $(LOCAL_MODULE) $(shell (mv libs/$(TARGET_ARCH_ABI)/$< libs/$(TARGET_ARCH_ABI)/lib$<.so)) 

Note TAB before $ (shells ... spaces will not work according to standard GNU Make.

+2
source

All Articles