Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

gradle - Where to place JNI/native libraries in Android Studio Project

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...