You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android添加GridLayout时依赖不一致问题求助

Fixing Android Support Library Version Conflict When Adding 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.

  1. Open your module-level app/build.gradle file (the one you shared in your question).
  2. Add a version enforcement rule right outside the android block 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'
                }
            }
        }
    }
    
  3. Update your dependencies block 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'
    }
    
  4. 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.all block tells Gradle to override any conflicting versions of Support Library dependencies and force them to use 28.0.0. This eliminates the mismatch between support-annotations:26.1.0 and appcompat-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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:39:58