无法将 RecyclerView 添加到 API 级别 19

Unable to add RecyclerView to API Level 19

我正在尝试将 RecyclerView 添加到我继承的项目中,该项目是针对 API 19 编译的,不能更高。

在我像这样从支持库中添加 RecylerView 之后:

compile 'com.android.support:recyclerview-v7:+'

该项目不再生成,它开始给出 multidex 异常错误,如下所示:

Error:Execution failed for task ':app:transformClassesWithDexForDevDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_91\bin\java.exe'' finished with non-zero exit value 3

为了解决这个问题,我做了以下工作

  1. 像这样启用 multidex 支持multiDexEnabled true
  2. 添加 compile 'com.android.support:multidex:1.0.1'
  3. 像这样在 Applciation class 中安装 multidex:MultiDex.install(this);

所有这些都没有帮助。这是来自 运行 ./gradlew app:dependencies

的依赖关系图

根据下图,我需要做哪些更改才能重新构建项目?

compile - Classpath for compiling the main sources.
+--- com.android.support:multidex:1.0.1
+--- org.simpleframework:simple-xml:2.7.1
|    \--- stax:stax:1.2.0
+--- com.squareup.retrofit2:converter-simplexml:2.0.0-beta3
|    +--- com.squareup.retrofit2:retrofit:2.0.0-beta3
|    |    \--- com.squareup.okhttp3:okhttp:3.0.0-RC1
|    |         \--- com.squareup.okio:okio:1.6.0
|    \--- org.simpleframework:simple-xml:2.7.1 (*)
+--- com.crashlytics.sdk.android:crashlytics:2.5.5
|    +--- com.crashlytics.sdk.android:answers:1.3.6
|    |    \--- io.fabric.sdk.android:fabric:1.3.10
|    +--- com.crashlytics.sdk.android:crashlytics-core:2.3.8
|    |    +--- com.crashlytics.sdk.android:answers:1.3.6 (*)
|    |    \--- io.fabric.sdk.android:fabric:1.3.10
|    +--- io.fabric.sdk.android:fabric:1.3.10
|    \--- com.crashlytics.sdk.android:beta:1.1.4
+--- com.crashlytics.sdk.android:crashlytics-ndk:1.1.2
|    +--- io.fabric.sdk.android:fabric:1.3.7 -> 1.3.10
|    \--- com.crashlytics.sdk.android:crashlytics-core:2.3.5 -> 2.3.8 (*)
+--- commons-net:commons-net:3.3
+--- com.github.danysantiago:sendgrid-android:1
|    +--- org.apache.httpcomponents:httpmime:4.3.5
|    |    \--- org.apache.httpcomponents:httpclient:4.3.5
|    |         +--- org.apache.httpcomponents:httpcore:4.3.2
|    |         +--- commons-logging:commons-logging:1.1.3
|    |         \--- commons-codec:commons-codec:1.6
|    \--- org.apache.httpcomponents:httpclient-android:4.3.5.1
+--- com.amazonaws:aws-android-sdk-core:2.2.11
|    \--- com.google.code.gson:gson:2.2.4
+--- com.amazonaws:aws-android-sdk-s3:2.2.11
|    \--- com.amazonaws:aws-android-sdk-core:2.2.11 (*)
+--- com.google.code.ksoap2-android:ksoap2-android:3.6.0
|    \--- com.google.code.ksoap2-android:ksoap2-j2se:3.6.0
|         \--- com.google.code.ksoap2-android:ksoap2-base:3.6.0
|              +--- net.sourceforge.kxml:kxml:2.2.4
|              |    \--- org.xmlpull:xmlpull:1.1.3.3
|              \--- net.sourceforge.kobjects:kobjects-j2me:0.0-SNAPSHOT-20040926-2
+--- com.squareup.retrofit2:retrofit:2.0.0-beta3 (*)
\--- com.android.support:recyclerview-v7:+ -> 24.1.1
     +--- com.android.support:support-v4:24.1.1
     |    \--- com.android.support:support-annotations:24.1.1
     \--- com.android.support:support-annotations:24.1.1

这是完整的 Gradle 输出 https://dl.dropboxusercontent.com/u/15447938/gradle_output.txt

这是完整的构建输出

