Bug 51689 - Xamarin does not handle "${applicationId}" build variable
Summary: Xamarin does not handle "${applicationId}" build variable
Status: RESOLVED UPSTREAM
Alias: None
Product: Android
Classification: Xamarin
Component: Bindings ()
Version: 7.0 (C8)
Hardware: PC Windows
: Normal normal
Target Milestone: 15.5
Assignee: dean.ellis
URL:
: 56690 56915 ()
Depends on:
Blocks:
 
Reported: 2017-01-22 10:46 UTC by Mikalai Daronin
Modified: 2017-09-18 16:58 UTC (History)
14 users (show)

Tags: bb
Is this bug a regression?: ---
Last known good build:


Attachments
A reproducible example. (594.23 KB, application/x-zip-compressed)
2017-01-22 10:46 UTC, Mikalai Daronin
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED UPSTREAM

Description Mikalai Daronin 2017-01-22 10:46:18 UTC
Created attachment 19434 [details]
A reproducible example.

I've faced with a problem with AAR bindings. It seems there is a bug in Xamarin build process.

Originally described here: https://stackoverflow.com/questions/41551813/how-to-deal-with-applicationid-in-xamarin

Zendesk Belvedere library (belvedere-1.0.2.1.aar) contains a file provider in its manifest file:
    <provider
        android:name="com.zendesk.belvedere.BelvedereFileProvider"
        android:authorities="${applicationId}.belvedere.attachments"
        android:exported="false"
        android:grantUriPermissions="true" >
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/belvedere_attachment_storage" />
    </provider>

When Gradle is used as build tool, it puts this aar to the APK file and it replaces "${applicationId}.belvedere.attachments" with "com.your_package_name_here.belvedere.attachments" in the merged manifest file. It's fine:
    <provider 
        android:name="com.zendesk.belvedere.BelvedereFileProvider" 
        android:exported="false" 
        android:authorities="com.example.mylibrary.belvedere.attachments" 
        android:grantUriPermissions="true">
        <meta-data 
            android:name="android.support.FILE_PROVIDER_PATHS" 
            android:resource="@xml/belvedere_attachment_storage" />
    </provider>

However, Xamarin handles it differently. Here is what I found in the manifest of my final APK:
    <provider 
        android:name="com.zendesk.belvedere.BelvedereFileProvider" 
        android:exported="false" 
        android:authorities="dollar_openBracket_applicationId_closeBracket" 
        android:grantUriPermissions="true">
        <meta-data 
            android:name="android.support.FILE_PROVIDER_PATHS" 
            android:resource="@xml/belvedere_attachment_storage" />
    </provider>

I think Xamarin should replace "dollar_openBracket_applicationId" with "coom.mypackagename" as Gradle does it.

I've tried to create a minimal reproducible project. So, I created the "library.aar" file with a provider in the manifest:
    <provider
        android:name="com.example.mylibrary.library.MyDummyProvider"
        android:authorities="${applicationId}.mydummyauthority" >
    </provider>

In that case Xamarin processes it successfully - the APK manifest has:
    <manifest 
        ...
        package="com.example.xamarinbug" >
        ...
        <application 
            ...
            <provider 
                android:name="com.example.mylibrary.library.MyDummyProvider" 
                android:authorities="com.example.xamarinbug.mydummyauthority" />
        </application>
    </manifest>


The major difference between "belvedere-1.0.2.1" and "mylibrary.aar" is that belvedere lib contains "aapt/AndroidManifest.xml" file with:
    <provider
        android:name="com.zendesk.belvedere.BelvedereFileProvider"
        android:authorities="dollar_openBracket_applicationId_closeBracket"
        android:exported="false"
        android:grantUriPermissions="true" >
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/belvedere_attachment_storage" />
    </provider>

Gradle somehow handles/ignores that file, but it breaks Xamarin build process.

I've attached a reproducible solution and my build environment details.
Comment 1 Atsushi Eno 2017-01-24 13:12:05 UTC
So far it's not reproducible on master on my dev. environment (Ubuntu).
Comment 2 Ben Askren 2017-05-26 22:34:39 UTC
Atsushi Eno:

