Skip to content

Problem adding a drawable to a net7.0-android project #7600

Closed

Description

@gmck

Android application type

Android for .NET (net6.0-android, etc.)

Affected platform version

VS 2022 17.5.0 Prev 1

Description

I’ve successfully converted one of my test apps. I started on a second app this morning with the idea of trying to find the fastest possible way of converting an existing xamarin.android into a net7.0-android app by starting with the application template.
I start with what appeared to be reasonable with the previous project, but I was attempting to get there by first getting all the resources over and building them successfully before attempting to add any code.

So create an app with the template, and make sure it builds and runs.

Then I change the namespace by using the old one and adding “net7” to it, and add “net7” to the application name so that both apps can be side by side in the app drawer.

So basically I delete the contents of the Resources of the new template app and start adding the Resources from the xamarin.android app Resources folder. Eg. anim, drawable, menu, navigation etc.

If you have the .csprog file open you see a new ItemGroup and a Folder include for each folder e.g.

<ItemGroup>
	  <Folder Include="Resources\drawable\" />
	  <Folder Include="Resources\navigation\" />
	  <Folder Include="Resources\menu\" />
</ItemGroup>

Then when you start adding the actual resources here it changes to a new ItemGroup and the individual Folder item of the one you just added disappears.

<ItemGroup>
	  <None Remove="Resources\anim\slide_in_left.xml" />
	  <None Remove="Resources\anim\slide_in_right.xml" />
	  <None Remove="Resources\anim\slide_out_left.xml" />
	  <None Remove="Resources\anim\slide_out_right.xml" />
</ItemGroup>

However, if I add webp images from the other project’s drawable folder. The Folder Include For Resources\drawable disappears, but the <None Remove doesn’t get the 3 webp files that just appeared in the project. Any of the other xml drawables that were in the old project drawable folder are correctly added to the None Remove part, but the 3 webp files aren’t.

Consequently when after you added the Themes.xml file to the values folders that refer to those webp files you get a heap of errors. Can’t find etc.

I’m presuming this is not meant to work like this. But I can’t proceed any further with this conversion. Can you suggest a workaround?

The project I’m trying to convert is https://github.com/gmck/NavigationGraph7. Also the app I did convert successfully is. https://github.com/gmck/NavigationGraph2Net7

Steps to Reproduce

See above

Did you find any workaround?

No

Relevant log output

No response

