升级Android Studio与Gradle后Snackbar显示InflateException问题求助
Hey, I’ve dealt with this exact <merge> tag-related crash when updating Android Studio and Gradle versions before—total head-scratcher at first. Let’s walk through why this is happening and how to fix it:
Why This Happens
The crash stems from Snackbar failing to inflate its internal layout properly. Common triggers include:
- Mismatched versions between your Support Design library and other Support libraries (like AppCompat, Support-v4)
- Stale build cache leftover from the upgrade
- Custom Snackbar layouts that misuse the
<merge>tag
Step-by-Step Solutions
1. Unify All Support Library Versions
You mentioned using com.android.support:design:27.1.1, but make sure every other Support library in your build.gradle uses the exact same version. Version mismatches are the #1 cause of this kind of layout inflation bug. Here’s how your dependencies should look:
// All support libraries share the same version implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support:design:27.1.1' implementation 'com.android.support:support-v4:27.1.1' // Add any other support libraries here with 27.1.1
2. Wipe Build Cache & Rebuild
Old cached build files from your previous setup can conflict with the new Gradle/Studio versions. Do a full clean:
- Go to
Build > Clean Projectin Android Studio - Then run
Build > Rebuild Project - For extra measure, manually delete the
.gradlefolder in your project root, plus thebuildfolders in your project and module directories. Then sync your project again.
3. Workaround: Use the Activity Root View
If the above doesn’t work immediately, try creating the Snackbar using your activity’s root view instead of the CoordinatorLayout (note: this loses CoordinatorLayout-specific behaviors like swipe-to-dismiss, but confirms if the issue is tied to your layout setup):
// Get the root view of the activity View rootView = getWindow().getDecorView().findViewById(android.R.id.content); Snackbar.make(rootView, "hello", Snackbar.LENGTH_LONG).show();
4. Check for Custom Snackbar Layouts
If you’ve created a custom layout for Snackbar, double-check that you aren’t using <merge> as the root tag. Snackbar’s internal inflation logic doesn’t support <merge> here—wrap your custom layout in a regular ViewGroup (like LinearLayout or FrameLayout) instead.
Bonus: Try Downgrading Gradle Temporarily
If none of the above works, it might be a compatibility quirk between Gradle 4.4 and Support Library 27.1.1. Try downgrading Gradle to version 4.1 or 4.2 (you can adjust this in your project’s gradle/wrapper/gradle-wrapper.properties file) to see if the crash goes away.
内容的提问来源于stack exchange,提问作者Farrokh Shahriari

