Android Studio 项目中 JNI/原生库的放置位置

Roy*_*son 7 java-native-interface android gradle android-ndk

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

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

到一个 apk 文件中。

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

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

但是,我不知道将它们放在哪里。我能找到的唯一参考是

1]如何在android studio项目中添加JNI库?但是,我的项目结构看起来与屏幕截图不同。

2]在 Android Studio 中创建 jni 文件夹的位置,该文​​件夹是旧的/过时的/缺乏详细信息。

这是我的项目结构:

项目_结构

shi*_*hen 7

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

\n\n
.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 CMakeLists.txt // Your cmake configuration files. \n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app.iml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 build\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 build.gradle\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 libs\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 proguard-rules.pro\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 androidTest\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 java\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 AndroidManifest.xml\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 cpp // Directory to put your jni native source code. \n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 native-lib.cpp\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 java\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 jniLibs // Directory to put your jni libs, i.e. the .so files. \n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 res\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test\n        \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 java\n
Run Code Online (Sandbox Code Playgroud)\n\n

jniLibs但是,理论上您可以在应用程序级文件中的任何位置配置路径build.gradle

\n\n
android {\n    ...\n    defaultConfig {\n        ...\n        externalNativeBuild {\n            cmake {\n                cppFlags "-frtti -fexceptions"\n            }\n        }\n    }\n    ...\n    externalNativeBuild {\n        cmake {\n            path "CMakeLists.txt"\n        }\n    }\n    ...\n    sourceSets {\n        main {\n            // put your jni libs.\n            jniLibs.srcDirs += "${projectDir}/jniLibs"]\n        }\n        debug {\n            // put your debug version jni libs.\n            jniLibs.srcDirs += "${projectDir}/jniLibs/debug"]\n        }\n        release {\n            // put your release version jni libs.\n            jniLibs.srcDirs += "${projectDir}/jniLibs/release"]\n        }\n    }\n    ...\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

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

\n