xamarin / XamarinComponents

Plugins for Xamarin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exception `Android.Content.ActivityNotFoundException` when targeting Android `TargetFrameworkVersion` above 10

RManesiya opened this issue · comments

I am using the following Nuget package https://www.nuget.org/packages/Xamarin.OpenId.AppAuth.Android/0.7.0 in my Xamarin project to authenticate the user against an OpenId Connect server. Below is the portion of the code that does the work for authenticating the user.

var _authService = new AuthorizationService(MainActivity.Instance);
var serviceConfig= await AuthorizationServiceConfiguration.FetchFromUrlAsync(Android.Net.Uri.Parse(OpenIdConnectConstants.DiscoveryEndpoint));
var authState = new AuthState();

var authRequest = new AuthorizationRequest.Builder(serviceConfig, OpenIdConnectConstants.ClientId, ResponseTypeValues.Code, Android.Net.Uri.Parse(OpenIdConnectConstants.RedirectUri))
.SetScope(string.Join(" ", OpenIdConnectConstants.Scopes))
.SetAdditionalParameters(OpenIdConnectConstants.AdditionalParameters)
.Build();

var discoveryDoc = serviceConfig.DiscoveryDoc;

var intent = new Intent(context, typeof(MainActivity));
intent.PutExtra(OpenIdConnectConstants.AuthStateKey, authState.JsonSerializeString());

if (discoveryDoc != null)
{
  intent.PutExtra(OpenIdConnectConstants.AuthServiceDiscoveryKey, discoveryDoc.DocJson.ToString());
}

var postAuthorizationIntent  = PendingIntent.GetActivity(MainActivity.Instance, authRequest.GetHashCode(), intent, PendingIntentFlags.Mutable)

_authService.PerformAuthorizationRequest(authRequest, postAuthorizationIntent);

Works (A browser window is a launched, and redirected where I can log in to FB, Twitter, etc. via OpenID connect server, once logged in I am back on the MainActivity screen, the way things are supposed to work)
The values in AndroidManifest.xml and in MyTestAndroid.csproj

<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29" />
<application android:label="TestMobile" android:theme="@style/MainTheme" android:icon="@drawable/logo">
<!-- Callback from authentication screen -->
<activity android:name="net.openid.appauth.RedirectUriReceiverActivity" android:exported="true">
    <!-- Filter which captures custom scheme based redirects for OpenID server authorization requests. -->
    <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="org.my.test.mobile" android:path="callback" />
    </intent-filter>
</activity>
</application>
 ...

<TargetFrameworkVersion>v10.0</TargetFrameworkVersion>

Doesn't Work (android:targetSdkVersion > 29 and TargetFrameworkVersion > 10)
The values in AndroidManifest.xml and in MyTestAndroid.csproj

<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />

<TargetFrameworkVersion>v11.0</TargetFrameworkVersion>

The code fails with the following error when the last piece of code _authService.PerformAuthorizationRequest(auth request, postAuthorizationIntent); gets executed. I get an exception.

Android.Content.ActivityNotFoundException: ''

Stack Trace

Android.Content.ActivityNotFoundException: Exception of type 'Android.Content.ActivityNotFoundException' was thrown.
  at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0006e] in <1921523bc22e407fa9a0855bdae29335>:0 
  at Android.Runtime.JNIEnv.CallVoidMethod (System.IntPtr jobject, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x00000] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNIEnv.g.cs:264 
  at OpenId.AppAuth.AuthorizationService.PerformAuthorizationRequest (OpenId.AppAuth.AuthorizationRequest request, Android.App.PendingIntent completedIntent) [0x0006e] in <8220ff7032c44b3fab43311d8877aa65>:0 
  at My.Android.Implementations.LoginProvider.MakeAuthRequest (OpenId.AppAuth.AuthorizationServiceConfiguration serviceConfig, OpenId.AppAuth.AuthState authState) [0x00054] in C:\My.Android\Implementations\LoginProvider.cs:73 
  --- End of managed Android.Content.ActivityNotFoundException stack trace ---
android.content.ActivityNotFoundException
	at net.openid.appauth.AuthorizationService.prepareAuthorizationRequestIntent(AuthorizationService.java:351)
	at net.openid.appauth.AuthorizationService.performAuthorizationRequest(AuthorizationService.java:221)
	at net.openid.appauth.AuthorizationService.performAuthorizationRequest(AuthorizationService.java:142)
	at net.openid.appauth.AuthorizationServiceConfiguration_RetrieveConfigurationCallback.n_onFetchConfigurationCompleted(Native Method)
	at net.openid.appauth.AuthorizationServiceConfiguration_RetrieveConfigurationCallback.onFetchConfigurationCompleted(AuthorizationServiceConfiguration_RetrieveConfigurationCallback.java:30)
	at net.openid.appauth.AuthorizationServiceConfiguration$ConfigurationRetrievalAsyncTask.onPostExecute(AuthorizationServiceConfiguration.java:364)
	at net.openid.appauth.AuthorizationServiceConfiguration$ConfigurationRetrievalAsyncTask.onPostExecute(AuthorizationServiceConfiguration.java:305)
	at android.os.AsyncTask.finish(AsyncTask.java:771)
	at android.os.AsyncTask.access$900(AsyncTask.java:199)
	at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:788)
	at android.os.Handler.dispatchMessage(Handler.java:106)
	at android.os.Looper.loopOnce(Looper.java:201)
	at android.os.Looper.loop(Looper.java:288)
	at android.app.ActivityThread.main(ActivityThread.java:7839)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
}
    base: {Java.Lang.RuntimeException}

I resolved this issue by following the workaround suggested here openid/AppAuth-Android#599
This is what I ended up putting in AndroidManifest.xml

<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
<queries>
  <intent>
	  <action android:name="android.intent.action.VIEW" />
	  <category android:name="android.intent.category.BROWSABLE" />
	  <category android:name="android.intent.category.DEFAULT" />
	  <data android:scheme="https" />
  </intent>
  <intent>
	  <action android:name="android.support.customtabs.action.CustomTabsService" />
  </intent>
</queries>

in MyTestAndroid.csproj

<TargetFrameworkVersion>v11.0</TargetFrameworkVersion>