在Android中实现Material Design时出现错误

5

我一直在阅读新的Material Design网站,但我在实现它时遇到了问题。

我目前正在开发的应用程序只是一个简单的测试应用程序,仅有一个按钮和一些文本视图。

问题出现在将implementation 'com.google.android.material:material:1.0.0-alpha1'放入gradle文件后。

这是我的build.gradle(app)文件:

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.user.firebasemessagingtest"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:27.1.1'
    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'

    implementation 'com.google.firebase:firebase-core:15.0.2'
    implementation 'com.google.firebase:firebase-messaging:15.0.2'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.2'
    implementation 'com.google.firebase:firebase-ml-vision:15.0.0'
    implementation 'com.google.android.material:material:1.0.0-alpha1'
}

apply plugin: 'com.google.gms.google-services'

我收到了4个错误提示,

error: resource android:attr/dialogCornerRadius not found.
error: resource android:attr/fontVariationSettings not found.
error: resource android:attr/ttcIndex not found.
error: failed linking references.

我已按照文档中所述,在项目Gradle文件中包含了Maven。

2个回答

2

试一下这个

更改compileSdkVersion和Support Libraries版本

compileSdkVersion  'android-P'
Support Libraries to '28.0.0-alpha1'

并使用

 api 'com.android.support:design:28.0.0-alpha1'

编辑

更新您的SDK平台更新 - Android P预览版(最新版本)

并将targetSdkVersion 'P'设置为目标SDK版本

然后清除-重建-运行您的项目

编辑2

apply plugin: 'com.android.application'

android {

    compileSdkVersion  'android-P'

    defaultConfig {
        applicationId "com.example.nilesh.testapp"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
    implementation 'com.android.support:support-v4:28.0.0-alpha1'
    compile 'com.android.support:design:28.0.0-alpha1'
    compile 'com.android.support:cardview-v7:28.0.0-alpha1'

    implementation 'com.android.support.constraint:constraint-layout:1.1.0'

    testImplementation 'junit:junit:4.12'

    implementation 'com.google.android.material:material:1.0.0-alpha1'


    implementation 'com.google.firebase:firebase-core:15.0.2'
    implementation 'com.google.firebase:firebase-messaging:15.0.2'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.2'
    implementation 'com.google.firebase:firebase-ml-vision:15.0.0'

    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
repositories {
    mavenCentral()
}

希望这可以帮助您。

1
这确实有所帮助,尽管它开始抛出其他错误。我已经尝试按照此处发布的答案进行操作,但仍然无法解决问题。 - Demonic218
1
仍然出现错误 - “程序类型已存在:android.support.v4.app.INotificationSideChannel” - Demonic218
1
android.useAndroidX = true android.enableJetifier = false 添加到 gradle.properties 中已经解决了构建错误,但是当我尝试在我的手机上运行它时,又出现了错误。 - Demonic218
1
抱歉,伙计,还是不行。在我的 SDK 管理器中,Android P 预览版显示为部分安装,如果我打开它,只有 Android SDK 平台 P 被安装,这样可以吗?还是所有的都需要下载? - Demonic218
1
我现在已经下载并安装了Android P sdk下的所有内容,但仍然遇到同样的问题。 - Demonic218
显示剩余2条评论

1
compileSdkVersion设置为'android-P'

将支持库与compileSdkVersion匹配。例如,使用implementation 'com.android.support:support-v4:28.0.0-alpha1'而不是implementation 'com.android.support:appcompat-v7:27.1.1'

implementation 'com.google.android.material:material:1.0.0-alpha1'替换为api 'com.android.support:design:28.0.0-alpha1'

应用程序的build.gradle应该类似于这样。
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 'android-P'

    defaultConfig {
        applicationId "com.wolfrevokcats.myapp"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 16
        versionName "2.3"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            debuggable false
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            debuggable false
            minifyEnabled false
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    api 'com.android.support:design:28.0.0-alpha1'
    implementation 'com.android.support:support-v4:28.0.0-alpha1'
    implementation ('com.someone.addons:library:1.6.1@aar'){
        exclude group: 'com.android.support'
        exclude module: 'appcompat-v7'
        exclude module: 'support-v4'
    }
    implementation 'com.google.android.gms:play-services-location:15.0.1'

    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'
}

由于正在使用'com.android.support:design:28.0.0-alpha1',我们的代码将不得不改用android.support.design库代替com.google.android.material库。这是一个使用新的底部应用栏小部件的示例。
<android.support.design.bottomappbar.BottomAppBar
        android:id="@+id/toolbar"
        style="@style/Widget.MaterialComponents.BottomAppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:fabAttached="true"
        app:fabAlignmentMode="center"/>

1
这实际上是我决定采取的路线,因为Material.io网站上的教程使用support:design而不是android.material。不过你能告诉我两者之间的区别吗? - Demonic218
Google正在推出新的软件包结构,以区分Android操作系统中包含和不包含的内容。那些不包含在内的内容将被归类到新的androidx.*软件包层级下。了解更多关于AndroidX - Nur Ilyas
com.android.support:design 库将被替换为 com.google.android.material:material:xxx 库,如 AndroidX 重构 页面所示。 - Nur Ilyas
android.support.* 版本将在 28.0.0 版本之后终止。 - Nur Ilyas
1
那么,既然android.support.design将被替换掉,现在实现它不是浪费时间吗? - Demonic218

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接