报错:android.support.v7.widget.Toolbar未找到,附App级build.gradle代码
android.support.v7.widget.Toolbar Not Found Error Hey there, let's break down why you're hitting this error and how to fix it. The core issues here are mixed AndroidX/Support Library dependencies and version mismatches across your libraries—let's tackle them one by one.
Root Causes
- Mixed AndroidX and Support Libraries: You're using
androidx.constraintlayout:constraintlayout:1.1.3(an AndroidX library) alongside old Support Library dependencies likecom.android.support:appcompat-v7. AndroidX and the old Support Library are incompatible, which leads to missing class errors. - Inconsistent Support Library Versions: Your
design/support-v4dependencies use28.0.0-rc01, whileappcompat-v7uses28.0.3. Mismatched versions cause class conflicts or missing implementations. - Firebase UI Version Mismatch:
firebase-ui-database:3.2.2isn't compatible with Support Library 28, which adds another layer of dependency conflict.
Solution 1: Migrate to AndroidX (Recommended)
The old Support Library is deprecated, so migrating to AndroidX is the long-term fix. Here's how to adjust your build.gradle (app-level):
Step 1: Enable AndroidX in gradle.properties
Add these lines to your project-level gradle.properties file:
android.useAndroidX=true android.enableJetifier=true
Jetifier automatically converts old Support Library dependencies to AndroidX, so you don't have to rewrite everything.
Step 2: Update Your App-Level build.gradle
Replace your dependencies with AndroidX equivalents, and use Firebase BOM to manage Firebase versions (avoids mismatches):
apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' android { compileSdkVersion 33 // Upgrade to a stable modern version buildToolsVersion "33.0.2" defaultConfig { applicationId "com.example.sscmessenger" minSdkVersion 19 targetSdkVersion 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) // AndroidX core libraries implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.9.0' // Replaces support:design implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' // Firebase BOM manages all Firebase dependency versions implementation platform('com.google.firebase:firebase-bom:32.4.0') implementation 'com.google.firebase:firebase-auth' implementation 'com.google.firebase:firebase-database' // Firebase UI compatible with AndroidX implementation 'com.firebaseui:firebase-ui-database:8.0.0' testImplementation 'junit:junit:4.13.2' }
Step 3: Update Your Layout XML
If your layout uses android.support.v7.widget.Toolbar, replace it with the AndroidX version:
<androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary"/>
Solution 2: Stick to Support Libraries (Not Recommended)
If you can't migrate right now, fix the version mismatches and remove AndroidX dependencies:
Updated App-Level build.gradle Dependencies
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) // 统一所有Support Library版本到稳定的28.0.0 implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support:design:28.0.0' implementation 'com.android.support:support-v4:28.0.0' // 替换AndroidX约束布局为Support版本 implementation 'com.android.support.constraint:constraint-layout:1.1.3' // Firebase UI compatible with Support Library 28 implementation 'com.firebaseui:firebase-ui-database:4.3.2' // Firebase versions compatible with Support Library 28 implementation 'com.google.firebase:firebase-auth:16.2.1' implementation 'com.google.firebase:firebase-database:16.1.0' testImplementation 'junit:junit:4.12' }
After making either set of changes, clean and rebuild your project (Build > Clean Project then Build > Rebuild Project in Android Studio) to apply the fixes.
内容的提问来源于stack exchange,提问作者sai charan

