How can I build a command line app for android?

huangapple go评论96阅读模式
英文:

How can I build a command line app for android?

问题

我想为Android构建一个小应用程序,我可以在通过SSH远程连接到Android设备时从Android命令行运行。我不想要任何按钮/启动器/图标或GUI界面。

如果可能的话,我更喜欢使用Golang进行开发。

我尝试使用gomobilegolang.org/x/mobile),但是当我将apk放到设备上并尝试运行时,出现以下错误:

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)

该应用程序只是一个简单的Hello World HTTP服务器。我该如何安装和运行它,以便我可以像其他内置于Android的命令行程序(如lscatcd等)一样使用它?

英文:

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

得分: 8

似乎因系统和版本而异,但我需要从这里获取Android NDK:

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

并安装工具链

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

然后使用以下命令构建二进制文件

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

我从这里获取了大部分信息:https://github.com/golang/go/issues/9412

但需要更改一些路径,将CC_FOR_TARGET更改为CC,将GOOS=android更改为GOOS=linux,并设置CGO_ENABLED=0

这是在Linux Fedora 25 Beta上使用Go 1.7.4完成的。

英文:

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

huangapple
  • 本文由 发表于 2016年11月13日 23:31:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/40575532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定