How do I run my packaged binary in my Android app?

我们两清 提交于 2021-02-18 03:26:09

问题


I have an Android app that needs to run a custom binary app I wrote. I already built the binary using ndk and packaged it in the apk under res/raw

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

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

What do I do after this to run the binary from resources? There was another question here that suggested the use of AssetManager but I don't understand how to do it exactly. I checked out some opensource Apps (android-wifi-tether, etc.) that have binaries packaged with them and I don't see AssetManager anywhere in their source and don't understand how exactly they're doing it.


回答1:


There's a nasty hacky way to trick Android into doing this for you (which I do, successfully).

When the application is installed, Android will copy the appropriate libraries for your architecture out of libs/$ARCH/libfoo.so into /data/data/$PACKAGE/libs for you. The tricky bit is that it will only copy files of the form lib$SOMETHING.so. It'll even preserve the execute permissions.

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

However, beware! Android is not designed to run standalone apps and Zygote really doesn't get on well with being forked. I've found that Runtime.exec() has race conditions and can crash. The only reliable technique I've found is to use JNI to implement a little library that does a fork and exec without touching any other data between the fork and the exec. I did have a question here about this with all the details, but I can't find it.

I don't have access to any of the code right now, but if you want I can find some of it for you on Monday.




回答2:


Adding to David Given's response, which is great, this is what I use at the end of Android.mk to rename the binary automatically. May be of some use to others trying the same thing:

...
LOCAL_MODULE := yourbinarynamehere

include $(BUILD_EXECUTABLE)

all: $(LOCAL_MODULE)
    $(shell (mv libs/$(TARGET_ARCH_ABI)/$< libs/$(TARGET_ARCH_ABI)/lib$<.so))

Mind the TAB before $(shell... spaces won't work as per standard GNU Make.



来源:https://stackoverflow.com/questions/6839767/how-do-i-run-my-packaged-binary-in-my-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!