Skip to content

Instantly share code, notes, and snippets.

@Ssioo
Last active November 28, 2022 08:12
Show Gist options
  • Save Ssioo/52564bf2986a1c65af93e6aae2d4be1b to your computer and use it in GitHub Desktop.
Save Ssioo/52564bf2986a1c65af93e6aae2d4be1b to your computer and use it in GitHub Desktop.
Android NDK settings example
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.whoissio.ndkexample"
minSdk 30
targetSdk 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
arguments '-DANDROID_ARM_NEON=ON', '-DHAVE_NEON=1'
// cFlags ''
cppFlags ''
// targets '', ''
}
}
ndk {
abiFilters "armeabi-v7a", "arm64-v8a"
}
}
ndkVersion "25.0.8775105"
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
jniDebuggable true
packagingOptions { doNotStrip "**//.so" }
}
}
aaptOptions {
noCompress "tflite"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.18.1'
}
}
buildFeatures {
viewBinding true
}
sourceSets {
main {
// let gradle pack the shared library into apk
jniLibs.srcDirs = ['../libs/tensorflowlite/jni']
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.core:core:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.18.1)
# Declares and names the project.
project("ndk_example") # 본 프로젝트 이름
# Build 시에 사용할 추가 변수 선언
set(TFLITE_LIBPATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/tensorflowlite/jni")
set(TFLITE_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/tensorflowlite/headers")
# Build 시의 option을 override
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -O3")
# set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -u ANativeActivity_onCreate") # NativeActivity Class를 통해서 Call할 때 사용.
# main. ndk_example 이라는 지정자로 cpp 내의 지정한 cpp파일들을 하나의 라이브러리로 build 해줌.
# Java 단에서 이 지정자로 System.loadLibrary("ndk_example") 을 통해 C 함수들을 호출할 수 있음.
add_library(ndk_example SHARED
ndk_example_jni.cpp
...
)
# native_app_glue
# prebuilt 되진 않았지만, 외부 Absolute 경로에 존재하는 코드들. Util function이 많아 유용.
include_directories(${CMAKE_ANDROID_NDK}/sources/android/native_app_glue)
add_library(native_app_glue STATIC ${CMAKE_ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)
# cpu-features
include_directories(${CMAKE_ANDROID_NDK}/sources/android/cpufeatures)
add_library(cpufeatures STATIC ${CMAKE_ANDROID_NDK}/sources/android/cpufeatures/cpu-features.c)
# ndk-helper
include_directories(${CMAKE_ANDROID_NDK}/sources/android/ndk_helper)
file(GLOB _NDK_HELPER_SRCS ${CMAKE_ANDROID_NDK}/sources/android/ndk_helper/*.cpp ${CMAKE_ANDROID_NDK}/sources/android/ndk_helper/gl3stub.c ${CMAKE_ANDROID_NDK}/sources/android/ndk_helper/NDKHelper.h)
add_library(ndk_helper ${_NDK_HELPER_SRCS})
unset(_NDK_HELPER_SRCS)
# 위의 외부 코드의 header file들을 target_include_directories를 통해 엮어줌으로서 우리 코드에서 사용가능하도록 빌드.
target_include_directories(omnitime_jni PUBLIC
${CMAKE_ANDROID_NDK}/sources/android/native_app_glue
${CMAKE_ANDROID_NDK}/sources/android/cpufeatures
${CMAKE_ANDROID_NDK}/sources/android/ndk_helper)
# 아래는 prebuilt된 라이브러리를 Import하는 방법. ex. tensorflow lite.
# Binary File (.so)의 경로와, header File (.h)이 들어있는 폴더의 경로를 지정해서 하나의 Library로 엮어주면 된다.
# tensorflow-lite
add_library(lib_tensorflowlite_gpu SHARED IMPORTED)
set_target_properties(lib_tensorflowlite_gpu PROPERTIES IMPORTED_LOCATION
${TFLITE_LIBPATH}/${CMAKE_ANDROID_ARCH_ABI}/libtensorflowlite_gpu_delegate.so)
add_library(lib_tensorflowlite_cpp SHARED IMPORTED)
set_target_properties(lib_tensorflowlite_cpp PROPERTIES IMPORTED_LOCATION
${TFLITE_LIBPATH}/${CMAKE_ANDROID_ARCH_ABI}/libtensorflowlite.so)
include_directories(${TFLITE_INCLUDE})
target_include_directories(omnitime_jni PRIVATE ${TFLITE_INCLUDE})
# 앞에서 엮었던 외부 Libaray를 우리 내부 library에 link.
# 이외에도 기본 API를 link 가능하다. https://developer.android.com/ndk/guides/stable_apis?hl=ko
# 기본 API는 prefix인 lib를 빼고 입력하면 된다.
target_link_libraries(ndk_example
android
cpufeatures
ndk_helper
EGL
nativewindow
mediandk
OpenMAXAL
GLESv3
lib_tensorflowlite_gpu
lib_tensorflowlite_cpp
z
log)
# 다른 지정자로 일부 파일을 다른 이름의 라이브러리로 build할 수 있다. Ex. System.loadLibrary("ndk_example_test")
add_library(ndk_example_test SHARED
test_tflite.cpp)
# 앞에서 엮었던 외부 Libaray를 우리 내부 library에 link.
target_link_libraries(ndk_example_test
android
ndk_helper
cpufeatures
EGL
nativewindow
GLESv3
lib_tensorflowlite_gpu
lib_tensorflowlite_cpp
z
log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment