Skip to content

Conversation

@jonpryor
Copy link
Contributor

@jonpryor jonpryor commented Aug 4, 2023

Context: https://developercommunity.visualstudio.com/t/Cannot-deploy-to-Android-emulators-and-d/10428163
Context: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1863835
Context: https://developer.android.com/guide/components/intents-filters

The <activity/> and related elements within AndroidManifest.xml can have multiple <intent-filter/>s specified. Android does not require that <intent-filter/>s be listed in any particular order.

However, Visual Studio does care about the order (?!)!

[Activity(Exported = true, Label = "@string/app_name", MainLauncher = true)]
[IntentFilter(
    new[] { Android.Content.Intent.ActionView },
    Categories = new[] {
        Android.Content.Intent.CategoryDefault,
        Android.Content.Intent.CategoryBrowsable,
    }
)]
[IntentFilter(
    new[] { Android.Content.Intent.ActionMain },
    Categories = new[] {
        Android.Content.Intent.CategoryLauncher,
        Android.Content.Intent.CategoryLeanbackLauncher,
    }
)]
public partial class MainActivity : Activity {}

If the [IntentFilter] with Android.Content.Intent.ActionMain is not first, then Visual Studio does not consider this Activity to be a launchable activity.

The reason for this is, in part, because
AndroidAppManifest.GetLaunchableActivities() only looked at the first <intent-filter/> to see if it had the category of android.intent.category.LAUNCHER.

Update AndroidAppManifest.GetLaunchableActivities() so that all <intent-filter/> elements are checked for the
.LAUNCHER category.

Workaround: Specify the [IntentFilter] with Intent.ActionMain first:

[Activity(Exported = true, Label = "@string/app_name", MainLauncher = true)]
[IntentFilter(
    new[] { Android.Content.Intent.ActionMain },
    Categories = new[] {
        Android.Content.Intent.CategoryLauncher,
        Android.Content.Intent.CategoryLeanbackLauncher,
    }
)]
[IntentFilter(
    new[] { Android.Content.Intent.ActionView },
    Categories = new[] {
        Android.Content.Intent.CategoryDefault,
        Android.Content.Intent.CategoryBrowsable,
    }
)]
public partial class MainActivity : Activity {}

Note: this change, in and of itself, may not be sufficient to fix Visual Studio, as there are a few other places that have the same "only check the first <intent-filter/>" bug.

Context: https://developercommunity.visualstudio.com/t/Cannot-deploy-to-Android-emulators-and-d/10428163
Context: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1863835
Context: https://developer.android.com/guide/components/intents-filters

The [`<activity/>`][0] and related elements within
`AndroidManifest.xml` can have multiple [`<intent-filter/>`][1]s
specified.  Android does not require that `<intent-filter/>`s be
listed in any particular order.

However, Visual Studio *does* care about the order (?!)!

	[Activity(Exported = true, Label = "@string/app_name", MainLauncher = true)]
	[IntentFilter(
	    new[] { Android.Content.Intent.ActionView },
	    Categories = new[] {
	        Android.Content.Intent.CategoryDefault,
	        Android.Content.Intent.CategoryBrowsable,
	    }
	)]
	[IntentFilter(
	    new[] { Android.Content.Intent.ActionMain },
	    Categories = new[] {
	        Android.Content.Intent.CategoryLauncher,
	        Android.Content.Intent.CategoryLeanbackLauncher,
	    }
	)]
	public partial class MainActivity : Activity {}

If the `[IntentFilter]` with `Android.Content.Intent.ActionMain`
is *not* first, then Visual Studio does not consider this Activity
to be a launchable activity.

The reason for this is, in part, because
`AndroidAppManifest.GetLaunchableActivities()` only looked at the
*first* `<intent-filter/>` to see if it had the category of
`android.intent.category.LAUNCHER`.

Update `AndroidAppManifest.GetLaunchableActivities()` so that *all*
`<intent-filter/>` elements are checked for the
`.LAUNCHER` category.

**Workaround**: Specify the `[IntentFilter]` with `Intent.ActionMain`
first:

	[Activity(Exported = true, Label = "@string/app_name", MainLauncher = true)]
	[IntentFilter(
	    new[] { Android.Content.Intent.ActionMain },
	    Categories = new[] {
	        Android.Content.Intent.CategoryLauncher,
	        Android.Content.Intent.CategoryLeanbackLauncher,
	    }
	)]
	[IntentFilter(
	    new[] { Android.Content.Intent.ActionView },
	    Categories = new[] {
	        Android.Content.Intent.CategoryDefault,
	        Android.Content.Intent.CategoryBrowsable,
	    }
	)]
	public partial class MainActivity : Activity {}

Note: this change, in and of itself, may not be sufficient to fix
Visual Studio, as there are a few other places that have the same
"only check the first `<intent-filter/>`" bug.

[0]: https://developer.android.com/guide/topics/manifest/activity-element
[1]: https://developer.android.com/guide/topics/manifest/intent-filter-element
@jonpryor jonpryor merged commit 52f0866 into main Aug 5, 2023
@jonpryor jonpryor deleted the jonp-check-all-filters branch August 5, 2023 08:36
jonpryor pushed a commit to dotnet/android that referenced this pull request Aug 17, 2023
Context: https://developercommunity.visualstudio.com/t/Cannot-deploy-to-Android-emulators-and-d/10428163

Changes: dotnet/android-tools@57be026...52f0866

  * dotnet/android-tools@52f0866: [Xamarin.Android.Tools.AndroidSdk] Check all <intent-filter/>s (dotnet/android-tools#214)

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
jonpryor pushed a commit to dotnet/java-interop that referenced this pull request Sep 11, 2023
Changes: dotnet/android-tools@3cee10b...9c50a2d

  * dotnet/android-tools@9c50a2d: [build] set `$(DisableTransitiveFrameworkReferenceDownloads)`=true (dotnet/android-tools#216)
  * dotnet/android-tools@52f0866: [Xamarin.Android.Tools.AndroidSdk] Check all <intent-filter/>s (dotnet/android-tools#214)
  * dotnet/android-tools@57be026: [Xamarin.Android.Tools.AndroidSdk] Update SDK component for API-34 (dotnet/android-tools#211)
  * dotnet/android-tools@0a9ea47: [Xamarin.Android.Tools.AndroidSdk] Add API-34 to KnownVersions (dotnet/android-tools#212)

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants