title: Launch Xamarin Android App Via a Browser Using Custom URL Scheme
body: <h2>Procedure</h2>
  
  <p>I have a cross-platform Xamarin project that needs a URL scheme. To enable this feature on Xamarin Android, I add following code in <code>AndroidManifest.xml</code> file, referenced to <a href="https://stackoverflow.com/q/2958701/9512497">launch custom android application</a> post.</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;activity android:name=".MainActivity"&gt;
      &lt;intent-filter&gt;
          &lt;action android:name="android.intent.action.VIEW" /&gt;
          &lt;category android:name="android.intent.category.DEFAULT" /&gt;
          &lt;category android:name="android.intent.category.BROWSABLE" /&gt;
          &lt;data android:scheme="urlschemetest" android:host="testurl" /&gt;
      &lt;/intent-filter&gt;
  &lt;/activity&gt;
  </code></pre>
  
  <p>Typing <code>urlschemetest://testurl</code> directly to a browser in an Android would go to Google search instead of launching the app. So, I have a simple <code>html</code> page that has a hyperlink to open the app, <a href="https://stackoverflow.com/questions/31876003/how-to-test-custom-url-scheme-in-android">test url scheme</a>.</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;a href="urlschemetest://testurl"&gt;open app&lt;/a&gt;
  </code></pre>
  
  <h2>Problem</h2>
  
  <p>By clicking on the hyperlink above, the app isn't launched. Visual Studio shows a <code>Unhandled Exception</code> error in the Output</p>
  
  <blockquote>
    <p>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]]</p>
  </blockquote>
  
  <h2>Attempted Solutions</h2>
  
  <p>1) I looked at <em>attributes</em> in <code>AndroidManifest.xml</code>, and maybe it's because <strong>package</strong> attribute did not match with <strong>namespace</strong> used in <code>MainActivity.cs</code>. So I changed it.</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
     android:versionCode="1" 
     android:versionName="1.0" 
     package="UrlSchemeTest.Droid" 
     android:installLocation="auto"&gt;
  </code></pre>
  
  <p>MainActivity.cs</p>
  
  <pre class="lang-cs prettyprint-override"><code>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
      }
  }
  </code></pre>
  
  <p>2) Using absolute classname in <code>&lt;activity&gt;</code> tag</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;activity android:name="UrlSchemeTest.Droid.MainActivity"&gt;
  </code></pre>
  
  <p>3) To see if this was a emulator bug, I've tested the app on both Android emulator and real Android device.</p>
  
  <p><strong>Note:</strong> This question is about making it work on Android platform although I've created this <a href="https://github.com/PhuongDTran/UrlSchemeTest" rel="nofollow noreferrer">Xamarin simple demo</a> on Android and iOS.</p>