Bug 35208 - Compile Using Android Version "Use Latest" does not append to AndroidManifest.xml
Summary: Compile Using Android Version "Use Latest" does not append to AndroidManifest...
Status: CONFIRMED
Alias: None
Product: Android
Classification: Xamarin
Component: MSBuild ()
Version: 5.1
Hardware: PC Windows
: High critical
Target Milestone: master
Assignee: Jonathan Pryor
URL:
: 59309 ()
Depends on:
Blocks:
 
Reported: 2015-10-23 12:43 UTC by Al Clark [MSFT]
Modified: 2018-05-18 21:09 UTC (History)
18 users (show)

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


Attachments
Screenshot of project properties and source of manifest (304.84 KB, image/png)
2015-10-23 12:43 UTC, Al Clark [MSFT]
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 for Bug 35208 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description Al Clark [MSFT] 2015-10-23 12:43:41 UTC
Created attachment 13507 [details]
Screenshot of project properties and source of manifest

In VS, in the project options, the following does not append the 'targetSdkVersion' attribute to the <uses-sdk /> tag

> Compile using Android version:
>    Use Latest Platform (API Level 23)
>
> Target Android version:
>    Use Compile using SDK version

It would be expected that this setup should automatically set the <uses-sdk /> tag to:

> <uses-sdk android:targetSdkVersion="23" />

Instead the tag holds no attributes.  Changing the 'Target Android version' to 'API Level 23' adds the attribute as expected.  Reverting back to 'Use Compile using SDK version' removes it.
Comment 1 Al Clark [MSFT] 2015-10-23 12:46:41 UTC
Microsoft Visual Studio Professional 2015
Version 14.0.23107.0 D14REL
Microsoft .NET Framework
Version 4.6.00079

Installed Version: Professional

Visual Basic 2015   00322-40000-00000-AA747
Microsoft Visual Basic 2015

Visual C# 2015   00322-40000-00000-AA747
Microsoft Visual C# 2015

Visual C++ 2015   00322-40000-00000-AA747
Microsoft Visual C++ 2015

Application Insights Tools for Visual Studio Package   1.0
Application Insights Tools for Visual Studio

ASP.NET and Web Tools   14.0.20626.0
ASP.NET and Web Tools

ASP.NET Web Frameworks and Tools 2013   5.2.30624.0
For additional information, visit http://www.asp.net/

Common Azure Tools   1.5
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

GenerateUnitTest   1.0
Generates unit test code for methods in classes under test.

Microsoft Azure Mobile Services Tools   1.4
Microsoft Azure Mobile Services Tools

NuGet Package Manager   3.0.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

PreEmptive Analytics Visualizer   1.2
Microsoft Visual Studio extension to visualize aggregated summaries from the PreEmptive Analytics product.

SQL Server Data Tools   14.0.50616.0
Microsoft SQL Server Data Tools

Xamarin   3.11.1537.0 (a11147b)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android   5.1.7.12 (53fce3730830417896a42f365a5ba35f1ee58d9d)
Visual Studio plugin to enable development for Xamarin.Android.

Xamarin.Forms Intellisense   1.0
Provides intellisense for Xamarin.Forms in the XML editor.

Xamarin.iOS   9.0.1.0 (73db6324ad6864caae30bf4010c185598d262e69)
Visual Studio extension to enable development for Xamarin.iOS.

Xamarin.iOS Unified Migration   1.0
Automated migration for Xamarin iOS Classic projects to Unified

Xamarin.TestCloud.Integration   1.0
Early preview of Xamarin Test Cloud integration
Comment 2 Al Clark [MSFT] 2015-10-23 12:54:07 UTC
Version information for Eric Schmeck - eric.schmeck@cosys.de

Microsoft Visual Studio Professional 2013
Version 12.0.40629.00 Update 5
Microsoft .NET Framework
Version 4.6.00079

Installierte Version: Professional

LightSwitch für Visual Studio 2013   06177-179-2300126-02950
Microsoft LightSwitch für Visual Studio 2013

