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

前端 未结 2 1079
自闭症患者
自闭症患者 2021-02-10 16:34

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 t

相关标签:
2条回答
  • 2021-02-10 17:15

    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.

    0 讨论(0)
  • 2021-02-10 17:16

    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.

    0 讨论(0)
提交回复
热议问题