8

I trying to build android application with some precompiled native libraries: liba.so and libb.so.1.2.3

Libraries are placed into jniLibs subdirectory. After building APK file, only liba.so included into it, but not libb.so.1.2.3.

Result is predictable. Application crashes at start.

What to do with build scripts to include all files from jniLibs into APK?

kostashv
  • 111
  • 3

3 Answers3

4

Due to the native library regex ^.+\\.so$ being used in the Android Gradle plugin, it is impossible to include anything other than .so files using the jniLibs folder. And even if you were to somehow add the library to the APK, the dynamic loader on Android is very limited and will most likely not be able to load them.

Your best bet is to remove the version altogether by renaming the library and changing its internal soname before linking against it.

alex
  • 1,228
  • 1
  • 16
  • 38
2

Unfortunately I don't develop for Android anymore, so I can't test this, but I know Gradle and this might work for you. Looking at the Android DSL docs, you might be able to change the filtering on the jniLibs folder:

android {
    sourceSets {
        main {
            jniLibs.filter.include("**/*")
        }
    }
}

Let me know if this works!

MartinTeeVarga
  • 10,478
  • 12
  • 61
  • 98
  • I have tried this, it gives an error: **Cannot set readonly property: filter for class: com.android.build.gradle.internal.api.DefaultAndroidSourceDirectorySet** – kostashv Mar 20 '17 at 08:43
  • Can you try setting the whole jniLibs? I can try to test it maybe tomorrow, if I can install the Android SDK. But it would be something like `jniLibs { filter = '' srcDir = '' }` etc (all properties from the docs). – MartinTeeVarga Mar 20 '17 at 09:35
  • So the filter is a PatternSet. I've updated my answer. This compiles (I tried it with Android plugin) but I can't deploy the app as I don't have any .so to try. – MartinTeeVarga Mar 21 '17 at 00:17
  • @kostashv I wonder if you have found your answer, it's an interesting problem ;) – MartinTeeVarga Mar 30 '17 at 01:59
  • 1
    I have tried code from your updated answer. It compiles, but versioned lib still not packed into apk. Also I have tried to **exclude** not versioned libs from apk and that didn't work to. – kostashv Mar 30 '17 at 19:09
-1

Just add the jniLibs folder in app/src/main and it will include the .so file in the apk.

/app/src/main/armeabi/*.so files

Alessandro Lallo
  • 741
  • 11
  • 21