1. app/src/main 하위에 jni 폴더 생성
2. "javah" External Tools 설정
javah는 java 10 버전부터 없어졌다고 하니, jdk 8 버전을 설치합니다.
Program: /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/bin/javah
Arguments: -classpath "$ContentRoot$/src/main/java" -v -jni "$FileClass$"
Working directory: "$ContentRoot$/src/main/jni"
3. Module Main에 loadLibrary 코드 작성
4. 헤더파일 생성
2단계에서 설정한 javah 명령어를 이용하여 헤더파일 생성 --> jni 디렉터리 하위에 메인모듈에 작성한 jni 코드를 바탕으로 헤더파일이 생성됩니다.
5. cpp 코드 작성
6. CMakeLists.txt 파일 작성
기본 틀은 다음과 같습니다.
# 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.10.2)
set( CPP_FILES
myfirstNDK.cpp)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
myfirstndk
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
${CPP_FILES} )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
myfirstndk
# Links the target library to the log library
# included in the NDK.
${log-lib}
android)
7. build.gradle(:app) 설정
ndkVersion 명시합니다.(ndk 버전은 SDKManager --> SDK Tools 탭에서 확인 가능)
android{ defaultConfig { externalNativeBuild { ... }}} 에서 cmake의 version 등을 명시합니다.
android{ externalNativeBuild { ... }} 에서 "CMakeLists.txt" 파일의 위치를 명시합니다.
plugins {
id 'com.android.application'
}
android {
compileSdk 30
// ndk version 명시
ndkVersion "23.0.7599858"
defaultConfig {
...
// externalNativeBuild 블록 안에서 cmake version, cppFlags, arguments 등 설정 가능
externalNativeBuild {
cmake {
version "3.18.1"
// cppFlags "-std=c++11", "-Wall"
// arguments "-DANDROID_STL=c++_static", "-DANDROID_ABI=arm64-v8a"
}
}
}
buildTypes {
...
}
compileOptions {
...
}
// Use this block to link gradle to your CMakeLists.txt file. only 'path' variable is valid here
externalNativeBuild {
cmake {
path "src/main/jni/CMakeLists.txt"
}
}
}
...
※ 특정 ABI(Application Binary Interface)를 위한 코드 생성
출처: https://developer.android.com/ndk/guides/abis#gc
android {
defaultConfig {
ndk {
abiFilters 'arm64-v8a'
}
}
}
8. 빌드 및 검사
(빌드)Build --> Make Project
(검사)Build --> Analyze APK. apk파일안에 빌드한 .so파일이 들어가있는지 확인합니다
※ 출처
https://question0.tistory.com/39 (기존 프로젝트에 NDK 설정)
https://math-coding.tistory.com/177 (NDK 프로젝트 예제)
https://web-inf.tistory.com/98 (NDK 프로젝트 상세한 예제)
https://github.com/android/ndk-samples (안드로이드 NDK 샘플 예제)
https://medium.com/@jrejaud/modern-android-ndk-tutorial-630bc11829a2 (Modern Android NDK Tutorial)
https://github.com/hankyojeong/Android-NDK_Study(안드로이드 NDK 스터디)
'Information Security > Android' 카테고리의 다른 글
Xposed Detection & Bypass (0) | 2021.10.01 |
---|---|
Android NDK Setting for LSPosed (0) | 2021.10.01 |
Android Native Memory Patch (0) | 2021.10.01 |
안드로이드 Native Hooking을 위한 Dobby Static Library 연결(Link Dobby Static Library for Android Native Hooking) (0) | 2021.09.29 |
Xposed 간단한 Module 제작 (0) | 2021.09.13 |