Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Xamarin.Android.Build.Tasks] Use Google Manifest Merger. (#3918)
Use the Google Manifest Merger tooling to merge library `AndroidManifest.xml` files into the App's `AndroidManifest.xml`. This should allow developers to override and remove some unwanted permissions which are currently automatically imported from some of the support libraries. Documentation on how to use the new manifest merger tooling is at: https://developer.android.com/studio/build/manifest-merge Because of the limitations in Command line argument lengths under Windows we have to build our own entry point for the manifest merger tool. Our entry point takes a single "response" file as an input, with each argument needs to be on a separate line for example: --main AndroidManifest.xml --libs obj\Debug\lp\0\ji\AndroidManifest.xml:obj\Debug\lp\2\ji\AndroidManifest.xml --out android\AndroidManifest.xml We have to put each argument on its own line because of the way the built in Manifest Merger Argument parser works.
- Loading branch information
1 parent
aa05045
commit 2c6f5cd
Showing
20 changed files
with
350 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/Xamarin.Android.Build.Tasks/Tasks/ManifestMerger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Xml.Linq; | ||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Utilities; | ||
|
||
namespace Xamarin.Android.Tasks | ||
{ | ||
public class ManifestMerger : JavaToolTask | ||
{ | ||
public override string TaskPrefix => "AMM"; | ||
|
||
public override string DefaultErrorCode => $"{TaskPrefix}0000"; | ||
|
||
[Required] | ||
public string ManifestMergerJarPath { get; set; } | ||
|
||
[Required] | ||
public string AndroidManifest { get; set; } | ||
|
||
[Required] | ||
public string OutputManifestFile { get; set; } | ||
|
||
public string [] LibraryManifestFiles { get; set; } | ||
|
||
public string [] ManifestPlaceholders { get; set; } | ||
|
||
string tempFile; | ||
string responseFile; | ||
|
||
public override bool Execute () | ||
{ | ||
tempFile = OutputManifestFile + ".tmp"; | ||
responseFile = Path.Combine (Path.GetDirectoryName (OutputManifestFile), "manifestmerger.rsp"); | ||
try { | ||
bool result = base.Execute (); | ||
if (!result) | ||
return result; | ||
var m = new ManifestDocument (tempFile, Log); | ||
using (var ms = new MemoryStream ()) { | ||
m.Save (ms); | ||
MonoAndroidHelper.CopyIfStreamChanged (ms, OutputManifestFile); | ||
return result; | ||
} | ||
} finally { | ||
if (File.Exists (tempFile)) | ||
File.Delete (tempFile); | ||
if (File.Exists (responseFile)) | ||
File.Delete (responseFile); | ||
} | ||
} | ||
|
||
protected override string GenerateCommandLineCommands () | ||
{ | ||
string cmd = GetCommandLineBuilder ().ToString (); | ||
Log.LogDebugMessage (cmd); | ||
return cmd; | ||
} | ||
|
||
protected virtual CommandLineBuilder GetCommandLineBuilder () | ||
{ | ||
var cmd = new CommandLineBuilder (); | ||
|
||
if (!string.IsNullOrEmpty (JavaOptions)) { | ||
cmd.AppendSwitch (JavaOptions); | ||
} | ||
cmd.AppendSwitchIfNotNull ("-Xmx", JavaMaximumHeapSize); | ||
cmd.AppendSwitchIfNotNull ("-cp ", $"{ManifestMergerJarPath}"); | ||
cmd.AppendSwitch ("com.xamarin.manifestmerger.Main"); | ||
StringBuilder sb = new StringBuilder (); | ||
sb.AppendLine ("--main"); | ||
sb.AppendLine (AndroidManifest); | ||
if (LibraryManifestFiles != null) { | ||
sb.AppendLine ("--libs"); | ||
sb.AppendLine ($"{string.Join ($"{Path.PathSeparator}", LibraryManifestFiles)}"); | ||
} | ||
if (ManifestPlaceholders != null) { | ||
foreach (var entry in ManifestPlaceholders.Select (e => e.Split (new char [] { '=' }, 2, StringSplitOptions.None))) { | ||
if (entry.Length == 2) { | ||
sb.AppendLine ("--placeholder"); | ||
sb.AppendLine ($"{entry [0]}={entry [1]}"); | ||
} else | ||
Log.LogWarning ("Invalid application placeholders (AndroidApplicationPlaceholders) value. Use 'key1=value1;key2=value2, ...' format. The specified value was: " + ManifestPlaceholders); | ||
} | ||
} | ||
sb.AppendLine ("--out"); | ||
sb.AppendLine (tempFile); | ||
File.WriteAllText (responseFile, sb.ToString ()); | ||
cmd.AppendFileNameIfNotNull (responseFile); | ||
return cmd; | ||
} | ||
|
||
protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance) | ||
{ | ||
if (ExitCode != 0) | ||
Log.LogCodedError (DefaultErrorCode, singleLine); | ||
base.LogEventsFromTextOutput (singleLine, messageImportance); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 9 additions & 4 deletions
13
...Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/Base/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="${PACKAGENAME}"> | ||
<uses-sdk /> | ||
<application android:label="${PROJECT_NAME}"> | ||
</application> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:versionCode="1" | ||
android:versionName="1.0" | ||
package="${PACKAGENAME}" | ||
> | ||
<uses-sdk /> | ||
<application android:label="${PROJECT_NAME}"> | ||
</application> | ||
</manifest> |
Oops, something went wrong.