Google Auth migrating to androidx: SignInHubActivity does not implement Lifecycle

孤街浪徒 提交于 2019-11-30 01:38:29

问题


After the migration to AndroidX i'am facing this the following issue with the Google Auth lib:

On Android 9 API 28:

java.lang.IncompatibleClassChangeError: Class 'com.google.android.gms.auth.api.signin.internal.SignInHubActivity' does not implement interface 'androidx.lifecycle.LifecycleOwner' in call to 'androidx.lifecycle.Lifecycle androidx.lifecycle.LifecycleOwner.getLifecycle()' (declaration of 'androidx.lifecycle.LiveData' appears in /data/app/fourbottles.bsg.workinghours4b-G1onPKgFFE-l3aqjx0qDJw==/split_lib_dependencies_apk.apk)
    at androidx.lifecycle.LiveData.observe(LiveData.java:172)
    at androidx.loader.app.LoaderManagerImpl$LoaderInfo.setCallback(LoaderManagerImpl.java:100)
    at androidx.loader.app.LoaderManagerImpl.createAndInstallLoader(LoaderManagerImpl.java:400)
    at androidx.loader.app.LoaderManagerImpl.initLoader(LoaderManagerImpl.java:421)
    at com.google.android.gms.auth.api.signin.internal.SignInHubActivity.zzn(Unknown Source:80)
    at com.google.android.gms.auth.api.signin.internal.SignInHubActivity.onActivityResult(Unknown Source:68)
    at android.app.Activity.dispatchActivityResult(Activity.java:7454)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4353)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4402)
    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

On Android 5.0.2 API 22

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.lifecycle.Lifecycle$State androidx.lifecycle.Lifecycle.getCurrentState()' on a null object reference
    at androidx.lifecycle.LiveData.observe(LiveData.java:172)
    at androidx.loader.app.LoaderManagerImpl$LoaderInfo.setCallback(LoaderManagerImpl.java:100)
    at androidx.loader.app.LoaderManagerImpl.createAndInstallLoader(LoaderManagerImpl.java:400)
    at androidx.loader.app.LoaderManagerImpl.initLoader(LoaderManagerImpl.java:421)
    at com.google.android.gms.auth.api.signin.internal.SignInHubActivity.zzn(Unknown Source)
    at com.google.android.gms.auth.api.signin.internal.SignInHubActivity.onActivityResult(Unknown Source)
    at android.app.Activity.dispatchActivityResult(Activity.java:6139)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3535)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582) 
    at android.app.ActivityThread.access$1300(ActivityThread.java:144) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5221) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

I'am using the following lib versions:

implementation 'com.google.android.gms:play-services-gcm:16.0.0'
implementation 'com.google.android.gms:play-services-auth:16.0.1'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.multidex:multidex:2.0.1'

The code is crashing as soon as i start an activity with the intent:

val signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient)
startActivityForResult(signInIntent, LOGIN_WITH_GOOGLE_CODE)

I have the following properties into the settings.gradle file:

android.useAndroidX=true
android.enableJetifier=true

The Google Auth lib doesn't support authentication with AndroidX libs?

Is there a way to start an intent from an androidX lib with the supported lib behaviour?  

I accept answers in both java and kotlin languages.

Update 1: 27.12.2017

Google api client creation:

 GoogleSignInOptions googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

 googleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this, this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
                .build();

It may be beacuse of enableAutoManage ?

Update 2: 28.12.2018

I managed to login with the following piece of code:

Task<GoogleSignInAccount> sign = googleSignInClient.silentSignIn();
GoogleSignInAccount result = sign.getResult();

So i think there is no problem with the credentials or the other configuration files. In this way the user won't be able to choose an account.

I think that Jetifier is failing in same way.

With API 27 i'am getting the same error.


回答1:


the same issue! I've just added the "core" dependency and bumped "appcompat" to the latest alpha version and it helped.

