简体   繁体   中英

Kotlin- xmlns:app=“http://schemas.android.com/apk/res-auto” is unavailable

I'm following the kotlin fundamentals code lab and in it it says that anything under API 21 will convert vector images to png images and to prevent that I need to add xmlns:app="http://schemas.android.com/apk/res-auto" to the activity_main.xml file and press Sync Now , and add vectorDrawables.useSupportLibrary = true to the defaultConfig { } in the .gradle(:app ) file. I followed everything but I kept getting an error:

/Users/Home/IdeaProjects/DiceRoller/app/src/main/res/layout/activity_main.xml:12

error: attribute android:srcCompat not found.

error: failed linking file resources.

I realized that the xmlns:app="http://schemas.android.com/apk/res-auto" line is grayed out

在此处输入图像描述

This is the gradle version I'm using:

dependencies {
    classpath 'com.android.tools.build:gradle:3.2.0-alpha18

What is causing this problem?

activity_main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center_vertical"
    tools:context=".MainActivity">

<android.support.v7.widget.AppCompatImageView 
           android:id="@+id/diceImageView"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center_horizontal"
           android:srcCompat="@drawable/empty_dice"
           tools:src="@drawable/dice_1">
</android.support.v7.widget.AppCompatImageView>

build:grade(:app)

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.diceroller"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.core:core-ktx:1.2.0-beta02'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}

build.gradle:

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0-alpha18'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

This is because the srcCompat attribute only exists in the app ( http://schemas.android.com/apk/res-auto ) namespace for the AppCompatImageView class (see the API documentation for more info), not the default android ( http://schemas.android.com/apk/res/android ) namespace.

To fix this, simply rename the android portion of android:srcCompat to app:srcCompat :

<android.support.v7.widget.AppCompatImageView 
           app:srcCompat="@drawable/empty_dice" />

(PS Consider self-closing the XML tag such that you don't have to write more code to close the XML element. See this question for more info)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM