Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

android - Add .so (prebuilt) library from another directory to APK

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, /vendor/lib, /system/lib]]] couldn't find "libtheprebuiltlib.so"

Common solutions I see in SO is this:

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

Tried both

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

and

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

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

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})

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']
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).

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'

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

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

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

One mistake in your build.gradle, it should read

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...