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

时间:2017-03-22 06:47:06

标签: android android-studio gradle

在我的应用程序中,我有一个模块(basiclib是一个库)。当我在库build.I中设置'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'
}

1 个答案:

答案 0 :(得分:1)

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

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

publishNonDefault true

在您图书馆的build.gradle文件的Android部分。

然后你必须为主模块添加不同的风格,它们可以是空声明。在build.gradle的依赖关系部分中,模块如下:

flavor1Compile project(path: ':yourlib', configuration: 'flavor1Release')
flavor2Compile project(path: ':yourlib', configuration: 'flavor2Release')