Activity

  1. added
    Area: App+Library BuildIssues when building Library projects or Application projects.
    needs-triageIssues that need to be assigned.
    on Dec 1, 2022
  2. dellis1972 commented on Dec 1, 2022

    @dellis1972
    Contributor

    @gmck I assume you are using the IDE (VS) to do this? That part of the system is handled by a different team, you will need to use the Report Issue option in Visual Studio to open a Developer Community issue, since they do not use github to track issues. So if there are specific issues about the kinds of ItemGroup the IDE is adding (some of those None additions are a bit weird) they could use the feedback :).

    That said, it does look like the AutoImports is not including the webp files by default, so we can add those https://github.com/xamarin/xamarin-android/blob/main/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/Sdk/AutoImport.props#L28.

    Another option is to completely disable the auto import feature via EnableDefaultAndroidItems. Then you can completely control everything android related (drawables, etc, .jar/.aar etc).

  3. added this to the Under Consideration milestone on Dec 1, 2022
  4. gmck commented on Dec 1, 2022

    @gmck
    Author

    @dellis1972

    Yes, I am using the IDE. That is what I’ve always used. Obviously, I really don’t know much about the alternative of using MS Build manually.

    Ok from your explanation and following along with the fix that Jonathon just did I can see that it will get fixed. I’m just a bit surprised that webp wasn’t included in the first place. Android has been using them as a replacement for mipmaps for quite a while now, I presume that they went that way because they are smaller, so surely that is a good enough reason to use them.
    However, could you give me some indication of when the fix will be available? I can shelve the conversion of my app until then and just stick with classic for now.

    I would like to keep converting though because the first test app was reasonably straightforward. If you could explain (expand on) how to disable the auto import feature via EnableDefaultAndroidItems I might just be able to keep on converting.

    Is it as simple as just adding to the top of the csproj?
    <ItemGroup Condition=" '$(EnableDefaultAndroidItems)' == 'false' ">

    Looking at https://github.com/xamarin/xamarin-android/blob/main/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/Sdk/AutoImport.props#L28 it appears that I’d be removing way too much though.

    From past experiences with the Report Issue option in Visual Studio to open a Developer Community issue I’m most reluctant to ever go that route again because of my previous experience of reporting that a Resources/navigation/nav_graph.xml breaks the Android Designer within the IDE. That was an absolute waste of time when a bot replies after approx. 3 mths - sorry Buddy not enough votes – not going to get fixed. At least they are consistent, it is still broken in the test app I attached. That Developer Community thing is full of replies very similar to what I got. Just read another one now about a question re the weird <None Remove stuff/>. He was told to stuff off too.

    What they and the bot don’t seem to understand or appreciate is that it takes an amount of time to detail a particular bug that you hit and supply a sample that exactly demonstrates the problem, which proves it is a bug in their IDE.

    I don’t understand why you guys are so reluctant to report such issues to them yourselves. One would have to think that they would more likely to respond favourably to you guys than some unknown developer, especially if they come from some tiny little island like Australia at the bottom end of the world.

  5. gmck commented on Dec 1, 2022

    @gmck
    Author

    @dellis1972
    I deleted the themes.xml and styles.xml and the 3 webp files and cleaned and rebuilt successfully.

    I added <ItemGroup Condition=" '$(EnableDefaultAndroidItems)' == 'false' " />
    just above the ItemGroup none Remove
    Added the 3 webp files and the themes and styles again. Still get the same problem.

    Can you help and show me the correct way of doing it?

  6. dellis1972 commented on Dec 2, 2022

    @dellis1972
    Contributor

    @gmck

    as a work around you can add the following to your csproj manually.

    <ItemGroup>
       <AndroidResource Include="Resources\*\*.webp" />
    </ItemGroup>
    

    The only problem with this is I don't know if the IDE will mess that up. So the next best thing would be to add a new file
    Directory.Build.props right next to your csproj. Then add the following contents

    <Project>
      <ItemGroup>
        <AndroidResource Include="Resources\*\*.webp" />
      </ItemGroup>
    </Project>
    

    This file will then be auto included in your build, and the webp files will be added.

    You are right disabling the EnableDefaultAndroidItems property would turn allot of stuff off.
    If you did want to try that you would need to add the following

    <PropertyGroup>
      <EnableDefaultAndroidItems>False</EnableDefaultAndroidItems>
    </PropertyGroup>
    <ItemGroup Condition=" '$(EnableDefaultAndroidItems)' == 'false' ">
    <AndroidResource Include="$(MonoAndroidResourcePrefix)\*\*.xml" />
        <AndroidResource Include="$(MonoAndroidResourcePrefix)\*\*.axml" />
        <AndroidResource Include="$(MonoAndroidResourcePrefix)\*\*.png" />
        <AndroidResource Include="$(MonoAndroidResourcePrefix)\*\*.jpg" />
        <AndroidResource Include="$(MonoAndroidResourcePrefix)\*\*.gif" />
        <AndroidResource Include="$(MonoAndroidResourcePrefix)\font\*.ttf" />
        <AndroidResource Include="$(MonoAndroidResourcePrefix)\font\*.otf" />
        <AndroidResource Include="$(MonoAndroidResourcePrefix)\font\*.ttc" />
        <AndroidResource Include="$(MonoAndroidResourcePrefix)\raw\*" Exclude="$(MonoAndroidResourcePrefix)\raw\.*" />
        <!-- Default Asset file inclusion -->
        <AndroidAsset Include="$(MonoAndroidAssetsPrefix)\**\*" Exclude="$(MonoAndroidAssetsPrefix)\**\.*\**" />
        <!-- Default XPath transforms for bindings -->
        <TransformFile Include="Transforms*.xml" />
        <TransformFile Include="Transforms\**\*.xml" />
        <!-- Default Java or native libraries -->
        <AndroidLibrary       Include="**\*.jar" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder);**\*-source.jar;**\*-sources.jar;**\*-src.jar" />
        <AndroidLibrary       Include="**\*.aar" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
        <AndroidNativeLibrary Include="**\*.so"  Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
        <JavaSourceJar        Include="**\*-source.jar;**\*-sources.jar;**\*-src.jar"   Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
        <AndroidJavaSource    Include="**\*.java" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
        <ProguardConfiguration Include="**\proguard.cfg" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
        <ProguardConfiguration Include="**\proguard.txt" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
    </ItemGroup>
    

    Which is allot more complicated. I would go with the Directory.Build.props work around for now.
    I'm not sure which release we can get the fix into , we will try to get it into a .net 7 service release if there is one.

  7. gmck commented on Dec 3, 2022

    @gmck
    Author

    @dellis1972 ,

    Thanks for your help.

    Unfortunately, I cheated on this one just to get it done. Before I got your answer, I had seen that jpgs were good, so I just converted them to jpg, which fixed that issue.

    I've got a number of these test apps to convert that use the same webp(s) so I'll try your way on the next conversion and let you know if it works.

    Be great if it just works out to be

    <ItemGroup>
       <AndroidResource Include="Resources\*\*.webp" />
    </ItemGroup>
    

    In my opinion, it is a must to disable both nullable and implicitUsings when converting an old project. You'd drive yourself nuts with all the warnings, with them enabled. I really don't understand the use of implicitUsings if you are converting an existing AndroidX app, as it just produces warnings that are not applicable. Maybe they work well for other types of projects, but they just get in the way of an AndroidX project with the existing settings. I tried adding for instance
    using AndroidX.Fragment.App but it still didn't help - ambiguous warnings everywhere.

    Even when you completed the conversion, I don't see how it would then help in future coding with it enabled.

    I've got nothing against nullable, but way too many warnings as you are converting. Finish the conversion, get the app running as a release build and then go back and enable nullable and then fix them. If you haven't used nullable before, like me, I reckon you need to bookmark https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/nullable-warnings. Took me way too long to figure out.

    navigationView.Menu.FindItem(Resource.Id.home_fragment).SetChecked(navDestination.Id == Resource.Id.home_fragment);
    should be
    navigationView!.Menu!.FindItem(Resource.Id.home_fragment)!.SetChecked(navDestination.Id == Resource.Id.home_fragment);

    If the compiler is so smart why doesn't it just do it automatically? Or at least suggest it as you write.

    It was sort of ironic that the IDE all of a sudden recognised what a webp was when I deleted them. Wacked them into
    <None Remove no problem at all!!

    None Remove is still a bit of a mystery to me.

  8. gmck commented on Dec 13, 2022

    @gmck
    Author

    @dellis1972

    Tried it in another project and was able to bring in the webp files by adding your suggestion

    <ItemGroup>
    		<AndroidResource Include="Resources\*\*.webp" />
    </ItemGroup>
    

    Thanks

  9. ghost locked as resolved and limited conversation to collaborators on Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Area: App+Library BuildIssues when building Library projects or Application projects.

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions