hermancain
hermancain

Reputation: 1622

How can I build a command line app for 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).

Upvotes: 9

Views: 5770

Answers (1)

hermancain
hermancain

Reputation: 1622

2023 Edit: This no longer works, I'll update this if I figure it out (it's probably simpler now, right?)

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

Upvotes: 8

Related Questions