Android添加GridLayout时依赖不一致问题求助
Hey there! Let's work through this dependency conflict issue together—since you're only 7 days into Android development, this kind of version mismatch is totally normal, and it's easy to fix once you know what's going on.
What's Causing the Problem?
All components in the Android Support Library need to use the exact same version to avoid compatibility issues. Right now, your project uses com.android.support:appcompat-v7:28.0.0, but somewhere in your dependencies (likely an indirect one), com.android.support:support-annotations:26.1.0 is being pulled in. When you try to add GridLayout with the + wildcard (which grabs the latest version), it creates a mismatch between these versions, leading to the error you see.
Step-by-Step Solution
We'll fix this by forcing all Support Library dependencies to use the same version (28.0.0, matching your compileSdkVersion) and explicitly adding GridLayout with the correct version.
- Open your module-level
app/build.gradlefile (the one you shared in your question). - Add a version enforcement rule right outside the
androidblock to make sure all Support Library dependencies use 28.0.0:configurations.all { resolutionStrategy.eachDependency { details -> def requested = details.requested if (requested.group == 'com.android.support') { if (!requested.name.startsWith("multidex")) { details.useVersion '28.0.0' } } } } - Update your
dependenciesblock to add GridLayout with the explicit 28.0.0 version (instead of using+):dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support:gridlayout-v7:28.0.0' // Add this line with exact version implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' } - Sync your project by clicking the "Sync Now" button that pops up in the top-right corner of Android Studio.
Why This Works
- The
configurations.allblock tells Gradle to override any conflicting versions of Support Library dependencies and force them to use 28.0.0. This eliminates the mismatch betweensupport-annotations:26.1.0andappcompat-v7:28.0.0. - Using an explicit version (28.0.0) for GridLayout instead of
+ensures it matches the rest of your Support Library components, preventing future version surprises.
After syncing, you should be able to use GridLayout without any compilation errors. Happy coding!
内容的提问来源于stack exchange,提问作者korra

