Where to place JNI/native libraries in Android Studio Project
Asked Answered
P

2

7

I am trying to compile the following code:

https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/master

into an apk file.

To do this, I created a default Android project with an empty activity. Afterwards, I added the relevant java files from the repository to my project and made some modifications. I also added the appropriate xml/image resources to my project.

Now, I need to add the JNI/native libraries to my project. https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/master/native/

However, I don't know where to place them. The only references I could find were

1] How to add JNI Libraries in android studio project? But, my project structure looks different from the screenshot.

and

2] Where to create jni folder in Android Studio which is old/outdated/lacks detail.

Here is my project structure:

Project_Structure

Philipps answered 27/8, 2018 at 20:48 Comment(1)
Just making sure you looked at: developer.android.com/ndk/guides/buildFerrante
M
9

The typical structure of an Android project with jni support is as below:

.
├── CMakeLists.txt // Your cmake configuration files. 
├── app.iml
├── build
├── build.gradle
├── libs
├── proguard-rules.pro
└── src
    ├── androidTest
    │   └── java
    ├── main
    │   ├── AndroidManifest.xml
    │   ├── cpp // Directory to put your jni native source code. 
    │   │   └── native-lib.cpp
    │   ├── java
    │   ├── jniLibs // Directory to put your jni libs, i.e. the .so files. 
    │   └── res
    └── test
        └── java

But, theoretically you can configure your jniLibs path anywhere that you like inside the app level build.gradle file.

android {
    ...
    defaultConfig {
        ...
        externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
            }
        }
    }
    ...
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
    ...
    sourceSets {
        main {
            // put your jni libs.
            jniLibs.srcDirs += "${projectDir}/jniLibs"]
        }
        debug {
            // put your debug version jni libs.
            jniLibs.srcDirs += "${projectDir}/jniLibs/debug"]
        }
        release {
            // put your release version jni libs.
            jniLibs.srcDirs += "${projectDir}/jniLibs/release"]
        }
    }
    ...
}

For Android Studio 3.0+, you don't need to explicitly configure the jniLibs path for your c/c++ source code as it will be automatically managed by Android Studio. All those c/c++ source code under src/main/cpp will be compiled and packaged into your apk automatically.

Misalliance answered 28/8, 2018 at 1:36 Comment(1)
i have .so file of other app that i have decompile i don't have native-lib.cpp file of that .so means i don't have native code i only have .so can i integrate and run it in my project because when i m runing the project this error is coming java.lang.UnsatisfiedLinkError: No implementation found for int com.krunal.camarademo.Libs.nativesystem.DMDHDR.getBracketedCount() (tried Java_com_krunal_camarademo_Libs_nativesystem_DMDHDR_getBracketedCount and Java_com_krunal_camarademo_Libs_nativesystem_DMDHDR_getBracketedCount__)Peripeteia
U
1

Based on shizhen answer

Year Gradle Version Android Studio Version CMake Version NDK Version
2024 8.4 Android Studio Iguana - 2023.2.1 3.22.1 26.2.11394342

build.gradle.kts - module or app

android {
 
    ...
 
    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
            sourceSets {
                getByName("release"){
                    jniLibs.srcDirs("src/main/nativeLibs/Release")
                }
            }

        }
        debug {
            sourceSets {
                getByName("debug"){
                    jniLibs.srcDirs("src/main/nativeLibs/Debug")
                }
            }
        }
    }

    ...

}

With the Android perspective. Only the "jniLibs" folder is shown with the respective ABIs. Check mark (debug) or (release) depending on the selected variant.

Android perspective - Debug Android perspective - Release Project perspective
debug release project perspective

CMakeList.txt

${CMAKE_BUILD_TYPE} can be Debug or Release when building is active.
${CMAKE_ANDROID_ARCH_ABI} can be amr64-v8a or armeabi-v7a or x86 or x86_64 when building is active.

cmake_minimum_required(VERSION 3.22.1)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
project("xdata")

set(LIB_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../nativeLibs/${CMAKE_BUILD_TYPE}/${CMAKE_ANDROID_ARCH_ABI})
set(MY_LIB "${LIB_PATH}/libMy.so")

include_directories(${CMAKE_SOURCE_DIR}/include)

add_library(${CMAKE_PROJECT_NAME} SHARED
        libinsane.cpp)

target_link_libraries(${CMAKE_PROJECT_NAME}
        android
        ${MY_LIB}
        log
        )
Ung answered 6/5 at 4:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.