android - 有人可以真正解释 Resource.Designer.cs 的工作原理吗?

标签 android xamarin xamarin.android aapt

我在项目构建期间经常遇到问题,其中生成了 Resource.Designer 但不再编译。我的搜索引擎显示我不是唯一遇到此问题的人,但我也没有找到任何可靠的“解决方案”来解决此问题。

例如,现在我有一个工作正常的项目,但如果我添加对 NuGet 库(我创建的)的引用,则应用程序将不再编译,因为 Resource.Designer.cs 中有大量错误.

在另一个项目中,我只是升级了 Xamarin Forms,现在我无法再编译,同样是由于 Resource.Designer.cs 中的大量错误。

当然,眼前的问题是“我如何修复这些项目中的错误”,但我真的想了解这里发生的根本问题。是什么导致 Resource.Designer 生成和重新生成?使用什么算法来决定将哪些项目放入该文件?为什么我们经常以生成的文件最终无法实际编译而告终?为什么它没有获得这些项目所需的引用?

我真的很想从根本上理解这一点,这样一旦我克服了当前让我头疼的事情,我就可以在将来再次出现时避免它(而且我肯定会再次出现) .

最佳答案

Resource.Designer.csR.java 同义在安卓系统中。哪个当然是由生成的常量引用的应用程序资源 Resource ID .

这些项目通常在您的 Xamarin.Android 中定义的 .csproj通过:

<AndroidResgenClass>Resource</AndroidResgenClass> (这可能已经过时了)

<AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile> (当前)

这是 Android Build 的一部分aapt 的过程工具将为项目中定义的每个资源生成相应的常量资源 ID(res/ - Android,Resources/ - Xamarin.Android)。然后将它们处理成二进制形式并嵌入到 .apk 中。 .因此 Resource.designer.cs成功后生成 aapt .

然后它进一步定义了一个 Resource在特定的 Resource Type:

http://code.google.com/android/reference/android/R.html

  • 动物
  • 动画师
  • 数组
  • 属性
  • bool
  • 颜色
  • 维度
  • 可绘制
  • 分数
  • 编号
  • 整数
  • 插值器
  • 布局
  • 菜单
  • 贴图
  • 复数
  • 原始
  • 字符串
  • 风格
  • 有风格
  • 过渡
  • xml

Since aapt is called in the Android Build tooling, it is highly recommended to not manually invoke it unless you understand the complete Android build process.

https://developer.android.com/studio/command-line/index.html

就“算法”而言,除了简单地映射 Resource ID 之外,我认为它并没有那么复杂。到上面定义的资源。在 Merging Resources 的意义上存在一些复杂性在一个项目中。 例如一个图书馆项目 -> 你的应用项目:

The build tools merge resources from a library module with those of a dependent app module. If a given resource ID is defined in both modules, the resource from the app is used.|

If conflicts occur between multiple AAR libraries, then the resource from the library listed first in the dependencies list (toward the top of the dependencies block) is used.

To avoid resource conflicts for common resource IDs, consider using a prefix or other consistent naming scheme that is unique to the module (or is unique across all project modules).

https://developer.android.com/studio/projects/android-library.html#Considerations

考虑到大多数人的问题 Resource.designer.cs ,它们通常来自理解实际第三方的角度 Resources来自以及如何支持它们。以下是我个人使用的一些技巧:

  1. 确保您的应用程序项目是针对最新的 API 版本编译的。通常这将是 <TargetFrameworkVersion> MSBuild 属性。我的一位同事有一篇关于这个因素经得起时间考验的精彩博文:

http://redth.codes/such-android-api-levels-much-confuse-wow/

  1. 找到Resource在哪里来自。它是否来自官方 NuGet 包? Resource 是什么时候引入到 Android 框架中的? (主要用于 Material Design 项目)。

例如,如果我有关于 colorPrimary 的错误消息, 我可能会检查它是在什么 API 中引入的:

Added in API level 21

https://developer.android.com/reference/android/R.attr.html#colorPrimary

因此我们现在知道我们至少需要 API 21+ 才能使用此项目。

  1. 深入研究您加载到项目中的依赖项。您可以使用像 dotPeek 这样的反编译器查看程序集并查看内容Resources它试图给你的项目。此外,您可以查看 cache各自的 .aar并查看它的 res/文件夹。这对于像 Android Support 这样的较大绑定(bind)非常有用。/Google Play Services那些。对于较小的项目,请查找 /res反编译后的字符串 .dll

例如让我们来看看com.android.support:appcompat-v7:24.2.1 :

首先我们需要找到我们机器上的缓存:

AppData\Local\Xamarin\Xamarin.Android.Support.v7.AppCompat\24.2.1\embedded\res (如果你在 OSX 上卡在这里,我之前写了一篇关于在这里找到这些路径的指南 - https://developer.xamarin.com/guides/android/troubleshooting/resolving-library-installation-errors/)

所以我们现在可以查看所有相应的 Resources这个图书馆正试图给我们。

  1. 最后一项是人们倾向于删除 Resource.designer.cs从他们的项目文件。 aapt之后过程完成,它将生成一个新的,否则将在 aapt 上失败.您可以确定该步骤是否通过(即检查主项目 Resources 文件夹中是否有新生成的 Resource.designer.cs 文件,以便您可以将其重新包含到您的项目中)。

关于android - 有人可以真正解释 Resource.Designer.cs 的工作原理吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40451758/

相关文章:

启用 Android WebView Javascript

android - 无法合并dex:库具有相同的包名称

安卓发光边框

android - 已签名的 apk 在我的设备上不起作用

android - 我可以将可见性绑定(bind)中的多个属性与 MvvmCross 结合使用吗?

c# - 机器人计时器

mvvm - 在MvvmCross中使用隐式模板选择对ListView进行分组

java - 可观察/观察者不工作?

c# - 通过 Xamarin Forms 在 XAML 中使用 LINQ

xamarin - 使用 Xamarin 读取 android 文件