I too am struggling with this issue and it appears others are as well (https://stackoverflow.com/questions/41551813/how-to-deal-with-applicationid-in-xamarin?answertab=active#tab-top).  If you can email me, I would be very happy to provide you with my VisualStudio Mac solution that demonstrates this problem in all of its glory.
Comment 3 Ben Askren 2017-05-27 14:22:23 UTC
Steps to reproduce:

(1) Using environment w/ VisualStudio Mac (see below for details)
(2) Select "File / New Solution ...".  "New Project" dialog box should appear.
(3) In the left pane, select "Multiplatform / App".
(4) In the middle pane, select "Blank Forms App".
(5) Select [Next] button.  The second "New Project" dialog should appear.
(6) Enter "DroidBuildFail" for "App Name:" and "Organization Identifier:".
(7) Assure that "Android" "Target Platform" has been selected.
(8) Select "Use Portable Class Library" for "Shared Code:" setting.
(9) Select [Next].  Third "New Project" dialog should appear.
(10) Select [Create].  VisualStudio will create a new project. Wait for packages to be added successfully.
(11) In the Solution Pad, right click on the "DroidBuildFail.Droid" project and select "Set as Startup Project".
(12) In the Solution Pad, expand the "DroidBuildFail.Droid" project and double click on the "Packages" folder.  The "Add Packages" dialog should appear.
(13) Search for and select the "Xamarin.Facebook.Android" package.
(14) Click [Add Package] and wait for the package to be successfully added.
(15) In the Solution Pad, right click on the ".Droid" project and select "Rebuild DroidBuildFail.Droid". 
(16) In the Solution Pad, right click on the  "DroidBuildFail" solution and select "Display Options / Show All Files".
(17) In the Solution Pad, navigate to "DroidBuildFail.Droid/obj/Debug/android" and select the "AndroidManifest.xml" file there.
(18) Inspect lines 34 and 46.  Notice the value of the "android:authorities" attribute is "dollar_openBracket_applicationId_closeBracket".  It should be your app's applicationId.

Environment Details:

=== Visual Studio Community 2017 for Mac ===

Version 7.0.1 (build 24)
Installation UUID: c3c61da2-4d87-4c39-b8d3-9fbf7e6af430
Runtime:
	Mono 5.0.1.1 (2017-02/5077205) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 500010001

=== NuGet ===

Version: 4.0.0.2323

=== .NET Core ===

Runtime: /usr/local/share/dotnet/dotnet
SDK: /usr/local/share/dotnet/sdk/1.0.3/Sdks
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.0.1/lib/mono/msbuild/15.0/bin/Sdks

=== Xamarin.Profiler ===

Version: 1.5.4
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Apple Developer Tools ===

Xcode 8.3.2 (12175)
Build 8E2002

=== Xamarin.Mac ===

Version: 3.4.0.36 (Visual Studio Community)

=== Xamarin.Android ===

Version: 7.3.1.2 (Visual Studio Community)
Android SDK: /Users/ben/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.3    (API level 10)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
		5.1    (API level 22)
		6.0    (API level 23)
		7.0    (API level 24)
		7.1    (API level 25)

SDK Tools Version: 26.0.2
SDK Platform Tools Version: 25.0.6
SDK Build Tools Version: 25.0.3

Java SDK: /usr
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Xamarin.iOS ===

Version: 10.10.0.36 (Visual Studio Community)
Hash: d2270eec
Branch: d15-2
Build date: 2017-05-22 16:30:53-0400

=== Xamarin Inspector ===

Version: 1.2.2
Hash: b71b035
Branch: d15-1
Build date: Fri, 21 Apr 2017 17:57:12 GMT

=== Build Information ===

Release ID: 700010024
Git revision: 7ab1ca2ced6f584e56b7a0d4d321d00775cd95c9
Build date: 2017-05-19 05:44:51-04
Xamarin addins: 08d17158f3365beee5e60f67999e607cce4b3f93
Build lane: monodevelop-lion-d15-2

=== Operating System ===

Mac OS X 10.12.5
Darwin 16.6.0 Darwin Kernel Version 16.6.0
    Fri Apr 14 16:21:16 PDT 2017
    root:xnu-3789.60.24~6/RELEASE_X86_64 x86_64
Comment 4 Ben Askren 2017-06-09 17:02:56 UTC
Is this bug being assigned?  What is the status?
Comment 5 Jon Douglas [MSFT] 2017-06-23 15:56:52 UTC
*** Bug 56915 has been marked as a duplicate of this bug. ***
Comment 6 Jon Douglas [MSFT] 2017-06-23 16:14:12 UTC
I believe this issue is directly related to lack of support of advanced AndroidManifest.xml merging and the lack of handling the ${applicationId} variable within MSBuild. I have another outstanding issue regarding supporting more advanced features of AndroidManifest.xml merging:

https://bugzilla.xamarin.com/show_bug.cgi?id=52857

I am also not able to reproduce this bug using https://bugzilla.xamarin.com/attachment.cgi?id=19434

However I am able to reproduce this issue using https://bugzilla.xamarin.com/show_bug.cgi?id=51689#c3 which gives the following element:

    <provider android:name="com.facebook.internal.FacebookInitProvider" android:authorities="dollar_openBracket_applicationId_closeBracket" android:exported="false" />

Thus I am marking this bug as "Confirmed" as I can reproduce on the following:

Xamarin 4.5.0.486
Xamarin.Android SDK 7.3.1.2

As for a sample project, you can follow this list of steps:

1. Create a blank Android project
2. Add the Xamarin.Facebook.Android 4.22.0 package to your solution
3. Build the project and notice the "obj\Debug\android\AndroidManifest.xml file has the provider element above.
Comment 7 dean.ellis 2017-06-26 09:17:41 UTC
The PR might be related https://github.com/xamarin/xamarin-android/pull/647.

The "dollar_openBracket_applicationId_closeBracket" does not appear to be a x-a issue, but something to do with the wrapping of the component maybe?
Comment 8 dean.ellis 2017-06-26 09:18:51 UTC
@Redth 

Any ideas what might be causing the "dollar_openBracket_applicationId_closeBracket" ?
Comment 9 Mikalai Daronin 2017-06-30 20:06:57 UTC
This bug can be reproduced using original attachment: https://bugzilla.xamarin.com/attachment.cgi?id=19434

Steps:

1. Download it and extract.

2. Build APK using msbuild:
$ msbuild \
/p:AndroidSdkDirectory=/Users/user/Library/Android/sdk \
/p:Configuration=Debug \
/p:Platform="AnyCPU" \
/p:OutputPath="bin/Debug" \
/t:PackageForAndroid \
Belvedere.XamarinExample/Belvedere.XamarinExample.csproj

3. Open "Belvedere.XamarinExample/bin/Debug/com.example.belvedere_xamarinexample.apk" in ClassyShark decompiler, you will find the following lines in "AndroidManifest.xml":
...
    <provider
        name='com.zendesk.belvedere.BelvedereFileProvider'
        exported='false'
        authorities='dollar_openBracket_applicationId_closeBracket'
        grantUriPermissions='true'>
      <meta-data
          name='android.support.FILE_PROVIDER_PATHS'
          resource='@res/0x7F060000'>
      </meta-data>
    </provider>
...

I use macOS Sierra 10.12.5, mono 5.0.1.1, Xamarin.Android 7.3.1, and msbuild 15.2.0.0.
Comment 10 Cody Beyer (MSFT) 2017-07-12 22:04:32 UTC
Confirmed using the steps provided
Comment 11 Jon Dick 2017-07-13 19:07:38 UTC
I'm having a hard time deducing why this is happening, but I can repro it here with the given version of Facebook Android nuget (4.22.0).

Interestingly enough, I've updated Facebook Android to the 4.24.0 bindings and tested the nuget package from our internal feed as it goes through review, and it does NOT exhibit the same behaviour.

Perhaps this has something to do with bindings built with a specific version of Xamarin.Android?
Comment 12 dean.ellis 2017-07-14 10:53:52 UTC
I have found where the issue is. It seems that the manifest file in the .aar aapt directory is the one that contains the "dollar_openBracket_applicationId_closeBracket" problem. Unfortunately this seems to be a result of the build process to build the .aar file. 

I am investigating a way to post process/fix up this issue since the building of these .aar files are outside of our control (they are produced by google, Facebook etc).
Comment 13 Jon Dick 2017-07-14 12:02:39 UTC
Very odd.  the most recent version of the Facebook SDK does not do this.
Comment 14 dean.ellis 2017-08-15 22:08:57 UTC
*** Bug 56690 has been marked as a duplicate of this bug. ***
Comment 15 Troy O'Neal 2017-09-12 02:04:01 UTC
I'm hesitant to use the Xamarin.Facebook.Android nuget due to comments I've seen about this bug possibly causing failed installs.  What's the most recent update here?  Does the latest Facebook SDK fix this for good?  When is it safe to use Xamarin.Facebook.Android?  

If I don't see "dollar_openBracket_applicationId_closeBracket" in the final apk manifest can I assume that the built files are correct (at least with regards to this bug)?  Just looking for a bit of clarity on the situation.

Thanks.
Comment 16 dean.ellis 2017-09-18 15:46:05 UTC
This looks like it was a bug in gradle which fixed. The version of the FaceBook SDK listed in the bug probably still has the issue, but the newer versions which are available do not. 

From our side once the component gets into this kind of state we can't really fix it. since the `${applicationid}` is supposed to have other stuff after it e.g `${applicationid}.myitem`. Once it gets into a "dollar_openBracket_applicationId_closeBracket" we cannot fix that up. 
 
So I am closing this issue since PR https://github.com/xamarin/xamarin-android/pull/647 did fix part of the problem. The rest is down to using the latest versions of the SDK which have the correct Manifest files.
Comment 17 Mikalai Daronin 2017-09-18 16:58:56 UTC
>Once it gets into a "dollar_openBracket_applicationId_closeBracket" we cannot fix that up. 

Why?
Indeed, belvedere-1.0.2.1.aar has "dollar_openBracket_applicationId_closeBracket" in "aapt/AndroidManifest.xml", but when I use this library in a native Android app, gradle correctly handles it and builds an APK which includes an absolutely correct manifest file.