0

I create a library module with two flavors. Each flavor implements a FlavourActivity.java in a different way. In my app I'm what to import my library module as a dependency, but I want to specify which flavor I'm going to import.

My problem is that I'm able to add the dependency in my app gradle file, but I cannot access the FlavourActivity.java. Somehow my app does not have access to it, although it has imported the library.


Structure

My structure is the following:

app/
├── src/
│   ├── Activity.java

Library/
├── lottieYes/
│   ├── FlavourActivity.java
├── lottieNo/
│   ├── FlavourActivity.java

Android Plugin Version: 3.4.0 Gradle Version: 5.1.0


Recources

My app gradle file is the following:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28

    defaultConfig {
        applicationId "com.asousa.testing.moduleflavour"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }

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

dependencies {
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'

    implementation project(path: ':mylibrary', configuration: 'lottieYes')
}

My library gradle file is the following:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28

    publishNonDefault true

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }

    flavorDimensions "lottie"

    productFlavors {
        lottieYes {
            dimension "lottie"
        }

        lottieNo {
            dimension "lottie"
        }
    }

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

        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

configurations {
    lottieYes
    lottieNo
}

dependencies {
    implementation 'com.android.support:appcompat-v7:28.0.0'
    lottieYesImplementation 'com.airbnb.android:lottie:2.5.6'
}


I already read the following articles:

I also checked the following question, but it not solve my issue since with the latest gradle version it does not work.

But none of them helped me. Does someone had a similar issue?

André Sousa
  • 1,692
  • 1
  • 12
  • 23
  • Possible duplicate of [Multi flavor app based on multi flavor library in Android Gradle](https://stackoverflow.com/questions/24860659/multi-flavor-app-based-on-multi-flavor-library-in-android-gradle) – JensV May 15 '19 at 14:50
  • Already read that post, not working. – André Sousa May 15 '19 at 15:12

2 Answers2

0

For the future record, I found a solution to my problem by declaring the java.srcDirs and res.srcDirs according to the feature state.


I declare a gradle external property declaring if the feature is enabled or not.

project.ext {
  hasLottie = true
}

More info on this blog post


And on my library gradle file:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28

    publishNonDefault true

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }

    sourceSets {
        main {
            if (hasLottieSupport.toBoolean()) {
                java.srcDirs += 'lottieYes/java'

            } else {
                java.srcDirs += 'lottieNo/java'
                res.srcDirs += 'lottieNo/res'
            }
        }
    }

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

        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation 'com.android.support:appcompat-v7:28.0.0'

    if (hasLottieSupport.toBoolean()) {
        implementation 'com.airbnb.android:lottie:2.5.6'
    }
}
André Sousa
  • 1,692
  • 1
  • 12
  • 23
0

You can try this.

apply plugin: 'com.android.application'

android {
   ......
   flavorDimensions "lottie"

   productFlavors {
     lottieYes {
        dimension "lottie"
     }

     lottieNo {
        dimension "lottie"
     }
   }
}

dependencies {
   ......
   lottieNoimplementation project(path: ':mylibraryX')
   lottieYesimplementation project(path: ':mylibraryY')

}