皮棉报告“命名空间中的硬编码包”

时间:2018-11-14 12:16:46

标签: android xml lint hardcode

今天,当我想从我的项目中获取版本时,android studio会显示此错误消息:

android {
    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }
}

所以我打开皮棉的报告文件夹并看到此错误:

<issue
    id="ResAuto"
    severity="Fatal"
    message="In Gradle projects, always use `http://schemas.android.com/apk/res-auto` for custom attributes"
    category="Correctness"
    priority="9"
    summary="Hardcoded Package in Namespace"
    explanation="In Gradle projects, the actual package used in the final APK can vary; for example,you can add a `.debug` package suffix in one version and not the other. Therefore, you should **not** hardcode the application package in the resource; instead, use the special namespace `http://schemas.android.com/apk/res-auto` which will cause the tools to figure out the right namespace for the resource regardless of the actual package used during the build."
    errorLine1="    xmlns:app=&quot;http://schemas.android.com/apk/res/ir.app.appname&quot;>">"
    errorLine2="               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    quickfix="studio">
    <location
        file="C:\...\app\src\main\res\color\mybutton_backgroundcolor.xml"
        line="3"
        column="16"/>
</issue>

mybutton_backgroundcolor.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/ir.app.appname">

    <item android:state_enabled="true" app:state_read ="false" android:color="@color/backgrpund_button_success"/>

    <item android:state_enabled="false" app:state_read ="false" android:color="@color/gray_4"/>
    <item app:state_read ="true" android:color="@color/gray_4"/>

</selector>

xmlns:app =“ http://schemas.android.com/apk/res/ir.app.appname”> 行中的错误。

有人可以帮我解决这个问题吗?!

更新: 我将com.android.support库更新为版本28

后,会发生此错误

1 个答案:

答案 0 :(得分:0)

app命名空间应声明为xmlns:app="http://schemas.android.com/apk/res-auto"

如皮棉所说:

  

在Gradle项目中,最终APK中使用的实际包可能会有所不同;   因为您可以在一个版本中添加.debug软件包后缀,而不是   其他。因此,您应该对应用程序包进行硬编码   资源;而是使用特殊的名称空间   http://schemas.android.com/apk/res-auto将导致工具   找出资源的正确名称空间,无论   构建过程中使用的实际软件包。