dependencies {

implementation "androidx.core:core:1.1.0-alpha03"
implementation "androidx.legacy:legacy-support-v4:1.0.0"
implementation "androidx.annotation:annotation:1.0.1"
implementation "androidx.recyclerview:recyclerview:1.1.0-alpha01"
implementation "androidx.appcompat:appcompat:1.1.0-alpha01"
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "androidx.cardview:cardview:1.0.0"
implementation "com.google.android.material:material:1.1.0-alpha02"

implementation "com.google.firebase:firebase-core:16.0.6"
implementation "com.google.firebase:firebase-auth:16.1.0"
implementation "com.google.firebase:firebase-storage:16.0.5"
implementation "com.google.firebase:firebase-perf:16.2.3"
implementation 'com.firebaseui:firebase-ui-database:4.3.0'

implementation "com.google.android.gms:play-services-auth:16.0.1"
implementation "com.google.android.gms:play-services-location:16.0.0"
implementation "com.google.android.gms:play-services-maps:16.0.0"
...
}

I use:

compileSdkVersion 28
buildToolsVersion "28.0.3"

Top gradle.build:

dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0'
    classpath 'com.google.gms:google-services:4.2.0'
    ...
}

gradle.properties:

android.debug.obsoleteApi=true
android.enableR8 = true
android.useAndroidX=true
android.enableJetifier=true

gradle wrapper's gradle version is set to gradle-4.10.1-all.zip. Also I tried to Invalidate/restart my AS 3.3. Hope it will help somebody.




回答2:


I fixed this issue by using the latest version of the appcompat library:

implementation "androidx.appcompat:appcompat:1.1.0-rc01"

In previous versions the class AppCompatActivity did not implement the LifecycleOwner interface thus leading to the errors you observed.




回答3:


I found an work around until i find a fix or the library is updated:

Task<GoogleSignInAccount> sign = googleSignInClient.silentSignIn();

if(sign.isSuccessful())
{
    GoogleSignInAccount resultAccount = sign.getResult();
    handleGoogleLogin(resultAccount);
}else
{
     Intent signInIntent = googleSignInClient.getSignInIntent();
     startActivityForResult(signInIntent, GOOGLE_SIGN_IN_CODE);
}

The code try to sign in silently, if the user didn't allow the sign in already, will start the intent. In my case the intent let me choose the account and crashes after tapping on the account. After the first tap, the silent signin will work.




回答4:


I fixed the issue by adding this dependency to my gradle:

implementation 'androidx.arch.core:core-runtime:2.0.1-alpha01'

EDIT

Those are the versions i have used:

  • App level gradle compileSdkVersion = 28, buildToolsVersion = "28.0.3":

    implementation "com.google.android.gms:play-services-auth:16.0.1"
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.vectordrawable:vectordrawable:1.0.1'
    implementation 'androidx.arch.core:core-runtime:2.0.1-alpha01' 
    

    All com.android.support libraries are of version: 28.0.0

  • Project level gradle:

    classpath 'com.android.tools.build:gradle:3.3.0'
    classpath 'com.google.gms:google-services:4.2.0'
    

Note 1: If you have the below line, try to build without it:

implementation "androidx.legacy:legacy-support-v4:1.0.0"

Note 2: If you are building with Android Studio 3.3 (the latest at the time i am writing this answer) I got these errors when trying to migrate to androidX:

WARNING: API 'variant.getJavaCompiler()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()'. It will be removed at the end of 2019. For more information, see https://d.android.com/r/tools/task-configuration-avoidance. To determine what is calling variant.getJavaCompiler(), use -Pandroid.debug.obsoleteApi=true on the command line to display a stack trace. Affected Modules: app

I could not manage to fix it, but just running the project with this error exists works surprisingly.




回答5:


I had same issue. I fixed it by changing following depedency

implementation 'androidx.core:core-ktx:1.1.0-alpha05'

to

implementation 'androidx.core:core-ktx:1.0.1'

So basically I am using stable release now.




回答6:


Latest update: android.core:1.2.0-alpha02 still does not have that method in ComponentActivity.getLifeCycle()

I update to latest version of android.core:

dependencies {
    // ...
    def core_version = "1.2.0-alpha04"
    implementation "androidx.core:core:$core_version"
    implementation "androidx.core:core-ktx:$core_version"
}

The latest version can be found in https://developer.android.com/jetpack/androidx/releases/core



来源:https://stackoverflow.com/questions/53899751/google-auth-migrating-to-androidx-signinhubactivity-does-not-implement-lifecycl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!