Build golang application to run on Android device
Recently I am researching the adb command line tool to communicate with the Android Emulator and Physical Device, and I find that we can…
Recently I am researching the adb
command line tool to communicate with the Android Emulator and Physical Device, and I find that we can upload a sqlite3
command to /data/local/tmp
folder, and also we can run the command in the folder, so I am thinking whether we can build a golang application and running to the Android Device or not, and actually it works, I am building a golang application that can run on both Android Emulator and Physical Device.
I write a sample project on github, which can run on android and browse the crash log on the device from web browser, the link following is the sample project.
Build golang application
In my computer, the build script for Emulator and Physical Device are like below:# Build for Emulator
GOARCH=amd64 GOOS=linux go build -o logproxyservice main.go# Build for Physical Device (Redmi Note 9)
GOARCH=arm64 GOOS=linux go build -o logproxyservice main.go
Upload the application to Android Deviceadb -s <$ANDROID_SERIAL> push logproxyservice /data/local/tmp/
Run the application on Android Device# Login to the android with adb shell
adb -s <$ANDROID_SERIAL> shellcd /data/local/tmp# Just run the application and can terminate with CTRL+C
./logproxyservice# Default port is 8080, we can change the port with -port parameter, for example, run the application on port 9090, then the command can be like this
./logproxyservice -port=9090# Run on background as a service
nohup ./logproxyservice > log_logproxyservice.log 2>&1 &
Browse Crash log from browser
On the device, we can just use http://localhost:8080/crashlog
, if visit with web browser on the computer, then we need to get the ip address of the device, then use the device’s ip address to browse the crash log.
Thanks for your reading! Hope this article can give you some help.