Want to compile native Android binary I can run in terminal on the phone Want to compile native Android binary I can run in terminal on the phone android android

Want to compile native Android binary I can run in terminal on the phone


Just use the android-ndk. And build a Android.mk like so.include $(BUILD_EXECUTABLE) is what tells it build a executable instead of a JNI .lib

Android.mk

ifneq ($(TARGET_SIMULATOR),true)LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_CFLAGS += -WallLOCAL_LDLIBS := -L$(LOCAL_PATH)/lib -llog -gLOCAL_C_INCLUDES := bionicLOCAL_C_INCLUDES += $(LOCAL_PATH)/includeLOCAL_SRC_FILES:= main.cppLOCAL_MODULE := mycmdinclude $(BUILD_EXECUTABLE)endif  # TARGET_SIMULATOR != true


First, make sure you have the NDK:

http://developer.android.com/tools/sdk/ndk/index.html

Here is the easiest way to compile a C binary for your phone:

http://developer.android.com/tools/sdk/ndk/index.html

http://www.kandroid.org/ndk/docs/STANDALONE-TOOLCHAIN.html

Usually $NDK(may be different) =

Linux:

/home/<user>/android-ndk

Mac OS X:

/Users/<user>/android-ndk

In Terminal:

# create tool-chain - one line# New method in ndk 12.$NDK/build/tools/make_standalone_toolchain.py --arch arm --install-dir=/tmp/my-android-toolchain# Old method.#$NDK/build/tools/make-standalone-toolchain.sh --platform=android-3 --install-dir=/tmp/my-android-toolchain# add to terminal PATH variableexport PATH=/tmp/my-android-toolchain/bin:$PATH# make alias CC be the new gcc binaryexport CC=arm-linux-androideabi-gcc# compile your C code(I tried hello world)$CC -o foo.o -c foo.c# push binary to phoneadb push foo.o /data/local/tmp# execute binaryadb /data/local/tmp/foo.o


Using CMake with the Android NDK is a nice way to compile Android console applications.

Download CMake and android-cmake (set it up like this). If your program is called main.c, then write the following in file CMakeLists.txt:

project(test)cmake_minimum_required(VERSION 2.8)add_executable(test ./main.c)

and run cmake -DCMAKE_TOOLCHAIN_FILE=$ANDTOOLCHAIN .

You will then have a Makefile for your program, you can run make to have your test executable.