MaikuB / flutter_appauth

A Flutter wrapper for AppAuth iOS and Android SDKs
269 stars 239 forks source link

java.lang.RuntimeException: Unable to start activity ComponentInfo #362

Open lauroboeni opened 1 year ago

lauroboeni commented 1 year ago

I'm facing some issues with the latest version of the package (flutter_appauth: 4.2.0):

It seems like some activity for RedirectUriReceiverActivity is missing / cannot be found - anyone with the same issue or any idea for the reason?

LOGS:

E/AndroidRuntime(12050):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
E/AndroidRuntime(12050):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
E/AndroidRuntime(12050):    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
E/AndroidRuntime(12050):    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
E/AndroidRuntime(12050):    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
E/AndroidRuntime(12050):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
E/AndroidRuntime(12050):    at android.os.Handler.dispatchMessage(Handler.java:107)
E/AndroidRuntime(12050):    at android.os.Looper.loop(Looper.java:214)
E/AndroidRuntime(12050):    at android.app.ActivityThread.main(ActivityThread.java:7356)
E/AndroidRuntime(12050):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(12050):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/AndroidRuntime(12050):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
E/AndroidRuntime(12050): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
E/AndroidRuntime(12050):    at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:852)
E/AndroidRuntime(12050):    at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:815)
E/AndroidRuntime(12050):    at androidx.appcompat.app.AppCompatDelegateImpl.initWindowDecorActionBar(AppCompatDelegateImpl.java:556)
E/AndroidRuntime(12050):    at androidx.appcompat.app.AppCompatDelegateImpl.getSupportActionBar(AppCompatDelegateImpl.java:543)
E/AndroidRuntime(12050):    at androidx.appcompat.app.AppCompatDelegateImpl.invalidateOptionsMenu(AppCompatDelegateImpl.java:1226)
E/AndroidRuntime(12050):    at androidx.appcompat.app.AppCompatActivity.invalidateOptionsMenu(AppCompatActivity.java:314)
E/AndroidRuntime(12050):    at androidx.activity.ComponentActivity.invalidateMenu(ComponentActivity.java:553)
E/AndroidRuntime(12050):    at androidx.activity.-$$Lambda$6vUCJMIem6_TsIeNg4KPj4HWSJA.run(Unknown Source:2)
E/AndroidRuntime(12050):    at androidx.core.view.MenuHostHelper.addMenuProvider(MenuHostHelper.java:116)
E/AndroidRuntime(12050):    at androidx.activity.ComponentActivity.addMenuProvider(ComponentActivity.java:531)
E/AndroidRuntime(12050):    at androidx.fragment.app.FragmentActivity$HostCallbacks.addMenuProvider(FragmentActivity.java:736)
E/AndroidRuntime(12050):    at androidx.fragment.app.FragmentManager.attachController(FragmentManager.java:2784)
E/AndroidRuntime(12050):    at androidx.fragment.app.FragmentController.attachHost(FragmentController.java:117)
E/AndroidRuntime(12050):    at androidx.fragment.app.FragmentActivity.lambda$init$3$FragmentActivity(FragmentActivity.java:140)
E/AndroidRuntime(12050):    at androidx.fragment.app.-$$Lambda$FragmentActivity$euPNEtWNfVdMY89Jt5kWt_WEHqw.onContextAvailable(Unknown Source:2)
E/AndroidRuntime(12050):    at androidx.activity.contextaware.ContextAwareHelper.dispatchOnContextAvailable(ContextAwareHelper.java:99)
E/AndroidRuntime(12050):    at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:352)
E/AndroidRuntime(12050):    at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:217)
E/AndroidRuntime(12050):    at net.openid.appauth.RedirectUriReceiverActivity.onCreate(RedirectUriReceiverActivity.java:49)
E/AndroidRuntime(12050):    at android.app.Activity.performCreate(Activity.java:7802)
E/AndroidRuntime(12050):    at android.app.Activity.performCreate(Activity.java:7791)
E/AndroidRuntime(12050):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
E/AndroidRuntime(12050):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
E/AndroidRuntime(12050):    ... 11 more
I/Process (12050): Sending signal. PID: 12050 SIG: 9
Lost connection to device.
MaikuB commented 1 year ago

I suspect it's the same issue raised on the Android SDK itself at https://github.com/openid/AppAuth-Android/issues/406. Although the stack trace doesn't seem to indicate that it's not found and looks like a different issue altogether. If you have a way to reproduce it without this plugin then I'd suggest to file an issue on the repo for the Android SDK as it looks like an issue with the SDK

