Problem with gradle build flavour and packagingOptions

Viewed 873

I have been developing for a while a library (in aar format) that is compatible with x86, armeabi-v7a and arm64-v8a abis. The library works as expected but, in order to reduce the final size, we want to develop two different libraries: one with arm64-v8a abi and one without it. The library depends on openCV (used in C++ language) and we have the following project structure:

src
 |_ main
     |_ jniLibs
           |    | arm64-v8a
           |----| armeabi-v7a
                | x86

Each abi folder contains libopencv_java3.so.

I have created two product flavours, each one with the abiFilters needed to work properly.

flavorDimensions "default"
productFlavors {
    v7a {
        externalNativeBuild {
            cmake {
                abiFilters 'x86', 'armeabi-v7a'
            }
        }
    }
    v8a {
        externalNativeBuild {
            cmake {
                abiFilters 'x86', 'armeabi-v7a', 'arm64-v8a'
            }
        }
    }
}

Now I have two build variants to select and both compile without errors. However, the v7a aar still contains the arm64-v8a folder and library inside it. To solve this, I have added to v7a flavour the following:

packagingOptions {
    exclude 'lib/arm64-v8a/libopencv_java3.so'
}

Now, v7a aar does not contain the arm64-v8a folder inside it but when I select v8a flavour, the folder exists but no libopencv_java3.so is placed inside it!

Should not this option only affect to v7a? What am I doing wrong? Any help will be appreciated.

Notes: gradle version tested: 3.1.2 and 3.1.4.

1 Answers

try to split differently, instead of using product favors -

alike this one can load armeabi-v7a assembly on arm64-v8a:

splits {
    abi {
        enable true
        reset()
        include "armeabi", "x86"
        universalApk true
    }
}

this would expect the (32bit version of the) library at armeabi/libopencv_java3.so.

externalNativeBuild only considers the libraries, which are being built (aka "your code"). if you insist on using the packagingOptions, you should not define them globally, but per product flavor.

v7a {
    ...
    packagingOptions {
        exclude 'lib/arm64-v8a/libopencv_java3.so'
    }
}
Related