无法从 XML 中的 AAR 中找到资源标识符 [英] Can't find resource identifiers from AAR in XML

查看:31
本文介绍了无法从 XML 中的 AAR 中找到资源标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为应用程序开发了自定义首选项.它有两个自定义属性.我正在尝试将首选项移动到库项目以在其他项目中重用.我移动了 Java 文件以及相应的布局和 attr XML 文件.这是库项目中的 attr 文件:

I developed a custom Preference for an application. It has two custom attributes. I'm trying to move the Preference to a library project to reuse in other projects. I moved the Java file along with the corresponding layout and attr XML files. Here's the attr file in the library project:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="NumberPickerPreference" >
        <attr name="minValue" format="integer" />
        <attr name="maxValue" format="integer" />
    </declare-styleable>
</resources>

这是清单:

<manifest package="com.mydomain.androiduilibrary">
</manifest>

肯定是一个看起来很奇怪的清单.当我生成库项目时,Android Studio 放在清单中的唯一内容是 SDK 信息,然后它会生成一条警告,提示它已在 build.gradle 文件中指定,因此我删除了清单中的条目.

An odd-looking manifest for sure. When I generated the library project the only thing Android Studio put in the manifest was the SDK info, which then generated a warning about it already being specified in the build.gradle file, so I deleted the entry in the manifest.

我使用 bundleRelease 任务生成 AAR 文件并获得一个名为 androiduilibrary-release.aar 的文件.然后我转到需要使用此首选项的项目并选择新模块 | 导入 .JAR 或 .AAR 包",导航到 AAR,它现在在我的项目中作为名为 androiduilibrary-release 的模块.我在 Project Structure 对话框中创建了一个模块依赖项,现在可以在 Java 代码中引用 Preference.到目前为止,一切都很好.

I generate the AAR file using the bundleRelease task and get a file named androiduilibrary-release.aar. I then go to the project that needs to use this Preference and select "New Module | Import .JAR or .AAR Package", navigate to the AAR, and it's now in my project as a module entitled androiduilibrary-release. I make a module dependency in the Project Structure dialog, and can now reference the Preference in Java code. So far, so good.

但是,当我尝试在 XML 中引用首选项时,我的自定义属性出现以下错误:错误:(9) 在包‘com.mydomain.androiduilibrary’中找不到属性‘minValue’的资源标识符".这是 XML:

However, when I attempt to reference the Preference in XML, I get the following error on my custom attributes: "Error:(9) No resource identifier found for attribute 'minValue' in package 'com.mydomain.androiduilibrary'". Here's the XML:

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:title="@string/location_service_settings_preference_header_title"
    xmlns:custom="http://schemas.android.com/apk/res/com.mydomain.androiduilibrary">

<com.mydomain.androiduilibrary.NumberPickerPreference
    android:title="@string/update_interval_preference_title"
    android:key="@string/update_interval_preference_key"
    android:defaultValue="@integer/update_interval_preference_default"
    android:summary="@string/update_interval_preference_summary"
    custom:minValue="@integer/update_interval_preference_min"
    custom:maxValue="@integer/update_interval_preference_max"
    android:dialogMessage="@string/update_interval_preference_dialog_message"
    android:dialogTitle="@string/update_interval_preference_title" />

我可以查看 AAR 的内部并看到值填充在 R.txt 中:

I can look inside the AAR and see that the values are populating in R.txt:

int attr maxValue 0x7f010001
int attr minValue 0x7f010000

并且它的清单被正确创建:

And that its manifest is being created correctly:

<?xml version="1.0" encoding="UTF-8"?>

<manifest android:versionName="1.0" android:versionCode="1"
    xmlns:android="http://schemas.android.com/apk/res/android" package="com.mydomain.androiduilibrary">

    <uses-sdk android:targetSdkVersion="21" android:minSdkVersion="11"/>

</manifest>

我在调用自定义首选项时也收到警告:此处不允许使用 Element com.mydomain.androiduilibrary.NumberPickerPreference",所以我猜这也会是一个问题.

I also get a warning when calling out the custom preference: "Element com.mydomain.androiduilibrary.NumberPickerPreference is not allowed here" so I'm guessing that will be a problem too.

是否遗漏了允许我从 XML 中的 AAR 引用元素的步骤?我正在使用 Android Studio 1.0.2.提前致谢.

Is there a step I'm missing that will allow me to reference elements from the AAR in my XML? I'm using Android Studio 1.0.2. Thanks in advance.

推荐答案

this 帖子,在它讨论外部库的底部.本质上,在代码中引用 AAR 元素时,您使用 AAR 清单中指定的包,但在 XML 中引用 AAR 元素时,您引用项目的命名空间(而不是 AAR).

Solved it from this post, down at the bottom where it talks about external libraries. Essentially, when referencing AAR elements in code you do so using the package specified in the AAR's manifest, but when referencing AAR elements in XML you cite the namespace of the project (not the AAR).

这篇关于无法从 XML 中的 AAR 中找到资源标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