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

前端 未结 2 1480
小蘑菇
小蘑菇 2020-12-07 02:49

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

相关标签:
2条回答
  • 2020-12-07 03:22

    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:\svn\sys_libs\armeabi\libtheprebuiltlib.so. But this does not explain why cmake does not work as expected.

    0 讨论(0)
  • 2020-12-07 03:27

    For NDK r16 and Gradle plugin v3.1.2, here is the step by step solution (thanks to @Alex Cohn).

    1. Set android.sourceSets.main.jniLibs.srcDir to point to the location of external .so

      android {
          sourceSets {
              main { 
                  jniLibs.srcDir 'C:/svn/sys_libs' 
              }
         }
      }
      
    2. Make sure that your external .so is located in the above path (step1), appended by correct architecture. In my case, it is armeabi, thus my .so is in

      C:/svn/sys_libs/armeabi/libtheprebuiltlib.so
      
    3. Move ndk from android.defaultConfig.externalNativeBuild.ndk to android.defaultConfig.ndk like so

      android {
          defaultConfig {
              ndk {
                  abiFilters 'armeabi'
              }
          }
      }
      
    4. Make sure that CMakeLists.txt points to the complete path with proper architecture.

      set_target_properties(theprebuiltlib PROPERTIES IMPORTED_LOCATION
              C:/svn/sys_libs/armeabi/libtheprebuiltlib.so)
      
    0 讨论(0)
提交回复
热议问题