Rechtschreibprüfung für die Visual Studio 2013-Codeanalyse   06177-179-2300126-02950
Rechtschreibprüfung für die Microsoft® Visual Studio® 2013-Codeanalyse

Teile des International CorrectSpell™-Rechtschreibprüfungssystems © 1993 von Lernout & Hauspie Speech Products N.V. Alle Rechte vorbehalten.

The American Heritage® Dictionary of the English Language, Third Edition Copyright © 1992 Houghton Mifflin Company. Elektronische Version lizenziert von Lernout & Hauspie Speech Products N.V. Alle Rechte vorbehalten.

Team Explorer für Visual Studio 2013   06177-179-2300126-02950
Microsoft Team Explorer für Visual Studio 2013

Visual Basic 2013   06177-179-2300126-02950
Microsoft Visual Basic 2013

Visual C# 2013   06177-179-2300126-02950
Microsoft Visual C# 2013

Visual C++ 2013   06177-179-2300126-02950
Microsoft Visual C++ 2013

Visual F# 2013   06177-179-2300126-02950
Microsoft Visual F# 2013

Windows Phone SDK 8.0 - DEU   06177-179-2300126-02950
Windows Phone SDK 8.0 - DEU

Allgemeine Azure-Tools   1.4
Bietet allgemeine Dienste für die Verwendung durch Azure Mobile Services und Microsoft Azure-Tools.

AnkhSVN - Subversion Support for Visual Studio   2.5.12478.19
AnkhSVN - Subversion Support for Visual Studio 2.5.12478.19 
 * Ankh.Package 2.5.12478.19
 * Subversion 1.8.10 via SharpSvn 1.8010.3368.58

SharpSvn is linked to: Apr 1.5.1, Apr-util 1.5.4, Cyrus Sasl 2.1.25, eXpat 2.0.1, OpenSSL 1.0.1j 15 Oct 2014, serf 1.3.8, SQLite 3.7.17, Subversion 1.8.10-SharpSvn-1.8.10, ZLib 1.2.8
SharpSvn is optionally linked to: Berkeley DB 4.4.20, SharpPlink 1.8010.3368.58

Application Insights Tools für Visual Studio Package   1.0
Application Insights Tools für Visual Studio

ASP.NET und Webtools   12.5.60612.0
Microsoft Web Developer Tools enthält die folgenden Komponenten:
Unterstützung beim Erstellen und Öffnen von ASP.NET-Webprojekten
Browserlink: Ein Kommunikationskanal zwischen Visual Studio und Browsern
Editor-Erweiterungen für HTML, CSS und JavaScript
Seitenprüfung: Prüfungstool für ASP.NET-Webprojekte
Gerüstbau: Ein Framework zum Erstellen und Ausführen von Codegeneratoren
Server Explorer-Erweiterungen für Microsoft Azure Web Apps
Webpublishing: Erweiterungen zum Veröffentlichen von ASP.NET-Webprojekten für Hostinganbieter, lokale Server oder Microsoft Azure

ASP.NET Web Frameworks and Tools 2012.2   4.1.21001.0
For additional information, visit http://go.microsoft.com/fwlink/?LinkID=309563

ASP.NET Web Frameworks and Tools 2013   5.2.30612.0
For additional information, visit http://www.asp.net/

JetBrains ReSharper 8.1   Full Edition build 8.1.23.546 on 2013-12-12T02:30:52
JetBrains ReSharper 8.1 package for Microsoft Visual Studio. For more information about ReSharper, visit http://www.jetbrains.com/resharper/. Copyright © 2003–2015 JetBrains, Inc.

Microsoft Azure Mobile Services Tools   1.4
Microsoft Azure Mobile Services Tools

NuGet-Paket-Manager   2.8.60610.756
NuGet-Paket-Manager in Visual Studio. Weitere Informationen zu NuGet finden Sie unter "http://docs.nuget.org/".

Office Developer Tools für Visual Studio 2013 DEU   12.0.30626
Microsoft Office Developer Tools für Visual Studio 2013 DEU

