Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I build a native (command line) executable to run on Android?

Tags:

linux

android

I've had success with building an Android app (GUI) that uses a native (JNI) library.

However, now I would like to create an executable that runs from the command line (root privileges) and does not use a GUI at all. How do I build something like that?

like image 487
Someone Somewhere Avatar asked Feb 27 '12 05:02

Someone Somewhere


People also ask

How to open cmd in Android Studio?

Open a command line—from Android Studio, select View > Tool Windows > Terminal—and navigate to the directory where your unsigned APK is located.

How do I run an emulator from command prompt?

Use the emulator command to start the emulator, as an alternative to running your project or starting it through the AVD Manager. Here's the basic command-line syntax for starting a virtual device from a terminal prompt: emulator -avd avd_name [ {- option [ value ]} … ]

What is NDK build?

The Native Development Kit (NDK) is a set of tools that allows you to use C and C++ code with Android, and provides platform libraries you can use to manage native activities and access physical device components, such as sensors and touch input.

How do I close an emulator in terminal?

adb -s emulator-5544 emu kill , where emulator-5544 - emulator name. adb -s emulator-5554 emu kill does shut down the emulator but the command does not terminate and blocks forever :/ to anyone who was confused by this, you can find the emulator name by using adb devices .


1 Answers

As of NDK r8d, this can be solved in a much simpler way.

  1. Create a project with the following directory hierarchy:

    project/     jni/         Android.mk         Application.mk         *.c, *.cpp, *.h, etc. 
  2. Fill in Android.mk with the following content. The most important thing is the last line. Check the NDK doc for the meaning of the other variables.

    LOCAL_PATH := $(call my-dir)  include $(CLEAR_VARS)  LOCAL_MODULE := name-of-your-executable LOCAL_SRC_FILES := a.cpp b.cpp c.cpp etc.cpp LOCAL_CPPFLAGS := -std=gnu++0x -Wall -fPIE         # whatever g++ flags you like LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog -fPIE -pie   # whatever ld flags you like  include $(BUILD_EXECUTABLE)    # <-- Use this to build an executable. 
  3. Go to the project/ directory, and simply type

    ndk-build 

    The result will be placed in project/libs/<arch>/name-of-your-executable.

like image 108
kennytm Avatar answered Sep 20 '22 21:09

kennytm