Android NDK tips - yszheda/wiki GitHub Wiki

Compile Naive Code

$NDK_ROOT/build/tools/make_standalone_toolchain.py --arch=arm --force --stl=libc++ --force -v --install-dir=/opt
/android-toolchain
/opt/android-ndk-r14b/build/tools/make_standalone_toolchain.py --arch=arm --force --stl=gnustl --force -v --install-dir=/opt/android-toolchain

STL

Android.mk / Makefile

LOCAL_LDLIBS / LOCAL_SHARED_LIBRARIES / LOCAL_LDFLAGS

build lib or execution file

include $(BUILD_EXECUTABLE)
include $(BUILD_STATIC_LIBRARY)
include $(BUILD_SHARED_LIBRARY)

print in native code

Build

ndk-build

NEON

c++11

ndk-stack

adb logcat | ./ndk-stack -sym ~/<path>/obj/local/armeabi

Trouble Shooting

cannot find -lrt

NDK can't find the application directory

ndk-build NDK_PROJECT_PATH=<proj>

You need to specify 3 things. NDK_PROJECT_PATH - the location of your project NDK_APPLICATION_MK - the path of the Application.mk file APP_BUILD_SCRIPT - the path to the Android.mk file

These are needed to override the default values of the build script, which expects things to be in the jni folder.

When calling ndk-build use ndk-build NDK_PROJECT_PATH=/path/to/proj NDK_APPLICATION_MK=/path/to/Application.mk

In Application.mk add APP_BUILD_SCRIPT := /path/to/Android.mk

uses VFP register arguments, output does not

When using armeabi-v7a-hard:

LOCAL_CFLAGS += -mhard-float -D_NDK_MATH_NO_SOFTFP=1
LOCAL_LDFLAGS += -Wl,--no-warn-mismatch -lm_hard

link third-party static library

Use pre-compiled static library in Android.mk:

LOCAL_PATH := $(call my-dir)/..

include $(CLEAR_VARS)

LOCAL_MODULE := openblas
# ld: fatal error: jni/../jni/../: pread failed
# LOCAL_SRC_FILES += $(LOCAL_PATH)/lib/libopenblas.a
LOCAL_SRC_FILES := $(LOCAL_PATH)/lib/libopenblas.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include

include $(PREBUILT_STATIC_LIBRARY)
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
LOCAL_STATIC_LIBRARIES := openblas

fatal error: :pread failed:Is a directory

no implementation found for native

⚠️ **GitHub.com Fallback** ⚠️