Search code examples
androidsdkandroid-studioandroid-5.0-lollipop

Support vs "real" libraries and "classes could not be found"


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

enter image description here

  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 can't I just use RecyclerView directly, without using the support...?

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

enter image description here

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

enter image description here

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

enter image description here

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>

Solution

  • 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:

    Libraries Screenshot

    Dependencies Screenshot

    or via your app module's build.gradle:

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