Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include library with flavor android

Tags:

android

gradle

My app gradle file before:

compile project(path: ':zblelib')

But when i add flavors into the lib my import don't work

my flavors:

flavorDimensions "dim"
    productFlavors {
        nocustomer {
            versionNameSuffix "-nocustomer"
            dimension = "dim"
        }
        customer001 {
            versionNameSuffix "-customer001"
            dimension = "dim"
        }
    }

how can i import my new library with choice of flavor?

edit: my build.gradle

library

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'

    defaultConfig {
        minSdkVersion 18
        targetSdkVersion 26
    }
    buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

    }

    flavorDimensions "dim"
    productFlavors {
        nocustomer {
            versionNameSuffix "-nocustomer"
            dimension = "dim"
        }
        customer001 {
            versionNameSuffix "-customer001"
            dimension = "dim"
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:27.1.1'
    compile 'com.android.support:design:27.1.1'
    compile 'com.android.support:support-v4:27.1.1'
    compile project(':criptolib-debug')
}

app

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'
    defaultPublishConfig "nocustomerRelease"

    defaultConfig {
        applicationId "com.axesstmc.bleappphone"
        minSdkVersion 18
        targetSdkVersion 26
        versionCode 91
        versionName "8.2"
    }

    buildTypes {
        debug {
            minifyEnabled false
            //proguardFiles 'proguard-rules.pro'
        }
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    ? ?
}
like image 595
Andrea Giga Ravalli Avatar asked Apr 13 '18 11:04

Andrea Giga Ravalli


People also ask

Where should I add library in Android Studio?

Navigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Library Dependency in the dropdown. In the Add Library Dependency dialog, use the search box to find the library to add.

What is productFlavors Android?

Product Flavors so what are they? Simply put, a product flavor is a variant of your app. It is very useful when you want to create multiple versions of your app. This means you can generate different versions or variants of your app using a single codebase.

What is Buildtype in Gradle Android?

Build types define certain properties that Gradle uses when building and packaging your app, and are typically configured for different stages of your development lifecycle.


2 Answers

The problem the app has is that it doesn't know which library's flavor to use.

The keyword matchingFallbacks will tell the app which library's flavor you want to select. But this keyword has to be use with a Flavor.

We have to add a flavor (+ dimension) on your app build.gradle:

android {
    ...
    //flavorDimensions is mandatory with flavors. Use the same name on your 2 files to avoid other conflicts.
    flavorDimensions "dim"
    productFlavors {
        nocustomer{
            dimension "dim"

            // App and library's flavor have the same name.
            // MatchingFallbacks can be omitted
            matchingFallbacks = ["nocustomer"]
        }
        customerNb{
            dimension "dim"

            // Here the app and library's flavor are different
            // Matching fallbacks will select the library's flavor 'customer001'
            matchingFallbacks = ["customer001"]
        }
    }
    ...
}
dependencies {
    implementation project(':zblelib')
}

In this way, when you select the app's flavor nocustomer, the library's flavor will select automatically nocustomer, and when you select the app's flavor customerNb, the library's flavor will select automatically customer001

PS

I am using implementation instead of compile, because compile is deprecated (see here)

like image 165
xiaomi Avatar answered Oct 13 '22 13:10

xiaomi


You should be using missingDimensionStrategy in your app's build.gradle file where it matches the missing flavors from your library. Check migration docs for Gradle 3.0.0 how to implement it.

For your particular problem where your library includes product flavors that the app doesn't check section A library dependency includes a flavor dimension that your app does not. from the table.

EDIT: Define flavors in your app's build.gradle file and then use matchingFallbacks to specify exactly which flavor you are to match from the libraries.

productFlavors {
    paid {
        dimension 'tier'
        matchingFallbacks = ['customer001', 'nocustomer']
    }
    free {
        dimension 'tier'
        matchingFallbacks = ['nocustomer', 'customer001']
    }
}
like image 37
Vladimir Petrovski Avatar answered Oct 13 '22 12:10

Vladimir Petrovski