urbanjagodic commented 1 year ago

Happens to me as well. Tested it with various emulators and physical devices running different android SDKs, but the result remains unfortunately the same.

E/AndroidRuntime(32012): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
E/AndroidRuntime(32012):    at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:852)
E/AndroidRuntime(32012):    at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:815)
E/AndroidRuntime(32012):    at androidx.appcompat.app.AppCompatDelegateImpl.initWindowDecorActionBar(AppCompatDelegateImpl.java:556)
E/AndroidRuntime(32012):    at androidx.appcompat.app.AppCompatDelegateImpl.getSupportActionBar(AppCompatDelegateImpl.java:543)
E/AndroidRuntime(32012):    at androidx.appcompat.app.AppCompatDelegateImpl.invalidateOptionsMenu(AppCompatDelegateImpl.java:1226)
E/AndroidRuntime(32012):    at androidx.appcompat.app.AppCompatActivity.invalidateOptionsMenu(AppCompatActivity.java:314)
E/AndroidRuntime(32012):    at androidx.activity.ComponentActivity.invalidateMenu(ComponentActivity.java:553)
E/AndroidRuntime(32012):    at androidx.activity.-$$Lambda$6vUCJMIem6_TsIeNg4KPj4HWSJA.run(Unknown Source:2)
E/AndroidRuntime(32012):    at androidx.core.view.MenuHostHelper.addMenuProvider(MenuHostHelper.java:116)
E/AndroidRuntime(32012):    at androidx.activity.ComponentActivity.addMenuProvider(ComponentActivity.java:531)
E/AndroidRuntime(32012):    at androidx.fragment.app.FragmentActivity$HostCallbacks.addMenuProvider(FragmentActivity.java:736)
E/AndroidRuntime(32012):    at androidx.fragment.app.FragmentManager.attachController(FragmentManager.java:2784)
E/AndroidRuntime(32012):    at androidx.fragment.app.FragmentController.attachHost(FragmentController.java:117)
E/AndroidRuntime(32012):    at androidx.fragment.app.FragmentActivity.lambda$init$3$FragmentActivity(FragmentActivity.java:140)
E/AndroidRuntime(32012):    at androidx.fragment.app.-$$Lambda$FragmentActivity$euPNEtWNfVdMY89Jt5kWt_WEHqw.onContextAvailable(Unknown Source:2)
E/AndroidRuntime(32012):    at androidx.activity.contextaware.ContextAwareHelper.dispatchOnContextAvailable(ContextAwareHelper.java:99)
E/AndroidRuntime(32012):    at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:352)
E/AndroidRuntime(32012):    at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:217)
E/AndroidRuntime(32012):    at net.openid.appauth.RedirectUriReceiverActivity.onCreate(RedirectUriReceiverActivity.java:49)
E/AndroidRuntime(32012):    at android.app.Activity.performCreate(Activity.java:7994)
E/AndroidRuntime(32012):    at android.app.Activity.performCreate(Activity.java:7978)
E/AndroidRuntime(32012):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
E/AndroidRuntime(32012):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
E/AndroidRuntime(32012):    ... 11 more
I/Process (32012): Sending signal. PID: 32012 SIG: 9
Lost connection to device.
lauroboeni commented 1 year ago

@urbanjagodic I found a workaround to make it work again - however, I'm not yet sure if that is a proper solution to the problem.

In my case I had to add xmlns:tools="http://schemas.android.com/tools" in the <manifest> clause in AndroidManifest.xml and then overwrite the theme.

That looks as follows:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="XXXX">
...
<application (...)
tools:replace="android:theme"
android:theme="@style/AppTheme">
....
urbanjagodic commented 1 year ago

@lauroboeni Thanks for the workaround, unfortunately i haven't found any proper solution to this issue, if it's SDK correlated than we will just have to wait for an update.

MaikuB commented 1 year ago

Do either of you have this issue running the example app? If not, can you create a repo that hosts a minimal app that can reproduce the issue

mattheuten commented 1 year ago

@lauroboeni Does the workaround still work for you? Flutter does not ship with an AppTheme style by default. Do you have an example for me? I'm running into the same issue.

lauroboeni commented 1 year ago

@mattheuten The workaround still works in my case, yes.

However, I also found out that apparently some specific versions of flutter_appauth and local_auth_android are incompatible which then leads to such crashs as mentioned above.