PreEmptive Analytics Visualizer   1.2
Microsoft Visual Studio-Erweiterung zur Visualisierung aggregierter Zusammenfassungen vom PreEmptive Analytics-Produkt.

SQL Server Data Tools   12.0.41012.0
Microsoft SQL Server Data Tools

StyleCop   4.7.49.0
Provides source code style and consistency tools. See http://www.stylecop.codeplex.com/ for more details.

Windows Phone 8.1 SDK-Integration   1.0
Dieses Paket integriert die Tools für das Windows Phone 8.1 SDK in die Menüs und Steuerelemente von Visual Studio.

Workflow Manager Tools 1.0   1.0
Dieses Paket enthält die erforderlichen Visual Studio-Integrationskomponenten für Workflow Manager.

Xamarin   3.11.1537.0 (a11147b)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android   5.1.7.12 (53fce3730830417896a42f365a5ba35f1ee58d9d)
Visual Studio plugin to enable development for Xamarin.Android.

Xamarin.Forms Intellisense   1.0
Provides intellisense for Xamarin.Forms in the XML editor.

Xamarin.iOS   9.0.1.0 (73db6324ad6864caae30bf4010c185598d262e69)
Visual Studio extension to enable development for Xamarin.iOS.

Xamarin.iOS Unified Migration   1.0
Automated migration for Xamarin iOS Classic projects to Unified

Xamarin.TestCloud.Integration   1.0
Early preview of Xamarin Test Cloud integration
Comment 3 graham 2016-03-03 12:10:49 UTC
Note that this is also a problem with Xamarin Studio on the Mac. Setting Target Android version in project settings to Automatic causes the targetSdkVersion attribute to be omitted from the manifest file. 
According to Android documentation, the effect of this is to set targetSdkVersion to the same as minSdkVersion and hence Android will apply compatibility behaviours to the app. For example, I have minSdkVersion=16. With no targetSdkVersion in the manifest, the phone will incorrectly assume pre-Marshmallow permissions and automatically enable dangerous permissions even though the target framework in the build is set to 23.
Comment 5 Jonathan Pryor 2016-05-18 19:26:11 UTC
> Instead the tag holds no attributes

//uses-sdk/@android:targetSdkVersion isn't present *in which file*?

Properties\AndroidManifest.xml is *not* the file which is used to generate the final .apk. It is instead a "template" file, which is *merged* with custom attributes and AndroidManifest.xml files included from Library projects:

https://developer.xamarin.com/guides/android/advanced_topics/working_with_androidmanifest.xml/

The generated file is obj\$(Configuration)\android\AndroidManifest.xml, and e.g. obj\Debug\android\AndroidManifest.xml will contain a //uses-sdk/@android:targetSdkVersion value.
Comment 6 Jonathan Pryor 2016-05-18 19:54:17 UTC
> //uses-sdk/@android:targetSdkVersion

My mistake; It's not present *at all*, even in the generated obj\$(Configuration)\android\AndroidManifest.xml file.

This was a deliberate design decision.

The problem is that Android treats apps different depending on whether or not they have //uses-sdk/@android:targetSdkVersion, and if it is present, what value it has.

http://lists.ximian.com/pipermail/monodroid/2011-March/003752.html

We tried providing a default value. It confused everyone.

...then the IDEs decided to clean things up, and provided a <uses-sdk/> element within AndroidManifest.xml, thus inhibiting Xamarin.Android's "let's generate a default <uses-sdk/> based on the Android SDK version..." behavior.

As such, Xamarin.Android hasn't produced a targetSdkVersion value in *years*.

I don't see a reason to change this behavior, either, because targetSdkVersion alters Android runtime behavior -- among numerous other things -- it SHOULD NOT be automatically set, because the results have confused developers.

~~~

This raises a question, then: What does the "Target Android version" drop-down *mean*? What does it control?

