添加Google Material库后出现Program type already present编译错误求助
Hey there, I know this error can feel frustrating when you're just getting started—let's break down what's going on and fix it!
What's Causing the Error?
The Program type already present message means the same Java class is being included in your project multiple times. Here's the root cause in your case:
- The
com.google.android.material:material:1.0.0-alpha1library already includes all the functionality from the oldcom.android.support:designandcom.android.support:support-v4libraries (Material Components is the official successor to the Design Support Library). - By adding those two support libraries manually alongside Material, you're forcing duplicate class definitions into your build, which the D8 compiler can't resolve.
Step-by-Step Fix
Remove the duplicate dependencies from your
build.gradlefile. You don’t need these anymore because Material Components covers their functionality:implementation 'com.android.support:design:28.0.0-alpha1'implementation 'com.android.support:support-v4:28.0.0-alpha1'
Sync and rebuild your project: Click the "Sync Project with Gradle Files" button (the elephant icon) in Android Studio, then try building your project again.
Modified build.gradle Example
Here's what your dependencies block should look like after the fix:
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0-alpha1' implementation 'com.google.android.material:material:1.0.0-alpha1' implementation 'com.android.support.constraint:constraint-layout:1.1.0' 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' }
Bonus Tip for Future Conflicts
If you run into similar dependency issues later, you can check your full dependency tree to spot duplicates. Open the terminal in Android Studio and run:
./gradlew app:dependencies
This will show you every library your project depends on. If you find a duplicate, you can exclude it from a specific dependency like this:
implementation 'some.third-party:library:1.0' { exclude group: 'com.android.support', module: 'support-v4' }
内容的提问来源于stack exchange,提问作者Smart

