将 .so(预构建)库从另一个目录添加到 APK [英] Add .so (prebuilt) library from another directory to APK

查看:14
本文介绍了将 .so(预构建)库从另一个目录添加到 APK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从另一个目录中包含我的 .so 库.编译我的项目工作正常.但是当我运行它时,它给了我

I am trying to include my .so library from another directory. Compiling my project works fine. But when I run it, it gives me

java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.company.gimikapp-2/base.apk"],nativeLibraryDirectories=[/data/app/com.company.gimikapp-2/lib/arm,/v​​endor/lib,/system/lib]]] 找不到libtheprebuiltlib.so"

java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.company.gimikapp-2/base.apk"],nativeLibraryDirectories=[/data/app/com.company.gimikapp-2/lib/arm, /vendor/lib, /system/lib]]] couldn't find "libtheprebuiltlib.so"

我在 SO 中看到的常见解决方案是这样的:

Common solutions I see in SO is this:

sourceSets {
    main {
        jniLibs.srcDirs = ['src/main/jniLibs']
    }
}

都试过了

jniLibs.srcDirs = ['C:\svn\sys_libs']

jniLibs.srcDirs = ['C:/svn/sys_libs']

您实际上如何将它指向您的 Android 项目之外的另一个目录?

How do you actually point it to another directory outside your Android project?

这是我的 CMakeList.txt:

This is my CMakeList.txt:

cmake_minimum_required(VERSION 3.4.1)
add_library( native-lib
            SHARED
            src/main/cpp/source/native-lib.cpp )
add_library(theprebuiltlib SHARED IMPORTED)
set_target_properties(theprebuiltlib PROPERTIES IMPORTED_LOCATION
            C:/svn/sys_libs/libtheprebuiltlib.so)
target_include_directories(
            native-lib PRIVATE
            src/main/cpp/source/
            C:/svn/sys_includes/)
find_library( log-lib
            log)
target_link_libraries( native-lib
            theprebuiltlib
            ${log-lib})

这是我的 JNI 的 gradle 设置:

And here is my gradle setup for my JNI:

android {
    ...
    defaultConfig {
        ...
        externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
            }
            ndk {
                abiFilters 'armeabi'
            }
        }
        ...
    }
    ...
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['C:/svn/sys_libs']
        }
    }
}

推荐答案

显然,您已经安装了 NDK r17 和 Android 插件 v.3.1.0 或更高版本(我们在 build.gradle 的已发布片段中没有看到这一点).

Apparently, you have NDK r17 installed and Android plugin v.3.1.0 or higher (we don't see this in the published fragment of build.gradle).

但是您将 abiFilters 设置为 armeabi,它已被删除.您应该将其设置为 armeabi-v7a,并确保 libtheprebuiltlib.so 也是为此 ABI 构建的,或者您可以 下载旧版本的 NDK 和 build.gradle 依赖集

But you set abiFilters to armeabi, which has been dropped. You should set it to armeabi-v7a, and make sure that libtheprebuiltlib.so is also built for this ABI, or you can download an older version of NDK and in build.gradle dependencies set

classpath 'com.android.tools.build:gradle:3.0.1'

如果你明确设置,你可以强制使用最新的插件句柄armeabi:

You can force the latest plugin handle armeabi if you set it explicitly:

android {
    defaultConfig {
        ndk {
            abiFilters 'armeabi'
        }
    }
}

(在你的脚本中,它在 android.defaultConfig.externalNativeBuild.ndk 下,所以没有效果).

(in your script, it is under android.defaultConfig.externalNativeBuild.ndk, so has no effect).

你的 build.gradle 中有一个错误,它应该是

One mistake in your build.gradle, it should read

android {
  sourceSets {
     main { 
       jniLibs.srcDir 'C:/svn/sys_libs' 
     }
   }
}

当你有文件C:svnsys_libsarmeabilibtheprebuiltlib.so.但这并不能解释为什么 cmake 不能按预期工作.

when you have the file C:svnsys_libsarmeabilibtheprebuiltlib.so. But this does not explain why cmake does not work as expected.

这篇关于将 .so(预构建)库从另一个目录添加到 APK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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