Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with gradle build flavour and packagingOptions

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.

like image 780
Miguel Isla Avatar asked Sep 13 '18 12:09

Miguel Isla


People also ask

What is productflavours in Gradle?

Till Gradle introduced ProductFlavours we were only able to build application with the a single configuration or default configuration. Since the introduction of ProductFlavours, we are able to create variables based on build types.

What is a flexible build system like Gradle?

A flexible build system like Gradle and its Android specific plugin allows us to configure builds for different versions of the application. Versions of app could be based on deployment environments (dev, qa, prod) or products (free, paid) or device (phone, tv, wear) on which app is running on.

Why is Gradle--version not working?

Other installation failures If gradle --version works, but all of your builds fail with the same error, it is possible there is a problem with one of your Gradle build configuration scripts. You can verify the problem is with Gradle scripts by running gradle help which executes configuration scripts, but no Gradle tasks.

Why can't I see Gradle in my system?

If not, here are some things you might see instead. If you get "command not found: gradle", you need to ensure that Gradle is properly added to your PATH. If you get something like: ERROR: JAVA_HOME is set to an invalid directory Please set the JAVA_HOME variable in your environment to match the location of your Java installation.


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'
    }
}
like image 66
Martin Zeitler Avatar answered Oct 20 '22 21:10

Martin Zeitler