Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error inflating class > com.google.android.material.switchmaterial.SwitchMaterial

So I use SwitchMaterial in one layout:

<com.google.android.material.switchmaterial.SwitchMaterial...

And Firebase Crashlytics spams me with the following errors for many users

Fatal Exception: java.lang.RuntimeException Unable to start activity ComponentInfo{...}: android.view.InflateException: Binary XML file line #324: Binary XML file line #324: Error inflating class com.google.android.material.switchmaterial.SwitchMaterial

Caused by android.content.res.Resources$NotFoundException File res/drawable/abc_switch_thumb_material.xml from drawable resource ID #0x7f080047

App theme:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">...

Material dep:

implementation "com.google.android.material:material:1.6.1"

What does it mean?

Update

I tried to use Switch from AndroidX AppCompat library instead and still the same issue with drawable:

Fatal Exception: java.lang.RuntimeException Unable to start activity ComponentInfo{****.ui.MainActivity}: android.view.InflateException: Binary XML file line #324: Binary XML file line #324: Error inflating class androidx.appcompat.widget.SwitchCompat

Caused by android.content.res.Resources$NotFoundException Drawable ***:drawable/abc_switch_thumb_material with resource ID #0x7f080047

This is so weird, it works fine for my smartphones though but it's effected 22 my users already

Yes, I have both the following options enabled for release builds

minifyEnabled true
shrinkResources true

But it worked fine all the time for different apps and I never had to add anything to a proguard file to keep drawable of some third party libraries (especially Google libraries) - abc_switch_thumb_material

Update 2

I just tried to download APK generated by Google Play Console from my AAB file

enter image description here

I checked apk\res\drawable\ and found that drawable is available

enter image description here

This is magic...

But this generated APK is universal and Google Play Consoles generates different APK based on type of device and its Android version, so what happens in this case I don't know

Update 3

I will try to use just Switch even if Android Studio shows the following warning:

Use SwitchCompat from AppCompat or SwitchMaterial from Material library

But at least I hope my app will stop getting crashed for some users because of it

p.s. devices for which the error happens (Firebase Crashlytics):

enter image description here

enter image description here

My build.gradle files of that app (if it's helpful in anyway and mb there are some conflicts between libraries)

Project build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext {
        kotlin_version = "1.7.10"
        nav_version = "2.5.1"
        hilt_version = "2.43.2"
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.2.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.gms:google-services:4.3.13"
        classpath "com.google.firebase:firebase-crashlytics-gradle:2.9.1"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" // for DataStore

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

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

App module build.gradle:

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "kotlin-kapt"
    id "com.google.gms.google-services"
    id "com.google.firebase.crashlytics"
    id "kotlin-parcelize"
    id "dagger.hilt.android.plugin"
    id "androidx.navigation.safeargs.kotlin"
    id "kotlinx-serialization" // for DataStore
}

android {
    compileSdkVersion 32
    defaultConfig {
        applicationId ***
        minSdkVersion 21
        targetSdkVersion 32
        multiDexEnabled true
        versionCode 18
        versionName "1.0.15"
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            manifestPlaceholders = [crashlyticsEnabled: false]
            signingConfig signingConfigs.release
        }
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
            manifestPlaceholders = [crashlyticsEnabled: true]
            signingConfig signingConfigs.release
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_11
    }
    buildFeatures {
        dataBinding = true
    }
}

dependencies {
    implementation "androidx.core:core-ktx:1.8.0"
    implementation "androidx.appcompat:appcompat:1.5.0"
    implementation "androidx.activity:activity-ktx:1.5.1"
    implementation "androidx.fragment:fragment-ktx:1.5.2"

    implementation "androidx.multidex:multidex:2.0.1"

    implementation "com.google.android.material:material:1.6.1"
    implementation "androidx.constraintlayout:constraintlayout:2.1.4"
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

    implementation "com.jakewharton.timber:timber:5.0.1"

    implementation platform("com.google.firebase:firebase-bom:30.3.2")
    implementation "com.google.firebase:firebase-analytics"
    implementation "com.google.firebase:firebase-crashlytics"

    implementation "com.google.firebase:firebase-ads:21.1.0"
    implementation "com.google.android.ump:user-messaging-platform:2.0.0"

    implementation "com.android.billingclient:billing-ktx:5.0.0"

    def lifecycle_version = "2.5.1"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"

    // Navigation Component
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"

    // Hilt
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_version"

    // OkHttp
    implementation(platform("com.squareup.okhttp3:okhttp-bom:4.9.0"))
    implementation("com.squareup.okhttp3:okhttp")
    implementation("com.squareup.okhttp3:logging-interceptor")

    // Retrofit
    def retrofit = "2.9.0"
    implementation "com.squareup.retrofit2:retrofit:$retrofit"
    implementation "com.squareup.retrofit2:converter-gson:$retrofit"

    // Glide
    def glide_version = "4.13.2"
    implementation "com.github.bumptech.glide:glide:$glide_version"
    kapt "com.github.bumptech.glide:compiler:$glide_version"

    // Pagination
    implementation "androidx.paging:paging-runtime-ktx:3.1.1"

    // Datastore
    implementation "androidx.datastore:datastore:1.0.0"
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-protobuf:1.4.0"

    implementation "org.jsoup:jsoup:1.15.2"

    testImplementation "junit:junit:4.13.2"
    androidTestImplementation "androidx.test.ext:junit:1.1.3"
    androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"
}
like image 801
user25 Avatar asked Nov 18 '25 10:11

user25


1 Answers

the crashes stopped when i switched to using <Switch>

both <SwitchCompat> and <SwitchMaterial> caused crashes in production

like image 86
Eric Avatar answered Nov 20 '25 01:11

Eric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!