android - 在我的应用程序中,我有一个模块(一个库)。当我在库构建中设置 'productFlavors' 时。应用程序不能依赖该模块

标签 android android-studio gradle

在我的应用程序中,我有一个模块(basiclib 是一个库)。当我在库构建中设置“productFlavors”时。我构建项目。应用程序有更多的导入错误。它无法导入 basiclib 包和 bascilib 依赖项包裹。

但是我删除了 basiclib 的“productFlavors”。一切都是正确的。

应用程序构建:

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.3'

    dataBinding {
        enabled true
    }


    defaultConfig {
        applicationId "xxxx"
        minSdkVersion 11
        targetSdkVersion 22
        versionCode 18000
        versionName "1.0.0"

    }


    lintOptions {
        abortOnError false
    }

    buildTypes {
        release {
            signingConfig signingConfigs.myConfig
            minifyEnabled true;
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            zipAlignEnabled true
        }
    }


    flavorDimensions "url", "theme"
    productFlavors {
        sit {
            buildConfigField 'String', 'API_URL', '"xxxx"'
            dimension "url"
        }
        uat {
            buildConfigField 'String', 'API_URL', '"xxxx"'
            dimension "url"
        }
        prd {
            buildConfigField 'String', 'API_URL', '"xxx"'
            dimension "url"
        }

        themewhite {
            buildConfigField 'String', 'theme', '"white"'
            dimension "theme"
        }


    }
    sourceSets {
        sit {
            java.srcDirs = ['src/sit_uat/java']
        }
        uat {
            java.srcDirs = ['src/sit_uat/java']
        }
        prd {
            java.srcDirs = ['src/prd/java']
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

}



dependencies {
    compile fileTree(include: '*.jar', dir: 'libs')
    sitCompile 'com.bugtags.library:bugtags-lib:latest.integration'
    prdCompile 'com.umeng.analytics:analytics:latest.integration'
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.0'
    compile 'com.jiechic.library:xUtils:2.6.14'
    compile 'com.loopj.android:android-async-http:1.4.7'
    compile 'org.apache.httpcomponents:httpcore:4.4.1'
    compile 'com.android.support:recyclerview-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
    // Did'n work.
    compile project(path: ':basiclib')
}

库构建(basiclib)
apply plugin: 'com.android.library'
apply plugin: 'realm-android'
apply plugin: 'me.tatarka.retrolambda'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    flavorDimensions "url"
    productFlavors {
        sit {
            buildConfigField 'String', 'API_URL', '"xxxxx"'
            dimension "url"
        }
        uat {
            buildConfigField 'String', 'API_URL', '"xxxxx"'
            dimension "url"
        }
        prd {
            buildConfigField 'String', 'API_URL', '"xxxxx"'
            dimension "url"
        }

    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}


dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'io.reactivex:rxjava:1.1.6'
    compile 'com.google.code.gson:gson:2.5'
    compile 'io.realm:realm-android-library:2.2.2'
    compile 'com.android.databinding:library:1.1'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.retrofit2:retrofit:2.2.0'
    compile 'com.android.support:appcompat-v7:23.2.1'
}

最佳答案

我遇到了同样的问题,经过大量搜索,我找到了以下文档条目:
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Referencing-a-Library

简而言之:默认情况下,在库模块中,只有默认配置(发布/无 flavor )对其他模块可见。要从主模块访问不同的 flavor ,请添加

publishNonDefault true

在你图书馆的 build.gradle 的 android 部分文件。

然后你还必须为主模块添加不同的风格,它们可以是空声明。在您的 build.gradle 的依赖项部分中引用模块如下:
flavor1Compile project(path: ':yourlib', configuration: 'flavor1Release')
flavor2Compile project(path: ':yourlib', configuration: 'flavor2Release')

关于android - 在我的应用程序中,我有一个模块(一个库)。当我在库构建中设置 'productFlavors' 时。应用程序不能依赖该模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42944523/

相关文章:

android - 如何在 Android Java 中序列化 Firestore TimeStamp?

Android TextView格式化多个单词

android - Messenger InstantGames - Web 请求在 Messenger 网页上运行良好,但在通过 Messenger 移动应用程序播放时失败

android - 找不到包 Android SDK 平台 25 的构建属性

java - 将 jar 文件从多项目 gradle 构建部署到目标目录

android-studio - 如何停止 Google Drive 创建 desktop.ini 文件

android - Gradle:我如何实际使用自定义依赖项配置或将外部依赖项交换为本地依赖项?

debugging - 错误 :Execution failed for task ':app:dexDebug'

android - 如何使用 Android Studio 在模块中添加 flavor ?

android - 重命名的 apk 无法通过 Android Studio 安装