Xamarin.Android without the 'android:exported' property set error
Asked Answered
C

5

5

When Xamarin.Android is set to Android 12, I received

"You uploaded an APK or Android App Bundle which has an activity, activity alias, service or broadcast receiver with intent filter, but without the 'android:exported' property set. This file can't be installed on Android 12 or higher. See: developer.android.com/about/versions/12/behavior-changes-12#exported"

error while uploading the APK to the Google Play Console for new release.

I have added the the Exported attribute to my activities and services, yet still setting this error.

[Activity(Label = "@string/AppDrawerName", Icon = "@mipmap/ic_launcher", RoundIcon = "@mipmap/ic_launcher_round", Theme = "@style/MainTheme", MainLauncher = true, Exported = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait)]

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {

Service

[Service(Exported = true)]
    public class BarcodeService : IBarcodeService
    {

From the compile output I can see the message below

Namespace 'com.google.android.gms.stats' used in: AndroidManifest.xml, AndroidManifest.xml.

android:exported needs to be explicitly specified for element <service#crc640921eac73192168e.PNMessagingService>. Apps targeting Android 12 and higher are required to specify an explicit value for android:exported when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.

Then I go into the "obj/Debug" folder to open the Manifest, I can see the below service is auto generated

<service android:name="crc640921eac73192168e.PNMessagingService">
      <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
      </intent-filter>

Anybody know how can I set [Service(Exported = true)] for this service since it's auto generated?

Chevy answered 27/1, 2022 at 10:10 Comment(5)
Tried setting it to false?Tyner
No help still cannot. I have nuget added, I wonder if those nuget would add any activity or service which is not set?Chevy
Look at the resulting AndroidManifest in the obj/Debug/Android folder and see what is actually being exportedTyner
looking at the output manifest file in obj/Debug/Android i find the service with "intent-filter" but that not have "android:exported" setJapan
When you find the activity or service from the Manifest inside the obj/Debug/Android, you should copy and paste it into your original Manifest and simply add the "android:exported = false" attribute.Sevik
C
11

After got the info from the "obj/Debug" AndroidManifest.xml of the below

I added it into my project's Manifest manually under the tag, finally it's working. Please see below for the Manifest in project.

<application android:label="XXXXX">
    <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/notification_icon" />
    <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/super_red" />
    <service android:name="crc640921eac73192168e.PNMessagingService" android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
</application>

It's good to check the Manifest on the obj/ folder to ensure all has exported set in the parent.

Chevy answered 10/9, 2022 at 9:47 Comment(1)
You are my HERO! Welcome to Ukraine!Defeatism
A
9

I decorated my FirebaseIIDService.cs file in my Android project with [Service(Exported = true)] which did the trick. If you cannot find this file in your project, just search for com.google.firebase.MESSAGING_EVENT

[Service(Exported = true)]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class FirebaseIIDService : FirebaseMessagingService
{
    public override void OnNewToken(string token) {
        System.Threading.Tasks.Task.Run(async () => {
            //
            // this call will only work once we have valid configuration data populated
            // upon first install, false will be returned
            await Umbraco.UpdateFirebaseToken(token);
        });
    }
}
Airfield answered 7/12, 2022 at 23:29 Comment(3)
This should be considered as the right solution because adding the android exported in a generated AndroidManifest.xml does not make sense if the file will be auto-generated again. Thanks for your solution!Alamein
@Alamein this is in the .cs file, not the manifest, therefore, no, it would not be re-generated.Airfield
already fixed that but thanks for mentioning! xDAlamein
E
1

problem solution

1 - copy the generated androidManifest in the obj/debug/android/AndroidManifest.xml folder, copy and paste it in the application's main androidManifest

2- include android:exported="true" in:

activity receiver service

This solution solved the problem for me.

Esteban answered 4/1, 2023 at 17:12 Comment(1)
This was helpful, thank you. FWIW, I only copied the specific stanzas that needed to be annotated with android:exported="true".Contrapuntal
C
0

A friend advised me not to use API 31 as it had known issues. I first had to upgrade my Visual Studio then installed API 33. After that I cleaned and rebuilt the Xamarin [Android] project. When I tried to Archive it, the process failed and it gave an error this time around. Apparently, there is another generated AndroidManifest.xml file. This time I got the error and realised it was coming from a service I had created in the [ProjectName].Droid.Services. All I had to do was to add Exported = true as follows:

using Firebase.Messaging;
using System;
using System.Collections.Generic;

namespace MyApp.Droid.Services
{
    [Service(Exported = true)]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class AppFirebaseMessagingService : FirebaseMessagingService
    {
         // code here
    }
}

I then clean, rebuilt, and archived successfully. The upload to Google Play Store was also successful.

Creel answered 7/6, 2023 at 21:1 Comment(0)
O
-1

A Detailed output MSBuild project output verbosity did the trick.

enter image description here

When I turned on this feature I discovered a NotificationPushService and a TokenService element needed to add a android:exported="true" tag.

Octopod answered 29/6, 2022 at 20:24 Comment(1)
This is not the solution for the problem we are having.Rachelrachele

© 2022 - 2024 — McMap. All rights reserved.