Skip to content

Instantly share code, notes, and snippets.

@Guiorgy
Last active January 21, 2025 14:03
Show Gist options
  • Select an option

  • Save Guiorgy/75e9f9a0acb47fba96bc4eea537f0143 to your computer and use it in GitHub Desktop.

Select an option

Save Guiorgy/75e9f9a0acb47fba96bc4eea537f0143 to your computer and use it in GitHub Desktop.
Currently (11/1/19) Xamarin does not support merging manifest, or having different manifests for different build configurations. This is a workaround originally found by AlexanderMelchers at https://forums.xamarin.com/discussion/97461/android-manifest-file-merge-rules
  1. Download AXMLPrinter2.jar
  2. Build your solution and create an archive (.apk) by selecting Build->Archive...
  3. After archiving is done, open folder and find the archived *.apk, open the archive (for example using 7Zip) and extract/unzip AndroidManifest.xml
  4. Open any text editor and type: start cmd.exe /c "java -jar AXMLPrinter2.jar AndroidManifest.xml > AndroidManifest.plaintext.xml" and save it as a *.bat file in the same directory
  5. Run the *.bat file and check the resulting AndroidManifest.plaintext.xml file. This is the file we will be editing using a shell script
  6. Create the shell script you need. It should target obj\$configuration\android\AndroidManifest.xml for example: (Get-Content 'obj\Release\*\android\AndroidManifest.xml').replace('<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />', '') | Set-Content 'obj\Release\*\android\AndroidManifest.xml' the RemoveAndroidPermission.ps1 script above finds READ_EXTERNAL_STORAGE permission and removes them
  7. Save your scrip into your solution directory
  8. Open your project file *.csproj in any text editor. Scroll to the bottom, and a new custom tag to execute your script:
    <Target Name="BeforeCreateBaseApk" BeforeTargets="_CreateBaseApk">
      <Exec Command="powershell -noexit &quot;&amp; '$(SolutionDir)[YOUR SCRIPT NAME].ps1'" />
    </Target>
    In my case for exaple:
    <Target Name="BeforeCreateBaseApk" BeforeTargets="_CreateBaseApk" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
      <Exec Command="powershell -noexit &quot;&amp; '$(SolutionDir)RemoveAndroidPermissions.ps1'" />
    </Target>
  9. Save the changes to *.csproj and try rebuilding your solution. If it fails, you either did something wrong or your script corupts the AndroidManifest.xml
  10. After building succeeds, see that the changes to obj\$configuration\android\AndroidManifest.xml were made successfully
  11. Repeate steps 2-5, in other words make and archive, extract AndroidManifest.xml, use AXMLPrinter2.jar and see that the changes were successful
  12. You are done! ;)
@Guiorgy
Copy link
Copy Markdown
Author

Guiorgy commented Jan 15, 2019

In my case, for example, I had several build configurations, some of which required READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions, but others did not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment