주섬주섬 개발일지

Android Studio .so 파일 생성 및 사용하기 본문

Programing/Android Studio

Android Studio .so 파일 생성 및 사용하기

zzu__e 2024. 2. 1. 11:52

.so 파일 Export

Native C++ 디폴트 프로젝트 생성 및 빌드&실행까지 완료한 상태

  1. Android Studio 메뉴바 Build > Build Bundles(s)
  2. Build > Build APK(s)
  3. Analyze APK… → 아래와 같은 app-debug.apk 선택

4. lib 폴더 안에 시스템 별로 .so 파일이 생성되어있는 것을 볼 수 있음

5. 해당 폴더로 이동하여 app-debug.apk를 압축 해제 (반디집 이용)

6.압축 해제 된 app-debug > lib > 하위 폴더들을 복사하여 사용할 수 있음

 

.so 파일 추가 및 사용

Native C++ 디폴트 프로젝트 생성 완료한 상태

  1. src>main 폴더에 jniLibs 폴더 생성 후 위의 폴더들을 복사 붙혀넣기

 

2. CMakeList.txt 라이브러리 추가 및 타겟 설정 (아래는 libmakefile 라이브러리를 추가)

# 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("sofiletest")


add_library(
        makefile
        SHARED
        IMPORTED)

set_target_properties(
        makefile
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libmakefile.so)


# 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.
        sofiletest

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp)

# 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.
        sofiletest
        makefile

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

3. MainActivity.java 라이브러리 추가

4. native-lib.cpp 라이브러리 함수 연결하여 사용 가능 (add(), subtract() 함수는 libmakefile의 함수임)