13

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 {
    ? ?
}
xiaomi
  • 6,553
  • 3
  • 29
  • 34

3 Answers3

20

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)

xiaomi
  • 6,553
  • 3
  • 29
  • 34
  • What if we have multiple Library, each having multiple Flavors?! For correct solution see: stackoverflow.com/[**Multi Flavor App with multi Flavor Library?**](https://stackoverflow.com/q/24860659/8740349) – Top-Master Apr 30 '22 at 03:53
2

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']
    }
}
Vladimir Petrovski
  • 1,966
  • 21
  • 25
0

I would should another way to implementation this:

First add a flavor and dimension to your app build.gradle which gradle id is com.android.application,like below:

plugins {
    id 'com.android.application'
}

android {
    resourcePrefix "app_"
    flavorDimensions "env"
    productFlavors {
        dev {
            dimension "env"
        }
        pre{
            dimension "env"
        }
        prod {
            dimension "env"
        }
    }
}

And then add the below code snippt to your library module

plugins {
    id 'com.android.library'
}

android {
    resourcePrefix "base_"

    flavorDimensions "env"
    productFlavors {
        register("dev")
        register("pre")
        register("prod")
    }
}

enter image description here

caopeng
  • 914
  • 13
  • 23