gpt4 book ai didi

android - 如何在Android Studio中添加.so、.mk等原生文件?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:00:49 27 4
gpt4 key购买 nike

我必须在我的 Android Studio 项目中添加原生文件.so、.mk、.sh、.cpp。在 Eclipse 中,.so 文件被添加到 libs 文件夹中,其他文件被添加到 jniobj 文件夹中,并且一切正常。但在 Android studio 中,因为我在 jniLibs 文件夹中添加了 .so 文件,并在 .gradle 文件中进行了如下更改

 ndk{
abiFilters 'armeabi', 'armeabi-v7a', 'x86'
}
sourceSets.main {
jniLibs.srcDir 'src/main/libs' //set libs as .so's location instead of jniLibs
jni.srcDirs = [] //disable automatic ndk-build call with auto-generated Android.mk
}

下面是我的项目结构和我需要添加的文件。有包含 native 代码的 jni 和 obj 文件夹,我必须使用它们。 Native files under jni, obj and jniLibs folders

我已经看到了可用的链接,但它们已经过时并且没有任何效果。任何帮助表示赞赏。

在进行以下更改后登录:

sourceSets.main {
jniLibs.srcDir 'src/main/libs' //set libs as .so's location instead of jniLibs
jni.srcDirs = [] //disable automatic ndk-build call with auto-generated Android.mk
}
externalNativeBuild {
ndkBuild {
path 'src/main/jni/Android.mk'
}
}

日志:

Error while executing 'E:\Android_SDK\ndk-bundle\ndk-build.cmd' with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=E:\folder\sampleapp\app\src\main\jni\Android.mk NDK_APPLICATION_MK=E:\folder\sampleapp\app\src\main\jni\Application.mk APP_ABI=armeabi-v7a NDK_ALL_ABIS=armeabi-v7a NDK_DEBUG=1 APP_PLATFORM=android-16 NDK_OUT=E:/folder/app/build/intermediates/ndkBuild/debug/obj NDK_LIBS_OUT=E:\folder\sampleapp\app\build\intermediates\ndkBuild\debug\lib APP_SHORT_COMMANDS=false LOCAL_SHORT_COMMANDS=false -B -n}
md "E:\folder\sampleapp\app\build\intermediates\ndkBuild\debug\lib\armeabi-v7a" >NUL 2>NUL || rem
copy /b/y "E:\Android_SDK\ndk-bundle\build\\..\prebuilt\android-arm\gdbserver\gdbserver" "E:\folder\sampleapp\app\build\intermediates\ndkBuild\debug\lib\armeabi-v7a\gdbserver" > NUL
E:/Android_SDK/ndk-bundle/build//../prebuilt/windows-x86_64/bin/echo.exe [armeabi-v7a] "Gdbserver ": "[arm-linux-androideabi] E:\folder\sampleapp\app\build\intermediates\ndkBuild\debug\lib/armeabi-v7a/gdbserver"
E:/Android_SDK/ndk-bundle/build//../prebuilt/windows-x86_64/bin/echo.exe [armeabi-v7a] "Gdbsetup ": "E:\folder\sampleapp\app\build\intermediates\ndkBuild\debug\lib/armeabi-v7a/gdb.setup"
E:/Android_SDK/ndk-bundle/build//../prebuilt/windows-x86_64/bin/echo.exe "directory E:/Android_SDK/ndk-bundle/build//../platforms/android-16/arch-arm/usr/include E:/folder/app/src/main/jni E:/Android_SDK/ndk-bundle/build//../sources/cxx-stl/system" >> E:\folder\sampleapp\app\build\intermediates\ndkBuild\debug\lib/armeabi-v7a/gdb.setup
E:/Android_SDK/ndk-bundle/build//../prebuilt/windows-x86_64/bin/echo.exe "set solib-search-path E:/folder/app/build/intermediates/ndkBuild/debug/obj/local/armeabi-v7a" > E:\folder\sampleapp\app\build\intermediates\ndkBuild\debug\lib/armeabi-v7a/gdb.setup
process_begin: CreateProcess(NULL, "", ...) failed.
make: *** No rule to make target `E:/folder/app/src/main/jni/SerialPort.c', needed by `E:/folder/app/build/intermediates/ndkBuild/debug/obj/local/armeabi-v7a/objs-debug/serial_port/SerialPort.o'. Stop.
Build command failed.
Error:executing external native build for ndkBuild E:\folder\sampleapp\app\src\main\jni\Android.mk
Build command failed.
Error while executing 'E:\Android_SDK\ndk-bundle\ndk-build.cmd' with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=E:\folder\sampleapp\app\src\main\jni\Android.mk NDK_APPLICATION_MK=E:\folder\sampleapp\app\src\main\jni\Application.mk APP_ABI=armeabi-v7a NDK_ALL_ABIS=armeabi-v7a NDK_DEBUG=0 APP_PLATFORM=android-16 NDK_OUT=E:/folder/app/build/intermediates/ndkBuild/release/obj NDK_LIBS_OUT=E:\folder\sampleapp\app\build\intermediates\ndkBuild\release\lib APP_SHORT_COMMANDS=false LOCAL_SHORT_COMMANDS=false -B -n}
make: *** No rule to make target `E:/folder/app/src/main/jni/SerialPort.c', needed by `E:/folder/app/build/intermediates/ndkBuild/release/obj/local/armeabi-v7a/objs/serial_port/SerialPort.o'. Stop.
process_begin: CreateProcess(NULL, "", ...) failed.
Error:executing external native build for ndkBuild E:\folder\sampleapp\app\src\main\jni\Android.mk

最佳答案

首先,确定你的源码位置,suites example。你有一些问题。所有原生文件都应该在 src/main/

../src/main
.........../jni # your cpp, header and mk files
.........../jniLibs # your folders with all libs
.........../java # other code

然后,确保在使用任何 native 、 Activity 、应用程序等之前将库加载到静态字段中。

private static final String LIB_NAME = "libserial_port";
private static final String LIB_NAME_1 = "libzqcom";
static {
System.loadLibrary(LIB_NAME);
System.loadLibrary(LIB_NAME_1);
}

更新 1:

确保您已经为 native Android 项目安装了 Android NDK 源。

From Android SDK Settings

关于android - 如何在Android Studio中添加.so、.mk等原生文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44254045/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com