在Android Studio Project中何处放置JNI/本机库 [英] Where to place JNI/native libraries in Android Studio Project

查看:135
本文介绍了在Android Studio Project中何处放置JNI/本机库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编译以下代码:

I am trying to compile the following code:

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

转换为apk文件.

为此, 我创建了一个默认的Android项目,活动为空.之后,我将资源库中的相关Java文件添加到了我的项目中,并进行了一些修改.我还向项目添加了适当的xml/image资源.

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.

现在,我需要将JNI/本地库添加到我的项目中. https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/master/native/

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] 如何在android studio项目中添加JNI库?但是,我的项目结构看起来与屏幕截图不同.

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

2] 在Android Studio中在何处创建jni文件夹这是旧的/过时的/缺少细节.

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

这是我的项目结构:

推荐答案

受jni支持的Android项目的典型结构如下:

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

但是,理论上,您可以在 app build.gradle文件中的任意位置配置jniLibs路径.

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"]
        }
    }
    ...
}


对于Android Studio 3.0+,您无需为c/c++源代码显式配置jniLibs路径,因为它将由Android Studio自动管理. src/main/cpp下的所有那些c/c++源代码将被自动编译并打包到您的apk中.


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.

这篇关于在Android Studio Project中何处放置JNI/本机库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