Support vs “real” libraries and “classes could not

2019-06-12 05:21发布

问题:

When I try to use the RecyclerView in a test project when Im about to try out Android 5.0 Lollipop, I see the following on Googles site:

1) What puzzles me is that they use the support thing instead of the "real" thing as provided in API 21. I was expecting to not use the support-package as I am running 5.0 on the device, have SDK 21 downloaded and installed. Why cant I just use RecyclerView directly, without using the support...?

2) Classes could not be found In Android Studio, I get this error:

"Element RecyclerView is not allowed here" without further information. Also, if I use the example as posted above, I get the same error:

And if I click "Fix Build Path", I see:

I always have these problems using JAVA and Android, but the error messages are unclear. The path to the sdk is correct (under SDK Location).

This is my gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "19.1.0"

    defaultConfig {
        applicationId "se.snapcode.lollipoptest"
        minSdkVersion 21
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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

and this is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="se.snapcode.lollipoptest" >

    <uses-sdk android:minSdkVersion="21"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

回答1:

Since I totally wrecked my first answer, let's try this again from the top...

What puzzles me is that they use the support thing instead of the "real" thing as provided in API 21.

There is no "real" thing. RecyclerView is only in the Android Support package, via the recyclerview-v7 library project/AAR.

This is akin to how Google packages ViewPager (support-v4 and support-v13), CardView (cardview-v7), the Leanback UI for Android TV (leanback-v17), etc.

The Android Support libraries is not just a set of backports. It does include various classes that are only available via those libraries. In addition to the above, there is NavUtils, FileProvider, and various other classes that are "native" to the Android Support libraries.

So, if you want RecyclerView, add recyclerview-v7 to your project, such as via Android Studio:

or via your app module's build.gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:recyclerview-v7:21.0.0'
}