More precise, my pubspec.yaml package declarations were as follows when above mentioned crash occured:

  flutter_appauth: 4.2.0
  local_auth: ^2.0.2
  local_auth_ios: ^1.0.5
  local_auth_android: ^1.0.9

When downgrading (and fixing) the local_auth_android package to 1.0.8, it perfectly works fine. It could be related with the Android Fragments - but that's currently only a guess and I need to investigate it further.

lauroboeni commented 1 year ago

@MaikuB the example app itself works fine on my side.

However, if I add

local_auth: ^2.0.2
local_auth_ios: ^1.0.5
local_auth_android: ^1.0.9

to pubspec.yaml of the example app, the example app does not build anymore with the following logs:

java.lang.RuntimeException: java.lang.RuntimeException: Duplicate class androidx.lifecycle.DefaultLifecycleObserver found in modules lifecycle-common-2.5.1.jar (androidx.lifecycle:lifecycle-common:2.5.1) and lifecycle-common-java8-2.2.0.jar (androidx.lifecycle:lifecycle-common-java8:2.2.0)

If I add

local_auth: ^2.0.2
local_auth_ios: ^1.0.5
local_auth_android: 1.0.8

the app starts perfectly fine.

So it really seems that these package versions are somehow not compatible.

MaikuB commented 1 year ago

@lauroboeni yep it would seem that way. My educated guess based on those logs is to do with conflicting AndroidX dependencies. These aren't explicitly specified by this plugin so could be to do with what's specified by the AppAuth Android SDK, your own app and/or the local_auth plugin. None of these are things I could help with as it's outside the control this plugin. I believe Android allows you to specify dependency overrides that you can look into to see if that also helps solve the issue

micmar commented 1 year ago

Hello, we experienced the same issue, but we think that we could solve it by replacing startActivityForResult (deprecated) and onActivityResult with registerForActivityResult

@MaikuB would you consider a PR if we prepare it?

Thanks and keep up the good work!

MaikuB commented 1 year ago

@micmar sure, open one and I'll review it

MaikuB commented 1 year ago

@micmar would you be able to elaborate on what makes you think that is the cause of the issue? The logs @lauroboeni shared points to conflicting dependencies so ordinarily I wouldn't think that switching the Android API used would fix something like this

micmar commented 1 year ago

@MaikuB IMHO the conflicting dependencies problem is due from the fact that flutter_appauth requires the main activity to inherit from FlutterActivity, while other packages require to inehrit from FlutterFragmentActivity.

Now in FlutterAppauthPlugin.java, deprecated APIs are used at lines: 370 and 462 merely the startActivityForResult calls. startActivityForResult + onActivityResults are now deprecated.

Apparently one should use registerForActivtyResult instead to get a launcher, that can be used to launch an intent.

Everything is explained very well in here:

https://tedblob.com/fragment-startactivityforresult-is-deprecated/

Hope it helps. Mik

PS: allow me to apologise as so far I did not have time to try it...

paul-charlton commented 1 year ago

