First of all, I believe, it's not a duplicate question. I don't want to write a native app with NDK, I just want to use Android device as a cheap Linux server. Server, in this context, means a program, which has no UI. Also, the server is written in bare C++, but uses no libraries at all, so that's why should that question differ from others.
The story
We've written a small server for Linux, it has a webserver interface, so you connect to it with a vanilla browser, and you can play with it. We've compiled it to Linux/x86, Linux/ARM (GuruPlug), Linux/PPC (some kind of NAS), and Mac OS X Darwin platforms.
The Linux/ARM version also runs well on my Palm Pré smartphone. I've just copied the executable onto my phone (in webOS scene, there's no such thing like jailbreak, there's an official "developer mode", which if you activate by typing a secret key combination, you can log in to your phone with ssh or use local ssh app). So, our small server program (daemon, may say) is running on even the smallest Linux devices. (It's funny, when connect with your giga-powered desktop machine's browser to a smartphone server.)
The server program is designed to run on even the smallest Linux (as I said, it runs on NAS, router, smartphone), it requires only STDLIB and PTHREADS.
The question
What't the straight way to get that simple server program run on an Android device?
So, I want just do the same thing with an Android device (phone, tablet), just as I did on my webOS device: copy the server program and run on it. Computers are for that, running programs, aren't they? ;)
I have not really tried it, but it should be possible to use the ARM GCC compiler you have, or the one provided by the Android NDK to compile native application, the same as you would use it on your desktop.
If your application is small (not too many files), you can manage the compilation by hand (or in a simple Makefile). The following commands are from the LuaJIT compilation guide for Android:
/tmp$ cat test.c
#include <stdio.h>
int main() {
printf("Hello world!\n");
return 0;
}
/tmp$ NDK=/opt/ndk
/tmp$ NDKABI=8
/tmp$ NDKVER=$NDK/toolchains/arm-linux-androideabi-4.4.3
/tmp$ NDKP=$NDKVER/prebuilt/darwin-x86/bin/arm-linux-androideabi-
/tmp$ NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-arm"
/tmp$ ${NDKP}gcc $NDKF -o test test.c
/tmp$ file test
test: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
Obviously, use linux-x86
instead of darwin-x86
if you are on Linux (not Mac OS X, also not sure about Windows), and change the path to NDK
to where you installed it.
Now you have to move the compiled binary somewhere to your Android device, and run adb shell
to see if it works:
/tmp$ ADB=/opt/sdk/platform-tools/adb
/tmp$ $ADB push test /data/
332 KB/s (3343 bytes in 0.009s)
/tmp$ $ADB shell
# /data/test
Hello world!
You definitely need a rooted android device.
I recommend that you install busybox
to get a bunch of nice command line tools such as wget, tar, gzip
and many more.
You can get shell access to your device by using adb shell
and then su
to get root access. You can either use adb push
or wget
to put your server on the device.
The /system partition is mounted as read only and depending on the manufacturer there are a lot of protections that restrict you from writing there so you should copy your server somewhere on the /data partition (e.g. mkdir /data/myserver
). After this, everything should be pretty straight forward.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With