dongtu0363 2016-11-13 15:31
浏览 180
已采纳

如何为Android构建命令行应用程序?

I want to build small app for android that I can run from the android command line while I'm remotely connected to the android device through ssh. I don't want a button/launcher/icon or gui interface of any kind.

I prefer to do this in golang if possible.

I tried using gomobile (golang.org/x/mobile) but when I put the apk on my device and try to run it I get the error:

u0_a44@ghost:/data/data/berserker.android.apps.sshdroid/home $ am start gserv.apk                                         
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] pkg=gserv.apk }
java.lang.SecurityException: Permission Denial: startActivity asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
    at android.os.Parcel.readException(Parcel.java:1546)
    at android.os.Parcel.readException(Parcel.java:1499)
    at android.app.ActivityManagerProxy.startActivityAsUser(ActivityManagerNative.java:2504)
    at com.android.commands.am.Am.runStart(Am.java:770)
    at com.android.commands.am.Am.onRun(Am.java:307)
    at com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
    at com.android.commands.am.Am.main(Am.java:98)
    at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
    at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:249)

The app is just a small hello world http server. How can I install and run it in a way that allows me to use it like other command line programs built into android that don't require gui (ls, cat, cd, etc).

  • 写回答

1条回答 默认 最新

  • dongtan5555 2016-11-13 17:30
    关注

    Appears to vary by system and version, but I needed to get the android ndk from here:

    https://developer.android.com/ndk/downloads/index.html

    and install the toolchain

    ./android-ndk-r10c/build/tools/make-standalone-toolchain.sh --platform=android-21 --install-dir=$NDK_ROOT

    and then build the binary with

    CC="$NDK_ROOT/bin/arm-linux-androideabi-gcc" GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=0 go build -v -o goserv main.go

    I got most of this info from here: https://github.com/golang/go/issues/9412

    But needed to change change some paths, change CC_FOR_TARGET to just CC, GOOS=android to GOOS=linux, and needed to set CGO_ENABLED=0.

    This was on Linux Fedora 25 Beta with Go 1.7.4

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?