we've traced this issue in our app to the local_auth library and I've raised a bug there (https://github.com/flutter/flutter/issues/113208). As a workaround I've downgraded the local_auth to version 1.1.11 and that seems to work.

@MaikuB I don't understand enough about what happens when packages are built to understand what might be happening here, or whether it helps narrow down the why?

MaikuB commented 1 year ago

IMHO the conflicting dependencies problem is due from the fact that flutter_appauth requires the main activity to inherit from FlutterActivity, while other packages require to inehrit from FlutterFragmentActivity.

@micmar if that were the cause then it should cause a problem for more versions of local_auth. However, if you look at what @lauroboeni shared, this problem occurs when local_auth_android 1.0.9+ is pulled. With 1.0.8, it's fine. The difference between these two is local_auth_android bumped an AndroidX library dependency. This should lend more credence to it being a dependency conflict and not to do with use of older APIs.

I would suggest those experiencing the issue look into how dependency resolution works on Android and try overriding it. Googling the error may help provide some solutions. Note that from the plugin's side, the only Android dependency it explicitly pulls in is the AppAuth Android SDK so anything else like AndroidX is transitive via the SDK. This means there isn't a fix for the plugin side. and libraries like this plugin shouldn't be trying to specify dependency overrides

paul-charlton commented 1 year ago

@MaikuB when you say trying to overriding the android dependency do you mean trying to get 'our' app AppAuth to use the older AndroidX dependency?

Also, could this be addressed by updating the AndroidX dependency in flutter_appauth.

(Apologies if these seem obvious, I'm very fresh to flutter development)

postflow commented 1 year ago

@urbanjagodic I found a workaround to make it work again - however, I'm not yet sure if that is a proper solution to the problem.

In my case I had to add xmlns:tools="http://schemas.android.com/tools" in the <manifest> clause in AndroidManifest.xml and then overwrite the theme.

That looks as follows:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="XXXX">
...
<application (...)
tools:replace="android:theme"
android:theme="@style/AppTheme">
....

i try you solution but obtain exception: ".....style/AppTheme) not found."

PraveenGangula1392 commented 1 year ago

@MaikuB IMHO the conflicting dependencies problem is due from the fact that flutter_appauth requires the main activity to inherit from FlutterActivity, while other packages require to inehrit from FlutterFragmentActivity.

Now in FlutterAppauthPlugin.java, deprecated APIs are used at lines: 370 and 462 merely the startActivityForResult calls. startActivityForResult + onActivityResults are now deprecated.

Apparently one should use registerForActivtyResult instead to get a launcher, that can be used to launch an intent.

Everything is explained very well in here:

https://tedblob.com/fragment-startactivityforresult-is-deprecated/

Hope it helps. Mik

PS: allow me to apologise as so far I did not have time to try it...

Hi @micmar Where you able to migrate the depreciated code and try it out ?

LennartMart commented 1 year ago

Was updating my app and ran into this problem. Works with following settings ( Thanks @lauroboeni !) :

flutter_appauth: ^4.2.1
local_auth: ^2.1.3
local_auth_android: 1.0.8

When looking into the changelog of local_auth_android (https://pub.dev/packages/local_auth_android/changelog), it seems to be incompatible since they've upgraded androidx.fragment to 1.5.1.

RWolfing commented 10 months ago

Also started to run into this issue recently. I am pretty surprised at the proposed solutions tbh. We are not using local_auth

The relevant parts of the logs are:

E/AndroidRuntime(12050): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
[...]
E/AndroidRuntime(12050):    at net.openid.appauth.RedirectUriReceiverActivity.onCreate(RedirectUriReceiverActivity.java:49)
E/AndroidRuntime(12050):    at android.app.Activity.performCreate(Activity.java:7802)
E/AndroidRuntime(12050):    at android.app.Activity.performCreate(Activity.java:7791)
E/AndroidRuntime(12050):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
E/AndroidRuntime(12050):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
E/AndroidRuntime(12050):    ... 11 more
I/Process (12050): Sending signal. PID: 12050 SIG: 9

This means that for some reason the RedirectUriReceiverActivity from the android library here is started without an AppCompat theme. Which is surprising as the RedirectUriReceiverActivity extends correctly from AppCompatActivity and in our Flutter app MainActivity is also extending the correct FlutterFragmentActivity as well as using the correct styles with Theme.AppCompat.

I am still scratching my head to what changed for us, as the plugin did work recently. The only potential issue I see is that the plugin uses the base Activity instead of any AppCompat here. And also does not use AppCompat.startActivityForResult here. (Which is now additionally deprecated)

So my last guess so far would be that there might be sort of erasure as only the superclass startActivityForResult is called instead of the AppCompat version and therefore the Theme is not overwritten with the correct Theme.AppCompat. I will try to make some time to validate this.

Anyway regarding the proposed fixes, imo this is the way: In your manifest overwrite the redirect activity with an appropriate appcompat theme.

        <activity
            android:name="net.openid.appauth.RedirectUriReceiverActivity"
            android:exported="true"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar"
            tools:node="replace">
            <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="your-scheme-check-docu-for-info"/>
            </intent-filter>
        </activity>

And on a personal note, I do think this is an issue in the plugin even if I can not pinpoint it yet. Maybe it would help to also update the example app to some up2date version of flutter.

trunhquang commented 10 months ago

This issue happen to me in version flutter_appauth: ^6.0.2. I resolve by add appcompat to build.gradle(app): dependencies { ... implementation 'androidx.appcompat:appcompat:1.6.1' } I hope it can help.

Devbrail commented 6 months ago

This issue happen to me in version flutter_appauth: ^6.0.2. I resolve by add appcompat to build.gradle(app): dependencies { ... implementation 'androidx.appcompat:appcompat:1.6.1' } I hope it can help.

That's works for me also Thanks @trunhquang