Procedure
I have a cross-platform Xamarin project that needs a URL scheme. To enable this feature on Xamarin Android, I add following code in AndroidManifest.xml
file, referenced to launch custom android application post.
<activity android:name=".MainActivity">
<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="urlschemetest" android:host="testurl" />
</intent-filter>
</activity>
Typing urlschemetest://testurl
directly to a browser in an Android would go to Google search instead of launching the app. So, I have a simple html
page that has a hyperlink to open the app, test url scheme.
<a href="urlschemetest://testurl">open app</a>
Problem
By clicking on the hyperlink above, the app isn't launched. Visual Studio shows a Unhandled Exception
error in the Output
Java.Lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.companyname.UrlSchemeTest/com.companyname.UrlSchemeTest.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.companyname.UrlSchemeTest.MainActivity" on path: DexPathList[[zip file "/data/app/com.companyname.UrlSchemeTest-1/base.apk"],nativeLibraryDirectories=[/data/app/com.companyname.UrlSchemeTest-1/lib/arm, /data/app/com.companyname.UrlSchemeTest-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
Attempted Solutions
1) I looked at attributes in AndroidManifest.xml
, and maybe it's because package attribute did not match with namespace used in MainActivity.cs
. So I changed it.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
package="UrlSchemeTest.Droid"
android:installLocation="auto">
MainActivity.cs
namespace UrlSchemeTest.Droid
{
[Activity(Label = "UrlSchemeTest", Icon = "@mipmap/icon",
Theme = "@style/MainTheme", MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity :global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
//body here
}
}
2) Using absolute classname in <activity>
tag
<activity android:name="UrlSchemeTest.Droid.MainActivity">
3) To see if this was a emulator bug, I've tested the app on both Android emulator and real Android device.
Note: This question is about making it work on Android platform although I've created this Xamarin simple demo on Android and iOS.