Information:Gradle tasks [:app:assembleDevDebug]
Warning:Dependency org.apache.httpcomponents:httpclient:4.3.5 is ignored for prodDebug as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
Warning:Dependency org.apache.httpcomponents:httpclient:4.3.5 is ignored for prodRelease as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
Warning:Dependency org.apache.httpcomponents:httpclient:4.3.5 is ignored for devDebug as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
Warning:Dependency org.apache.httpcomponents:httpclient:4.3.5 is ignored for devRelease as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
:app:preBuild UP-TO-DATE
:app:preDevDebugBuild UP-TO-DATE
:app:checkDevDebugManifest
:app:preDevReleaseBuild UP-TO-DATE
:app:preProdDebugBuild UP-TO-DATE
:app:preProdReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72411Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42411Library UP-TO-DATE
:app:prepareComCrashlyticsSdkAndroidAnswers136Library UP-TO-DATE
:app:prepareComCrashlyticsSdkAndroidBeta114Library UP-TO-DATE
:app:prepareComCrashlyticsSdkAndroidCrashlytics255Library UP-TO-DATE
:app:prepareComCrashlyticsSdkAndroidCrashlyticsCore238Library UP-TO-DATE
:app:prepareComCrashlyticsSdkAndroidCrashlyticsNdk112Library UP-TO-DATE
:app:prepareComGithubDanysantiagoSendgridAndroid1Library UP-TO-DATE
:app:prepareIoFabricSdkAndroidFabric1310Library UP-TO-DATE
:app:prepareDevDebugDependencies
:app:compileDevDebugAidl UP-TO-DATE
:app:compileDevDebugRenderscript UP-TO-DATE
:app:generateDevDebugBuildConfig UP-TO-DATE
:app:generateDevDebugAssets UP-TO-DATE
:app:mergeDevDebugAssets UP-TO-DATE
:app:processDevDebugManifest UP-TO-DATE
:app:fabricGenerateResourcesDevDebug
:app:generateDevDebugResValues UP-TO-DATE
:app:generateDevDebugResources
:app:mergeDevDebugResources
:app:processDevDebugResources
:app:generateDevDebugSources
:app:compileDevDebugJavaWithJavac
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:app:compileDevDebugNdk
:app:compileDevDebugSources
:app:transformClassesWithDexForDevDebug
Warning:Ignoring InnerClasses attribute for an anonymous inner class
(com.bea.xml.stream.util.CircularQueue) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.
UNEXPECTED TOP-LEVEL EXCEPTION:
Error:Execution failed for task ':app:transformClassesWithDexForDevDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_91\bin\java.exe'' finished with non-zero exit value 2
Information:BUILD FAILED
Information:Total time: 1 mins 10.137 secs
Information:1 error
Information:5 warnings
Information:See complete output in console

可以在 API 19 台设备上构建和 运行 这个没有问题,并且有很多 RecyclerView 视图。

随心所欲。如果它没有帮助,对不起。

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "com.androidstack.app"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
//        generatedDensities = []
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

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

    // This is handled for you by the 2.0+ Gradle Plugin
    aaptOptions {
        additionalParameters "--no-version-vectors"
    }
}

ext {
    // Variables to keep libraries consistent
    supportLibrary = '23+'

    // Support Libraries dependencies
    supportDependencies = [
            design           :         "com.android.support:design:${supportLibrary}",
            recyclerView     :         "com.android.support:recyclerview-v7:${supportLibrary}",
            appCompatV7      :         "com.android.support:appcompat-v7:${supportLibrary}",
    ]
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // compile supportDependencies.recyclerView

    // recommended instead - Includes themes, AppCompat libraries, and RecyclerView
    compile supportDependencies.design

}

依赖树

+--- com.android.support:design:23+ -> 23.4.0
     +--- com.android.support:recyclerview-v7:23.4.0
     |    +--- com.android.support:support-annotations:23.4.0
     |    \--- com.android.support:support-v4:23.4.0
     |         \--- com.android.support:support-annotations:23.4.0
     +--- com.android.support:appcompat-v7:23.4.0
     |    +--- com.android.support:animated-vector-drawable:23.4.0
     |    |    \--- com.android.support:support-vector-drawable:23.4.0
     |    |         \--- com.android.support:support-v4:23.4.0 (*)
     |    +--- com.android.support:support-v4:23.4.0 (*)
     |    \--- com.android.support:support-vector-drawable:23.4.0 (*)
     \--- com.android.support:support-v4:23.4.0 (*)

由于您要添加:

compile 'com.android.support:recyclerview-v7:+'

您正在使用 24.1.1 版本。
支持库 v24 需要 API 24 才能编译。

android {
    compileSdkVersion 24
    //...

}

您可以使用 RecyclerView 的 another version,但第一个版本是 21.0.0,需要 API21.