androidgradle

Android apk - How to exclude a .so file from a 3rd party dependency using gradle


I have an android project with several dependencies. Two of them (let's call them dependency A and B) have native libraries (.so files).

Dependency A has the following architectures: arm64-v8a, armeabi, armeabi-v7a, x86 and x86_64. Dependency B has the following architectures: armeabi, x86

Thus when my app runs on an armeabi-v7a device (for instance), and dependency B calls a native method, it cannot find the relevant library to get it from (as it is not present in armeabi-v7a folder and does not fall back automatically to armeabi where the library is).

Is there any way to work around this? For instance, can I add some configuration to my build.gradle file in order for arm64-v8a, armeabi-v7a, and x86_64 folders not to be integrated to my final apk?

I have tried packagingOptions / exclude, but with no results : the folders in questions are still there.


Solution

  • Try a clean build, I didn't and it was still picking up the files from the previous build. I ended up deleting my app/build/ folder just to be sure.

    android { 
        packagingOptions {
            exclude 'lib/armeabi-v7a/libSomeLibYouDontWant.so'
        }
    }
    

    Worked for me on an app that was previously crashing.

    An alternative would be to use

    android{
        defaultConfig{
            ndk{
                abiFilters "armeabi", "x86"
            }
        }
    }
    

    There are some similar questions which might be helpful

    Gradle exclude arm64 libs

    How to use 32-bit native libraries on 64-bit Android device