Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fail to import android support v4 or v7 in library using productFlavors

I am setting up an Android Library with different productFlavors. The library has a light and a full flavor. The file is setted up correctly:

src/main/java/com/example/... for main classes
src/full/java/com/example/... for full classes
src/light/java/com/example/... for light classes

Android Studio correctly understood this and added a (full) to the full flavor.

ISSUE : The dependencies like okhttp is working as expected but NOT the appcompat-v7. Everything using ViewPager, FragmentActivity, RecyclerView. I've tried added the dependencies to the fullCompile but it didn't work either. The dependencies is not resolved by Android Studio, import is not working, except ok okhttp, exoplayer and so on.

I've tried Invalidate Cache/Restart, clean Project, Resync gradle, none worked.

Library build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    ...

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

    publishNonDefault true

    productFlavors {
        full {
        }
        light {
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:23.1.1'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.squareup.okhttp:okhttp:2.3.0'
    fullCompile 'com.android.support:support-v4:23.1.1'
    fullCompile 'com.android.support:appcompat-v7:23.1.1'
    fullCompile 'com.android.support:recyclerview-v7:23.1.1'
    fullCompile 'com.squareup.picasso:picasso:2.5.0'
}

App build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    buildTypes {
        release {
        }
    }

    productFlavors {
        full {
        }
        light {
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.+'
    compile 'com.android.support:support-v4:23.+'
    compile 'com.google.android.gms:play-services-base:7.5.0'
    compile 'com.google.android.gms:play-services-ads:7.5.0'
    compile 'com.google.android.gms:play-services-location:7.5.0'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    fullCompile project(path: ':library', configuration: 'fullRelease')
    lightCompile project(path: ':library', configuration: 'lightRelease')
}
like image 634
Hugo Gresse Avatar asked Dec 14 '15 13:12

Hugo Gresse


1 Answers

You have to declare the configuration in the app gradle. When it is related to a library, the configurations are not declared properly. Try:

configurations {
    fullDebugCompile
    fullReleaseCompile
    lightDebugCompile
    lightReleaseCompile
}

dependencies {
...
    fullDebugCompile project(path:":library", configuration:"fullDebug")
    fullReleaseCompile project(path:":library", configuration:"fullRelease")
    lightDebugCompile project(path:":library", configuration:"lightDebug")
    lightReleaseCompile project(path:":library", configuration:"lightRelease")
}

Long explanation

The gradle android plugin uses different implementations for the app and the library, called AppVariant and LibraryVariant respectively. Sometimes the way variants and build types work is different in both kind of projects. In this case, some time ago the libraries always were compiled in the release build type within a given variant, something that made library projects not so flexible as apps were.

That is why they decided to enable the publishNonDefault option and bring support in Android Studio for this kind of behaviours, so you can use different builds of a library in different builds of an app, but you have to specify which build uses which library. This is the reason that makes you declare the configurations explicitly.

The convention name used by the Android Build Tools team is {buildType}{flavor}TaskName, so for the classpath configuration you have to use the same name.

All of this process has a downside, which is if you publish non default dependencies, the android plugin will ensure all of the possible library configurations are compiled before your app is built, so the build time can increase a bit (depending on the library size)

like image 126
droidpl Avatar answered Sep 21 '22 14:09

droidpl