【问题标题】:Unable to add RecyclerView to API Level 19无法将 RecyclerView 添加到 API 级别 19
【发布时间】:2016-08-04 18:31:12
【问题描述】:

我正在尝试将 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 类中安装 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$1) 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

【问题讨论】:

  • 我不认为 Gradle 错误与 MultiDex 明确相关,因为添加 MultiDex 显然没有帮助
  • 是的,我将删除 multidex。但该项目仍未建立
  • 除了异常之外,您能否添加更多的 Gradle 输出?
  • 完整的gradle输出很长,这里是dl.dropboxusercontent.com/u/15447938/gradle_output.txt
  • 我指的是构建输出,而不是依赖项。也请先清洁。

标签: android android-recyclerview android-gradle-plugin android-support-library


【解决方案1】:

由于您要添加:

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

您使用的是 24.1.1 版本。
支持库 v24 需要编译 API 24。

android {
    compileSdkVersion 24
    //...

}

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

【讨论】:

    【解决方案2】:

    可以在 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 (*)
    

    【讨论】:

      猜你喜欢
      • 2014-01-09
      • 1970-01-01
      • 2019-04-28
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      • 2016-09-16
      • 1970-01-01
      • 2023-04-05
      相关资源
      最近更新 更多