如何将 MultiMaps gradle 添加到 android

How to add MultiMaps gradle to android

我想在我的项目中使用 MultiMaps,因为它允许存储重复的键值。但问题在于添加 gradle。这就是我添加的方式:

在我的子模块 gradle 中添加:

compile 'com.google.guava:guava:21.0'

然后我收到一条错误消息,要求添加这些行:

如果您在库子模块中使用 'java' gradle 插件,请添加

targetCompatibility = '1.7'
sourceCompatibility = '1.7'

到该子模块的 build.gradle 文件。

所以我像这样更改了 gradle 文件:

android {
  compileSdkVersion 25
  buildToolsVersion "23.0.2"
  defaultConfig {
    applicationId "com.example.golondon"
    minSdkVersion 16
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

dependencies {
compile 'com.google.guava:guava:21.0'

targetCompatibility = '1.7'
sourceCompatibility = '1.7'
}

但我仍然收到错误。我也试过:

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

然后它说要添加插孔选项并启用插孔选项我应该将我的 "buildToolsVersion" 更改为 24.0.1。

这会产生一大堆错误,例如: 来自 jar 文件的 Lambda 需要编译类路径上的接口,未知接口是 java.util.function.BiConsumer

对于 Android 您不能使用 Guava 21,因为它是第一个版本 运行 仅在 Java 1.8+ 上。见 Guava's README:

Requires JDK 1.8 or higher. If you need support for JDK 1.6 or Android, use 20.0 for now. In the next release (22.0) we will begin providing a backport for use on Android and lower JDK versions.

TLDR:使用 Guava 20.0。