It controls which API level of `android.jar` is used to build and package the app. This in turn controls which Java types are available for use by Android Callable Wrappers, which resources are available, which AndroidManifest.xml attributes are available, etc.
Comment 7 graham 2016-05-18 20:15:00 UTC
I actually find the current behaviour far more confusing - omitting the targetSdkVersion is effectively the same as setting the targetSdkVersion to the minimum SDK version whilst still building with the latest version, so when you think that you have your target set to the current latest version Android is actually treating your app as though it was built for the min SDK version and applying compatibility behavours that you're not expecting. The UI makes things worse as it's not at all clear that selecting Automatic is different from selecting the latest version manually - I would expect Automatic just to mean that I don't need to change it if I install a newer SDK, but in fact it means that the targetSDKVersion doesn't get written to the manifest and so expected behaviours such as the Marshmallow permission model don't work. No where is it clear that I need to explicitly select a target SDK Version in the drop down in order for Android to treat my app as though it is built for the version of Android that it was actually built for.
Comment 8 Guillaume 2016-06-10 20:42:13 UTC
Same here as @graham... Very confusing as UI in Visual Studio looks pretty explicit with "Target Android Version: Use Compile SDK version".

@jonp regarding your comment

> This raises a question, then: What does the "Target Android version" drop-down *mean*? What does it control?
> 
> It controls which API level of `android.jar` is used to build and package the app.
According to current project settings UI in Visual Studio, this description for me relate to "Compile using Android version:" instead (the first of the 3 comboboxes).
If your statement is true, then why setting a value different than "Use Compile using SDK version" (like Android 6.0) also set the //uses-sdk/@android:targetSdkVersion in AndroidManifest.xml? According to you it should not as this combobox **do not** drive this attribute...
Comment 9 James Montemagno [MSFT] 2016-07-25 17:47:51 UTC
I would like to re-open this as this causes an issue when debugging applications. 

If you set the "Target Android version" to use Compile SDK version then the AndroidManifest.xml is NOT updated. If you are using App Compat or Material design then none of the settings work for instance ContextCompat.

If you manually set the Target Android version manually then it will work. Can we please please please fix this.
Comment 10 Jonathan Pryor 2016-12-15 00:15:58 UTC
As it happens, this entire process probably needs a complete re-think *anyway*.

https://github.com/xamarin/xamarin-android/projects/3
Comment 11 Franc 2016-12-16 02:08:11 UTC
The comments by @graham above are right on target.

A missing targetSdkVersion attribute  in the AndroidManifest.xml file means that the targetSdkVersion will be assumed to be the same as minSdkVersion.

Google developer will then not allow updating the production apk unless it is always set to minSdkVersion which, in turn, forces the app to miss on features available on higher sdk's and that the app might make use of.

This makes no sense.

A missing targetSdkVersion attribute in the AndroidManifest.xml file cannot be justified.
Comment 12 James Montemagno [MSFT] 2017-05-30 17:52:56 UTC
I see two ways to fix this as it is extremely important and a lot of developers are running into issues here. See what Franc said above, and a good example is permissions in marshmallow and code that can get stuck as the targetSDK is not set.

Here is what I think:

1.) The best fix is to insert it into the android manifest at build time as we generate it and this makes the most sense. 

2.) Remove the "Use Compile" from the IDE and just default it to the latest version on new apps or where Use Compile was.
Comment 13 Peter Collins 2017-09-07 21:04:38 UTC
I'd like to add a few more votes to help advocate for a need for change in this UX.

When selecting this automatic option in the IDE[0], I assume that my manifest will reflect this selection and include 'android:targetSdkVersion=26' in the '<uses-sdk/>' element.

However, my build generated AndroidManifest.xml (obj\$(Configuration)\android\AndroidManifest.xml) only contains <uses-sdk android:minSdkVersion="11" />'.

Now I'm susceptible to confusing runtime issues, because of the following:

> A missing targetSdkVersion attribute in the AndroidManifest.xml file means that the targetSdkVersion will be assumed to be the same as minSdkVersion.
[0] https://screencast.com/t/XdRJ4Bxabx6
Comment 14 Peter Collins 2017-09-07 21:06:22 UTC
*** Bug 59309 has been marked as a duplicate of this bug. ***