dongtu0363
2016-11-13 15:31
浏览 154

如何为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).

图片转代码服务由CSDN问答提供 功能建议

我想为android构建小型应用程序,在我远程连接到该应用程序时可以从android命令行运行 通过ssh的android设备。 我不需要任何类型的按钮/启动器/图标或gui界面。

如果可能的话,我更喜欢在golang中这样做。

我尝试使用 gomobile golang.org/x/mobile ),但是当我将apk放在设备上并尝试运行它时,出现错误:

  u0_a44 @ ghost:/data/data/berserker.android.apps.sshdroid/home $ am start gserv.apk 
启动:Intent {act = android.intent.action.MAIN cat =  [android.intent.category.LAUNCHER] pkg = gserv.apk} 
java.lang.SecurityException:权限被拒绝:startActivity要求以用户-2的身份运行,但正在从用户0调用; 这需要在android.os.Parcel.readException(Parcel.java:1546)
在android.os.Parcel.readException(Parcel.java:1499)
在android.app.ActivityManagerProxy有android.permission.INTERACT_ACROSS_USERS_FULL 
。  startActivityAsUser(ActivityManagerNative.java:2504)
在com.android.commands.am.Am.runStart(Am.java:770)
在com.android.commands.am.Am.onRun(Am.java:307)  
在com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
在com.android.commands.am.Am.main(Am.java:98)
在com.android.internal  .os.RuntimeInit.nativeFinishInit(本机方法)
位于com.android.internal.os.RuntimeInit.main(RuntimeInit.java:249)
   
 
 

该应用 只是一个小的Hello World HTTP服务器。 如何安装和运行它,使其能够像不需要gui( ls cat ,< code> cd 等)。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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

    点赞 评论

相关推荐 更多相似问题