Xamarin-android: Properly Merge Android Manifests

Created on 1 Aug 2019  路  16Comments  路  Source: xamarin/xamarin-android

We currently do not follow the rules of merging Android Manifest files, according to Google:
https://developer.android.com/studio/build/manifest-merge

However, Google does provide a tool on Maven called manifest merger:
https://dl.google.com:443/dl/android/maven2/com/android/tools/build/manifest-merger/26.4.2/manifest-merger-26.4.2.jar

@mattleibow built a proof of concept NuGet around this tool:
https://github.com/mattleibow/AndroidManifestMerger

But we should really take this one step further.

The problem reared its head around Google Play Services / Firebase where we were seeing AndroidManifest.xml files from multiple .aar's which contained <application><service android:name><metadata ... /></service></application> elements where the service name was the same in different files but each contain their own metadata elements. The ending result is supposed to be a single service element with all the metadata elements from each aar's manifest file, but instead we are getting multiple service elements. At runtime only one service element is considered so the other ones are lost along with their child elements.

Right now @mattleibow 's .targets are clearing out the library manifest group items, letting xamarin-android do its thing and after it merges its own manifest (with c# attributes and all that), it performs the merge using google's tool on all of the files.

The problem with this approach is that it overwrites the manifest file that xamarin-android is creating, so xamarin-android's tasks will always see the manifest has changed from what it thinks it should be and will always overwrite it, causing a lot more build tasks to happen than should be necessary.

Maybe we can have some sort of hook created where our own part of the manifest merging can be run and output the changes to the file in a way that subsequent builds are smart enough to know the xamarin-android manifest portion is unchanged even though we merged the library manifests afterwards with the xamarin-android produced one?

xamarin-android Build

All 16 comments

We have been wanting to split up GenerateJavaStubs for a while now, this seems like a good excuse to get that work started. Using the Google approved tooling seems like a better way than maintaining our own version. It will also allow our users to use the special manifest merging annotations and perhaps fix our long standing manifest ordering issue.

1335

1332

1336

We should also expose the values for versionCode and versionName from the final manifest as properties for users to use later during archiving or renaming of apks. Context https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1005105

Any update to this issue ?

We currently have multiple application elements in our AndroidManifest.xml which causes an error when publishing the app to the Android Store.
The element from an referencing library should be merged with our main element in our AndroidManifest.xml. Currently the two elements will be next to each other in the resulting AndroidManifest.xml.

I think this item might now be ready to be closed thanks to #3918 and #4022.

Those improvements are now included in the Xamarin.Android version 10.2 preview: https://docs.microsoft.com/xamarin/android/release-notes/10/10.2#improved-android-manifest-merging

The improvements are included on Windows in Visual Studio 2019 version 16.5 Preview 2 and higher. To try the Preview version that includes the fix, check for the latest updates in Visual Studio Preview.

The improvements are included on macOS in Visual Studio 2019 for Mac version 8.5 Preview 1 and higher. To try the Preview version that includes the fix, check for the latest updates on the Preview updater channel.


@dellis1972, do #3918 and #4022 cover everything that was planned for this issue? Is this ready to be closed?

We should also expose the values for versionCode and versionName from the final manifest as properties for users to use later during archiving or renaming of apks. Context https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1005105

On the whole this issue is done, we do have this outstanding item though ^.

Ah, got it. Sounds good. In that case, I'll hold off on adding this to the list of fixes for the Xamarin.Android 10.2 release notes.

@brendanzagaeski I tell you what, I'll add a new issue for that part. We can then close this one and put it in the release notes :)

Excellent. Adding release info to this one.


Release status update

A new Preview version has now been published that includes the new feature for this item. The feature is not yet included in a Release version. I will update this item again when a Release version is available that includes the feature.

Feature included in Xamarin.Android 10.2.0.84.

Feature included on Windows in Visual Studio 2019 version 16.5 Preview 2. To try the Preview version that includes the fix, check for the latest updates in Visual Studio Preview.

Feature included on macOS in Visual Studio 2019 for Mac version 8.5 Preview 1. To try the Preview version that includes the fix, check for the latest updates on the Preview updater channel.

To enable the new behavior, set the $(AndroidManifestMerger) MSBuild property to manifestmerger.jar in the .csproj file:

<PropertyGroup>
  <AndroidManifestMerger>manifestmerger.jar</AndroidManifestMerger>
</PropertyGroup>

See the release notes for additional details.

Release status update

A new Release version has now been published on Windows that includes the new feature for this item. The feature is not yet published in a Release version on macOS. I will update this item again when a Release version is available on macOS that includes the feature.

Feature included in Xamarin.Android 10.2.0.100.

Feature included on Windows in Visual Studio 2019 version 16.5. To get the new version that includes the feature, check for the latest updates or install the latest version from https://visualstudio.microsoft.com/downloads/.

(Feature also included on macOS in Visual Studio 2019 for Mac version 8.5 Preview 1 and higher. To try the Preview version that includes the fix, check for the latest updates on the Preview updater channel.)

To enable the new behavior, set the $(AndroidManifestMerger) MSBuild property to manifestmerger.jar in the .csproj file:

<PropertyGroup>
  <AndroidManifestMerger>manifestmerger.jar</AndroidManifestMerger>
</PropertyGroup>

See the release notes for additional details.

Release status update

A new Release version has now been published on macOS that includes the new feature for this item.

Feature included in Xamarin.Android 10.2.0.100.

Feature included on macOS in Visual Studio 2019 for Mac version 8.5. To get the new version that includes the feature, check for the latest updates on the Stable updater channel.

(Feature also included on Windows in Visual Studio 2019 version 16.5 and higher. To get the new version that includes the feature, check for the latest updates or install the latest version from https://visualstudio.microsoft.com/downloads/.)

To enable the new behavior, set the $(AndroidManifestMerger) MSBuild property to manifestmerger.jar in the .csproj file:

<PropertyGroup>
  <AndroidManifestMerger>manifestmerger.jar</AndroidManifestMerger>
</PropertyGroup>

See the release notes for additional details.

@brendanzagaeski @dellis1972 Is to possible to add multiple AndroidManifest.xml to single app and use manifestmerger.jar to merge them? I would like to have default, production AndroidManifest.xml that is always included and also AndroidManifestDEV.xml added to project only in DEV config. Second manifest would override properties in merge process like: label, package, version.

I know I could use AndroidManifestPlaceholders for most properties but it can't handle package as described here:
https://github.com/xamarin/xamarin-android/issues/3283

It looks like I had the foresight to add a ManifestOverlayFiles property to the ManifestMerger Task, but neglected to hook it up in the targets. This is the exact property you would need.

I'll have to add a new build action. Something like AndroidManifestOverlay. Which will then be passed in to the manifest merger as an overlay on top of the other files.

Then you might be able to do something like this in your csproj.

<ItemGroup>
   <AndridManifestOverlay Include="AndroidManifestDev.xml" Condition=" '$(Configuration)' != 'Release' " />
</ItemGroup>

I added a new issue to track this request https://github.com/xamarin/xamarin-android/issues/5312

@brendanzagaeski i have a xamarin.android library class project and xamarin.android main project but when I implement your suggestion, it is not merging for me. Manifest of library project should be merged into the main Manifest, i expect. Shoul i add this tag into both projects? how exactly should be working?

Just tested one more time by adding on both projects and it actually works but partly. see the side by side left one is left manifest is from android library and right one is main android project

image

And the merge results

image

i am missing here Vibrate, ACCESS_COARSE_LOCATION,ACCESS_FINE_LOCATION, ACCESS_NETWORK_STATE,ACCESS_WIFI_STATE permission...
Many of the others but surprisingly Internet, WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE merged from library into main manifest.

Was this page helpful?
0 / 5 - 0 ratings