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

查看:249
本文介绍了将另一个目录中的.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文件"/data/app/com.company.gimikapp-2/base.apk"],nativeLibraryDirectories=[/data/app/com. company.gimikapp-2/lib/arm,/vendor/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,并确保还为此ABI构建了libtheprebuiltlib.so,或者可以

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:\svn\sys_libs\armeabi\libtheprebuiltlib.so时.但这不能解释为什么 cmake 无法按预期工作.

when you have the file C:\svn\sys_libs\armeabi\libtheprebuiltlib.so. But this does not explain why cmake does not work as expected.

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

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