MAUI Android应用实现Deep Linking时遭遇ClassNotFoundException
点击ed2k链接选择应用打开时,抛出以下错误:
Java.Lang.RuntimeException: 'Unable to instantiate activity ComponentInfo{com.mycompany.test_maui/com.mycompany.test_maui.test_maui.MainActivity}:
java.lang.ClassNotFoundException: Didn't find class "com.mycompany.test_maui.test_maui.MainActivity" on path:
DexPathList[[zip file "/data/app/com.mycompany.test_maui-xpJaFrKRoLluech0atEfZQ==/base.apk"],
nativeLibraryDirectories=[/data/app/com.mycompany.test_maui-xpJaFrKRoLluech0atEfZQ==/lib/arm64,
/data/app/com.mycompany.test_maui-xpJaFrKRoLluech0atEfZQ==/base.apk!/lib/arm64-v8a, /system/lib64, /hw_product/lib64, /system/product/lib64, /prets/lib64]]
现有代码
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mycompany.test_maui"> <application android:allowBackup="true" android:icon="@mipmap/appicon" android:supportsRtl="true" android:usesCleartextTraffic="true"> <activity android:name=".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="ed2k" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> </manifest>
MainActivity.cs
using Android.App; using Android.Content.PM; using Android.OS; using Android.Widget; namespace test_maui { [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)] public class MainActivity : MauiAppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Check if the activity was started from a link if (Android.Content.Intent.ActionView.Equals(Intent.Action)) { // Get the link var elink = Intent.DataString; // Display a message on screen with the link Toast.MakeText(this, "ed2k Link: " + elink, ToastLength.Long).Show(); } } } }
解决方案
错误根源是AndroidManifest中指定的Activity类名与实际编译生成的类名不匹配:
- AndroidManifest的
package为com.mycompany.test_maui,使用.MainActivity会被解析为com.mycompany.test_maui.MainActivity - 但MainActivity的命名空间是
test_maui,实际生成的Java类全名为test_maui.MainActivity
可以通过以下两种方式修复:
方式一:统一命名空间与AndroidManifest的package
修改MainActivity.cs的命名空间,与AndroidManifest中的package保持一致:
namespace com.mycompany.test_maui // 改为和package相同的命名空间 { [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)] public class MainActivity : MauiAppCompatActivity { // 原有代码不变 } }
方式二:在AndroidManifest中指定完整类名
修改AndroidManifest.xml中Activity的android:name为完整的类名(包含命名空间):
<activity android:name="test_maui.MainActivity" android:exported="true"> <!-- 原有intent-filter不变 --> </activity>
修复后,点击ed2k链接即可正常启动应用并显示链接提示。
内容的提问来源于stack exchange,提问作者Carlos García

