Can anyone really explain how Resource.Designer.cs works?

I usually have problems during project build when Resource.Designer is generated, but no longer compiled. My search engine shows that I'm not the only one who has this problem, but I also did not find a reliable "fix" when this happens.

For example, right now I have a project that works very well, but if I add a link to the NuGet library (which I created), the application will no longer compile due to many errors in Resource.Designer.cs.

In another project, I just updated Xamarin Forms, and now I can no longer compile it due to loading errors in Resource.Designer.cs.

Of course, the immediate problem is "how to fix errors in these projects," but in fact I want to understand what is happening mainly here. What makes Resource.Designer receive and regenerate? What algorithm is used to determine which elements fall into this file? Why do we end up so often with a file that then cannot be compiled? Why doesn't he get the links needed for these elements?

I would really like to understand this at a fundamental level, so that as soon as I pass the current thing that caused headaches, I can avoid this in the future when it appears again (and I will certainly come again).

+1
source share
1 answer

Resource.Designer.cs is a synonym for R.java in Android. Of course, these are application resources referenced by the generated Resource ID constant.

These elements are usually defined in Xamarin.Android .csproj through:

<AndroidResgenClass>Resource</AndroidResgenClass> (This may be deprecated)

or

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

This is part of the Android Build process in which the aapt tool will generate the corresponding constant resource identifiers for each resource defined in your project ( res/ - Android, Resources/ - Xamarin.Android). They are then processed into binary form and embedded in .apk . Thus, Resource.Designer.cs is created after aapt successful.

Next, the definition of Resource in a specific Resource Type: will be slightly defined Resource Type:

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

  • anime
  • animator
  • an array
  • atr
  • Bool
  • Color
  • DIMEN
  • range hood
  • fractions
  • ID
  • integer
  • Interpolator
  • location
  • menu
  • mipmap
  • plural
  • raw
  • Bite
  • style
  • styleable
  • transition
  • XML

Since aapt is called in the Android Build aapt , it is highly recommended that you do not manually call it if you do not understand the complete Android build process.

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

As for the "Algorithm", I do not think that this is really such a complex object, except for a simple mapping of the Resource ID to the resource, as defined above. The project has several difficulties in the sense of Merging Resources . For example, a library project -> your application project:

Assembly tools combine resources from the library module with elements of the application dependent module. If the specified resource identifier is defined in both modules, the resource from the application is used. |

If conflicts arise between several AAR libraries, then the resource from the library specified in the list of dependencies (at the top of the block of dependencies) is used.

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

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

Given that most people encounter the Resource.Designer.cs problem, they tend to arise in terms of understanding where the real third-party Resources comes from and how they are supported. Here are some tips that I personally use:

  • Make sure your application project is compiled with the latest version of the API. Usually this will be the <TargetFrameworkVersion> MSBuild property. One of my colleagues has a wonderful blog post that checks the time for this factor:

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

  1. Find where the Resource comes from. Does this come from the official NuGet package? When was the resource introduced into the framework of Android? (Useful mainly for Material Design materials).

For example, if I get a colorPrimary error colorPrimary , I can check which API it was introduced in:

Added to API Level 21

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

Thus, we now know that using this element requires a minimum of API 21+.

  1. Take a deep dive into the dependencies that you upload to your project. You can use a decompiler such as dotPeek to view the assembly and see what Resources trying to transfer your project to. In addition, you can look at the cache corresponding .aar and look at its res/ . This is mostly useful for large bindings like Android Support / Google Play Services . For small projects, find the line /res inside the decompiled .dll

For example, 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 (If you are stuck here on OSX, I again wrote a guide on finding these paths here - https://developer.xamarin.com / guides / android / troubleshooting / resolving-library-installation-errors / )

So, now we can see all the relevant Resources that this library is trying to provide us with.

  1. Finally, the last element is that people usually delete the Resource.Designer.cs file from their project. Upon completion of the aapt process aapt it will generate a new one, or it will fail on aapt . It is up to you to decide whether this step has passed or not (i.e. check the folder of the main Resources project for the newly created Resource.Designer.cs file so that you can re-include it in your project).
+4
source

Source: https://habr.com/ru/post/1259446/


All Articles