Search code examples
mauiandroid-deep-link

Deep Link for Android with .Net Maui error - Unable to instantiate activity ComponentInfo MainActivity


I'm trying to get deep links to work for my maui android app but when I click to open the link with the app I get this error:

Java.Lang.RuntimeException: 'Unable to instantiate activity ComponentInfo{app.mydomain/MyApp.Client.MAUI.MainActivity}:
java.lang.ClassNotFoundException: Didn't find class "MyApp.Client.MAUI.MainActivity" 
on path: DexPathList[[zip file "/data/app/~~0C9vYHAHNOr9CaB4f63clQ==/app.mydomain-YYQ6ZPc3-KSVnF8_VcuQVQ==/base.apk"],
nativeLibraryDirectories=[/data/app/~~0C9vYHAHNOr9CaB4f63clQ==/app.mydomain-YYQ6ZPc3-KSVnF8_VcuQVQ==/lib/arm64, 
/data/app/~~0C9vYHAHNOr9CaB4f63clQ==/app.mydomain-YYQ6ZPc3-KSVnF8_VcuQVQ==/base.apk!/lib/arm64-v8a, /system/lib64, 
/system_ext/lib64]]'

My MainActivity.cs looks like this

namespace MyApp.Client.MAUI
{
 [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
    DataScheme = "https", DataHost = "mydomain.app", DataPathPattern = "/.*", AutoVerify = true)]
public class MainActivity : MauiAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
    }
    protected override void OnResume()
    {
        base.OnResume();

        Platform.OnResume(this);
    }
    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);

        var data = intent.DataString;

        if (intent.Action != Intent.ActionView) return;
        if (string.IsNullOrWhiteSpace(data)) return;

        var path = data.Replace(@"https://mydomain.app", "");
        //todo - handle path

        StartActivity(typeof(MainActivity));
    }
}

The intent filter in the AndroidManifest.xml is like so:

    <activity android:name="MyApp.Client.MAUI.MainActivity" android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="https" android:host="mydomain.app" android:pathPattern="/.*" />
        </intent-filter>
    </activity>

Solution

  • I could reproduce your problem.

    And I can resolve this problem by adding tag <application> inside of AndroidManifest.xml.

    You can refer to the full code of AndroidManifest.xml on my side:

    <?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android">
          <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
          <uses-permission android:name="android.permission.INTERNET" />
    
          <application android:allowBackup="true"  android:supportsRtl="true" android:theme="@style/AppTheme" android:name="android.app.Application" android:debuggable="true" android:extractNativeLibs="true">
                <activity android:name="MauiBlazorApp.MainActivity" android:exported="true">
                      <intent-filter>
                            <action android:name="android.intent.action.VIEW" />
                            <category android:name="android.intent.category.DEFAULT" />
                            <category android:name="android.intent.category.BROWSABLE" />
                            <data android:scheme="https" android:host="mydomain.app" android:pathPattern="/.*" />
                      </intent-filter>
                </activity>
                
          </application>
    
    </manifest>
    

    Note: Remember to change the value of android:name(YourAppName) of your activity to yours.

    <application android:name="YourAppName.MainActivity">