dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.92k stars 526 forks source link

Cannot merge Android Manifest elements from class library #8262

Open hansmbakker opened 1 year ago

hansmbakker commented 1 year ago

Android application type

.NET Android (net7.0-android, etc.)

Affected platform version

VS 2022 17.8 Pre 1, .NET 8 preview 7

Description

I want to create a Maui class library that uses content provider queries to get data from another application.

For this, it is required to add the queries element to the Android Manifest.

I want to automatically add the required element to the Android Manifest of the application project that consumes my library so that it works without manually modifying the Android manifest of that application.

I tried doing this using <AndroidManifestOverlay Include="somefile.xml" /> .

Steps to Reproduce

  1. Create .NET Maui App project
  2. Create .NET Maui class library and add an xml file QueriesPackageRegistration.xml with these contents:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android">
      <queries>
        <package android:name="com.companyname.someappid" />
      </queries>
    </manifest>
  3. Add <AndroidManifestOverlay Include="QueriesPackageRegistration.xml" /> to the class library project file
  4. Try adding <AndroidManifestMerger>manifestmerger.jar</AndroidManifestMerger> - no ideas whether this is still needed in xamarin-android 13?
  5. Add a dependency on the class library to the App project
  6. Deploy the app
  7. See that the queries element is not output to the generated Android Manifest.

Did you find any workaround?

No response

Relevant log output

No response

hansmbakker commented 1 year ago

I found somebody write on StackOverflow that

It appears that the manifestmerger is not able to merge the AndroidManifest.xml from multiple projects in the Android solution.

jonathanpeppers commented 1 year ago

Android class libraries do not contain an AndroidManifest.xml, as there is no mechanism to bundle and redistribute it. There isn't an MSBuild item group for the file type, etc.

However, what you could probably do for now as a workaround:

  1. Make an .aar file -- it is just a .zip file with the extension renamed.
  2. Put your AndroidManifest.xml in the root of the archive
  3. Put the .aar file in your project directory (next to .csproj) file

We do have a way for .aar files to flow from class libraries to application projects. They also happen to support AndroidManifest.xml files.

/cc @dellis1972 if you have other ideas here.

dellis1972 commented 1 year ago

The AndroidManifestOverlay will only work in the app project. The suggestion about packaging te file into an .aar "should" work. We do have a way of packaging android resources into an aar for a clas library I think, so maybe just flagging it with AndroidManifest in the class library will work. If it doesn't we should probably add that as an option.

hansmbakker commented 1 year ago

Thank you both for the ideas! I'll try it early next week and share the outcome here.

hansmbakker commented 1 year ago

@jonathanpeppers @dellis1972

Outcomes:

I verified both approaches by checking whether the contents of the AndroidManifest I wanted to merge ended up in ConsumingAppProjectDir\obj\Debug\net8.0-android\AndroidManifest.xml

Am I missing something?

Regarding

We do have a way for .aar files to flow from class libraries to application projects.

Should this be picked up automatically?

dellis1972 commented 1 year ago

Fix is up https://github.com/xamarin/xamarin-android/pull/8273.

hansmbakker commented 1 year ago

Wow! This must be one of the fastest fixes I've seen here! Thank you very much!

jpobst commented 3 months ago

Should be fixed in https://github.com/xamarin/xamarin-android/pull/8273.

jpobst commented 3 months ago

My bad, that PR isn't merged.

BernhardPollerspoeck commented 1 month ago

Is this to be expected to be worked on in the near future?

dellis1972 commented 1 month ago

The PR which fixes this issue was deemed to much of a change to make it into the .net 8 release. It was then overlooked for inclusion in .net 9. That said, I have rebased the PR now, but I doubt we will be able to get it into the .net 9 release this might have to wait until the next major release. The main reasons for the caution is the change in bahaviour from existing library projects. We have no data on how this will effect user if AndroidManifest.xml files from library projects start getting included into the build system.

BernhardPollerspoeck commented 1 month ago

so i read this correct, that it will take about 16 month until this is in production available? serious?

i apprechiate the effort and quick resonse though.