Kotlin: failed to find style ‘cardView Style’ in current theme

Kotlin Programming

Question or issue of Kotlin Programming:

After updating support library version 27.1.0 Android Studio unable to render CardView. It shows error message as

failed to find style 'cardView Style' in current theme

But no error in compiling and no difference while seeing on the phone.

I have reverted the support library version to 27.0.2 and it’s rendering fine.

The issue with support library or Android studio? How to fix this?

App Level Gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'realm-android'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.tmmmt.tmmmt"
        minSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            buildConfigField 'String', 'FS_CLIENT_ID', FOURSQUARE_CLIENT_ID
            buildConfigField 'String', 'FS_CLIENT_SECRET', FOURSQUARE_CLIENT_SECRET
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false
            buildConfigField 'String', 'FS_CLIENT_ID', FOURSQUARE_CLIENT_ID
            buildConfigField 'String', 'FS_CLIENT_SECRET', FOURSQUARE_CLIENT_SECRET
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    flavorDimensions "default"

    productFlavors {
        dev {
            dimension "default"
            versionNameSuffix "-dev"
            buildConfigField 'String', 'BASE_URL', BASE_URL_DEV
            manifestPlaceholders = [GEO_API_KEY: GOOGLE_MAP_KEY_DEV]
        }

        beta {
            dimension "default"
            versionNameSuffix "-beta"
            buildConfigField 'String', 'BASE_URL', BASE_URL_BETA
            manifestPlaceholders = [GEO_API_KEY: GOOGLE_MAP_KEY_BETA]
        }

        live {
            dimension "default"
            buildConfigField 'String', 'BASE_URL', BASE_URL_LIVE

            manifestPlaceholders = [GEO_API_KEY: GOOGLE_MAP_KEY_LIVE]
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation "org.jetbrains.anko:anko-common:$anko_version"
    implementation "com.android.support:appcompat-v7:$support_version"
    implementation "com.android.support:support-v13:$support_version"
    implementation "com.android.support:design:$support_version"
    implementation "com.android.support:cardview-v7:$support_version"
    implementation "com.google.firebase:firebase-messaging:$play_version"
    implementation "com.google.android.gms:play-services-maps:$play_version"
    implementation "com.google.android.gms:play-services-location:$play_version"
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
    implementation 'com.squareup.okhttp3:okhttp:3.8.1'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'com.google.code.gson:gson:2.8.0'
    implementation 'com.wang.avi:library:2.1.3'
    implementation 'com.tmmmt.library:arrowtab:1.3'
    implementation 'com.github.stfalcon:chatkit:0.2.2'
    implementation 'com.amazonaws:aws-android-sdk-core:2.6.16'
    implementation 'com.amazonaws:aws-android-sdk-cognito:2.6.16'
    implementation 'com.amazonaws:aws-android-sdk-s3:2.6.16'
    implementation 'com.orhanobut:logger:2.1.1'
    implementation 'com.tmmmt.library:animations:0.6'
    implementation 'com.github.bumptech.glide:glide:4.6.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'
    compile('io.socket:socket.io-client:1.0.0') {
        exclude group: 'org.json', module: 'json'
    }
    /*Fort*/
    implementation project(':FortSDKv1.4.1')
    implementation 'com.victor:lib:1.0.1'
    implementation 'com.shamanland:fonticon:0.1.8'
    implementation('com.nispok:snackbar:2.11.0') {
        exclude group: 'com.google.android', module: 'support-v4'
    }
    implementation 'com.google.guava:guava:23.0-android'
    implementation 'org.bouncycastle:bcprov-jdk16:1.46'
    implementation 'commons-codec:commons-codec:1.10'
    /*Fort*/
}

apply plugin: 'com.google.gms.google-services'

Project Level Gradle:

buildscript {
    ext.kotlin_version = '1.2.30'
    ext.anko_version = '0.10.1'
    ext.support_version = '27.1.0'
    ext.play_version = '11.8.0'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "io.realm:realm-gradle-plugin:4.3.2"
        classpath 'com.google.gms:google-services:3.1.0'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url "https://mymavenrepo.com/repo/eDGOo6Dqr4f6uNA0HoWX/"
            credentials {
                username = 'myMavenRepo'
                password = 'tmmmt123'
            }
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Update:

There is no such error from Android Studio 3.2. The error was gone completely.

How to solve this issue?

Solution no. 1:

Checking the source of CardView one can see that the constructor

public CardView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    initialize(context, attrs, 0);
}

has been changed to

public CardView(@NonNull Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, R.attr.cardViewStyle);
}

And the new attribute cardViewStyle is defined in the library. I guess that the Android Studio preview is not including the value for this attribute from the library, for some reason. I’m not sure if it usually resolves custom attributes defined in libraries and this is a bug, or if this is intended.

Workaround 1

Resolve the attribute in your theme (maybe only in debug version), this way the error is gone. @style/CardView is already defined in the support library so you don’t need to create the style, just reference it.


                  

                  

Be sure to use AppTheme in the layout preview and clean build.

Workaround 2

Add the style with tools namespace so that it won’t affect your production code. The rendering error will be logged, but you will see the CardView anyway


                  

Good news

Android Studio 3.1 seems to be handling this correctly, no workaround needed (the error in the layout preview is still logged though).

On Android Studio 3.2 (currently in Canary channel) the error is gone.

Solution no. 2:

I also had the same problem, so I added

@style/CardView

in my styles.xml file. But it was showing a warning "The resource @style/CardView is marked as private in com.android.support:design".

So, after that I tried this one

@style/CardView.Light

and this worked for me. I am using Android Studio 3.1.4 and Gradle 4.4.

Solution no. 3:

updating the CardView from 27.1.1 to 28.0.0-alpha3 fixed the XML preview for me on AS 3.1.3.

implementation "com.android.support:cardview-v7:28.0.0-alpha3"

it still complains, but it renders the preview.

there's also a new androidx class now, which should be the same:

implementation "androidx.cardview:cardview:1.0.0"

when updating com.android.support:design to 28.0.0-alpha3, it hints for:

The resource @style/CardView is marked as private in com.android.support:design

downgrading back to API 27 with buildTools 27.0.3 & supportLibrary 27.1.1 also prevents the issues. might upgrade to 28.0.0 with Android Studio 3.2 then.

Solution no. 4:

In my case the reason of the error with CardView was because of the wrong listheader attribute of a child Spinner. Alongside the failed to find style 'cardView Style' in current theme error message I got Spinner adapter view type count must be 1, so the roots of the CardView failing were surprisingly in Spinner rendering failure.

Hope this helps!