Where to place JNI/native libraries in Android Studio Project

后端 未结 1 431
眼角桃花
眼角桃花 2020-12-19 21:25

I am trying to compile the following code:

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

into an apk file.

To do

相关标签:
1条回答
  • 2020-12-19 22:00

    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.

    0 讨论(0)
提交回复
热议问题