在 Android Studio 项目中放置 JNI/本机库的位置 [英] Where to place JNI/native libraries in Android Studio Project

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

问题描述

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

解决方案

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

<代码>.├── CMakeLists.txt//你的 cmake 配置文件.├── app.iml├── 建造├── build.gradle├── 库├── proguard-rules.pro└── 源├── androidTest│ └── java├── 主要│ ├── AndroidManifest.xml│ ├── cpp//存放 jni 原生源代码的目录.│ │ └── native-lib.cpp│ ├── java│ ├── jniLibs//存放 jni 库的目录,即 .so 文件.│ └── res└── 测试└── java

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

<代码>android {...默认配置 {...外部原生构建 {制作{cppFlags "-frtti -fexceptions"}}}...外部原生构建 {制作{路径CMakeLists.txt"}}...源集{主要的 {//放置你的 jni 库.jniLibs.srcDirs += "${projectDir}/jniLibs"]}调试{//把你的调试版本 jni libs.jniLibs.srcDirs += "${projectDir}/jniLibs/debug"]}发布 {//把你的发布版本 jni libs.jniLibs.srcDirs += "${projectDir}/jniLibs/release"]}}...}

<小时>

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

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:

解决方案

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.

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

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