有人可以真正解释Resource.Designer.cs的工作原理吗? [英] Can someone actually explain the workings of Resource.Designer.cs?

查看:180
本文介绍了有人可以真正解释Resource.Designer.cs的工作原理吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在生成Resource.Designer但无法编译的项目构建期间,我通常遇到问题.我的搜索引擎表明,我并不是唯一一个遇到此问题的人,但是我也没有找到任何可靠的解决方案".

I routinely have problems during project builds where Resource.Designer gets generated but will no longer compile. My search engine shows that I'm not the only one that has this problem, but I've also not found any reliable "fix" for when it happens.

例如,现在我有一个可以正常运行的项目,但是如果我添加对我创建的NuGet库的引用,则该应用将由于Resource.Designer.cs中的大量错误而不再编译.

For example, right now I have a project that works just fine, but if I add a reference to a NuGet library (that I created) then the app will no longer compile because of loads of errors in Resource.Designer.cs.

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

On another project, I simply upgraded Xamarin Forms and now I can no longer compile, again due to a load of errors in Resource.Designer.cs.

当然,当前的问题是我该如何解决这些项目中的错误",但实际上我想了解这里发生的根本情况.是什么导致Resource.Designer生成和重新生成?使用哪种算法来决定将哪些项目放入该文件?为什么为什么我们常常以生成文件然后最终无法编译的结果告终?为什么没有得到这些项目所需的参考?

Of course the immediate issue is "how do I fix the errors in these projects" but really I want to understand what is fundamentally going on here. What causes Resource.Designer to get generated and regenerated? What algorithm is used to decide what items get put into that file? Why do we so often end up with a file getting generated that then cannot actually compile? Why is it not getting the references it needs for those items?

我真的很想从根本上理解这一点,以便一旦我克服了当前让我头疼的问题,以后就可以避免它再次出现(而且我肯定会再次出现)

I'd really like to understand this at a fundamental level so that once I get past the current thing causing me headaches, I can avoid it in the future when it comes up again (and I most certainly will come up again).

推荐答案

Resource.Designer.cs在Android中与R.java同义.当然,哪个是由生成的常量Resource ID引用的应用程序资源.

Resource.Designer.cs is synonymous with R.java in Android. Which of course is an application's resources that are referred to by a generated constant Resource ID.

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

These items are usually defined in your Xamarin.Android's .csproj via:

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

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

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

This is part of the Android Build process in which the aapt tooling will generate respective constant resource IDs per each resource defined in your project(res/ - Android, Resources/ - Xamarin.Android). These are then processed into binary form and embedded into the .apk. Thus the Resource.designer.cs is generated after a successful aapt.

然后再进一步在特定的Resource Type:

It then goes a bit further to define a Resource in a specific Resource Type:

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

  • 动物
  • 动画师
  • 数组
  • attr
  • 布尔
  • 颜色
  • dimen
  • 可绘制
  • 分数
  • id
  • 整数
  • 插值器
  • 布局
  • 菜单
  • mipmap
  • 复数
  • 原始
  • 字符串
  • 样式
  • 可样式化
  • 过渡
  • xml
  • anim
  • animator
  • array
  • attr
  • bool
  • color
  • dimen
  • drawable
  • fraction
  • id
  • integer
  • interpolator
  • layout
  • menu
  • mipmap
  • plurals
  • raw
  • string
  • style
  • styleable
  • transition
  • xml

由于aapt是在Android构建工具中调用的,因此强烈建议不要手动调用它,除非您了解完整的Android构建过程.

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的意义上有一些复杂性. 例如图书馆项目->您的应用程序项目:

As far as an "Algorithm", I don't think it's really that complex other than simply mapping a Resource ID to a resource as defined above. There are some complexities in the sense of Merging Resources in a project. For example a library project -> your application project:

构建工具将库模块中的资源与从属应用程序模块中的资源合并.如果在两个模块中都定义了给定的资源ID,则将使用来自应用程序的资源.|

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.|

如果多个AAR库之间发生冲突,那么将使用依赖列表中最先列出的库中的资源(朝向依赖块的顶部).

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.

为避免常见资源ID的资源冲突,请考虑使用模块唯一的(或在所有项目模块中唯一的)前缀或其他一致的命名方案.

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的来源以及如何获得支持的角度出发的.这里是我个人使用的一些提示:

Given the majority of people's issues with Resource.designer.cs, they typically come from a point of view of understanding where actual third-party Resources come from and how they are supported. Here are some tips that I personally use:

  1. 确保根据最新的API版本编译您的Application项目.通常,这将是<TargetFrameworkVersion> MSBuild属性.我的一位同事写了一篇很棒的博客文章,该文章经受了时间的考验:
  1. Ensure your Application project is compiled against the latest API version. Typically this will be the <TargetFrameworkVersion> MSBuild property. One of my colleagues has an amazing blog post that stands the test of time about this factor:

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

  1. 查找Resource的来源.它来自官方的NuGet软件包吗?何时将Resource引入Android框架? (对于大多数材料设计项目很有用).
  1. Find where the Resource is coming from. Does it come from an official NuGet package? When was the Resource introduced into the Android framework? (Useful for mostly Material Design items).

例如,如果我收到有关colorPrimary的错误消息,我可以检查它引入了什么API:

For example, if I had an error message regarding colorPrimary, I might check what API it was introduced in:

在API级别21中添加

Added in API level 21

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

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

Thus we now know we require API 21+ at minimum to use this item.

  1. 深入研究要加载到项目中的依赖项.您可以使用像 dotPeek 这样的反编译器来查看程序集并查看其试图执行的Resources给你的项目.另外,您可以查看各个.aarcache并查看它的res/文件夹.这对于较大的绑定(如Android Support/Google Play Services的绑定)最有用.对于较小的项目,请在反编译的.dll
  2. 中查找/res字符串
  1. Take a deep dive into the dependencies you are loading into your project. You can use a decompiler like dotPeek to look through an assembly and see what Resources it's trying to give your project. Additionally you can look at the cache of respective .aar and view it's res/ folder. This is mostly useful for larger bindings like the Android Support / Google Play Services ones. For smaller projects, look for the /res string inside the decompiled .dll

例如,让我们看一下com.android.support:appcompat-v7:24.2.1 :

For example let's take a look at com.android.support:appcompat-v7:24.2.1:

首先,我们需要在我们的计算机上找到缓存:

First we need to find the cache on our machine:

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/)

AppData\Local\Xamarin\Xamarin.Android.Support.v7.AppCompat\24.2.1\embedded\res (If you get stuck here on OSX, I wrote a guide awhile back about finding these paths here - https://developer.xamarin.com/guides/android/troubleshooting/resolving-library-installation-errors/)

因此,我们现在可以浏览该库试图提供给我们的所有Resources.

So we can now look through all of the respective Resources this library is trying to give to us.

  1. 最后,最后一项是人们倾向于从其项目中删除Resource.designer.cs文件. aapt进程完成后,它将生成一个新的进程,否则将在aapt上失败.由您自己确定是否通过了该步骤(即检查主项目Resources文件夹中是否有新生成的Resource.designer.cs文件,以便可以将其重新包含到项目中).
  1. Finally the last item is that people tend to delete the Resource.designer.cs file from their project. After the aapt process is complete, it will generate a new one or it will fail on aapt. It is up to you to figure out whether that step passed or not (i.e. Check the main project Resources folder for the newly generated Resource.designer.cs file so you can re-include it into your project).

这篇关于有人可以真正解释Resource.Designer.cs的工作原理吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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