android - 无法使用 productFlavors 在库中导入 android 支持 v4 或 v7

标签 android android-gradle-plugin android-support-library android-productflavors

我正在设置一个具有不同productFlavorsAndroid 库。 该库具有清淡完整 flavor 。文件设置正确:

src/main/java/com/example/... 主要类
src/full/java/com/example/... 完整类
src/light/java/com/example/... 用于光照类

Android Studio 正确地理解了这一点并添加了一个 (full) 到 full flavor。

问题: okhttp 等依赖项按预期工作,但 appcompat-v7 不工作。一切都使用 ViewPagerFragmentActivityRecyclerView。我已经尝试将依赖项添加到 fullCompile 但它也没有用。 Android Studio 没有解决依赖关系,导入不起作用,除了 ok okhttpexoplayer 等。

我试过Invalidate Cache/Restartclean ProjectResync gradle,都没有用。

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'
}

应用 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')
}

最佳答案

您必须在应用程序 gradle 中声明配置。当它与库相关时,配置未正确声明。尝试:

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")
}

详细解释

gradle android 插件对应用程序和库使用不同的实现,分别称为AppVariantLibraryVariant。有时,变体和构建类型在两种项目中的工作方式是不同的。在这种情况下,前段时间库总是在给定变体中以发布构建类型编译,这使得库项目不像应用程序那样灵活。

这就是为什么他们决定启用 publishNonDefault 选项并在 Android Studio 中为这种行为提供支持,这样您就可以在应用的不同构建中使用不同构建的库,但是您必须指定哪个构建使用哪个库。这就是让您显式声明 configurations 的原因。

Android 构建工具团队使用的约定名称是 {buildType}{flavor}TaskName,因此对于类路径配置,您必须使用相同的名称。

所有这些过程都有一个缺点,那就是如果您发布非默认依赖项,android 插件将确保在构建您的应用程序之前编译所有可能的库配置,因此构建时间可能会增加一点(取决于库大小)

关于android - 无法使用 productFlavors 在库中导入 android 支持 v4 或 v7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34268414/

相关文章:

gradle - 如何评估Groovy中的字符串键,使其输出值?

java - openOptionsMenu() 不适用于 appcompat-v7 22.1.0 或更新版本

android - 支持库必须使用 compileSdkVersion 28 的确切版本规范错误

android - 未注册 fragment Root View 的边距(在 ViewPager 中使用时)

java - 如何修复类 android.support.v7.internal.app.WindowDecorActionBar 的构建路径

android - Retrofit 2 + RxJava 重试时

安卓 : SEND_SMS Permission not listed for tablet

android - 为 Android 构建 react-native-firebase 时的依赖冲突(多个 dex 文件定义 Lcom/google/firebase/iid/zzb$1)

android - 如何修复 “ERROR: Unable to resolve dependency for ':app @ debug/compileClasspath':。” Android错误

android - 自定义选项卡支持库