编译代码时出现此错误:
<!-- language: lang-none -->
Program type already present:
com.google.android.material.internal.package-info
Message{kind=ERROR, text=Program type already present:
com.google.android.material.internal.package-info, sources=
[Unknown source file], tool name=Optional.of(D8)}
当我搜索到类似的错误时,它建议添加
configurations {all*.exclude group: 'com.android.support', module: 'support-v13'}
或将这些行添加到'gradle.properties'文件中
android.useAndroidX = true
android.enableJetifier = false
但这些解决方案都不适合我,这是我的gradle文件
app
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.agh.yaomi"
minSdkVersion 17
targetSdkVersion 28
multiDexEnabled true
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
configurations {
all*.exclude group: 'com.android.support', module: 'support-v13'
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// added meta data in manifiest to resolve
//suppport version conflict
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:support-annotations:28.0.0-beta01'
implementation "org.jetbrains.anko:anko:0.10.4"
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
implementation 'com.google.android.material:material:1.0.0-beta01'
implementation 'com.android.support:design:28.0.0-alpha3'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:support-media-compat:28.0.0-alpha3'
// added to solve merge dex error
implementation 'com.android.support:multidex:1.0.3'
implementation ('com.github.omadahealth:lollipin:2.1.0@aar') {
transitive = true
}
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'
}
项目
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.21'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven{ url "https://github.com/omadahealth/omada-nexus/raw/master/release" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
有什么建议?
搜索了3个小时后我解决了它:
问题是:当添加material:1.0.0-beta01
依赖项,并使用支持库与任何版本,所以你得到这个错误。
解决方案:
1-你应该使用所有androidx
依赖项。
所以这些线
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:support-annotations:28.0.0-beta01'
implementation "org.jetbrains.anko:anko:0.10.4"
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
implementation 'com.google.android.material:material:1.0.0-beta01'
implementation 'com.android.support:design:28.0.0-alpha3'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:support-media-compat:28.0.0-alpha3'
将它们改为:
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.annotation:annotation:1.0.0-alpha1'
implementation "org.jetbrains.anko:anko:0.10.4"
implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
implementation 'com.google.android.material:material:1.0.0-beta01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
implementation 'androidx.media:media:1.0.0-alpha1'
我们删除了design:28.0.0-alpha3
,因为它包含在material:1.0.0-beta01
2-将这些行添加到gradle.properties
文件中
android.useAndroidX = true
android.enableJetifier = false
如this answer所述。
3-最后一步你可能会在MainActivity
中出错,因为你仍然从支持库中导入AppCompatActivity
,所以你应该删除旧的导入并从androidx
包中再次导入它,因为这个
import androidx.appcompat.app.AppCompatActivity;