Android Google SignIn getCredentialAsync方法参数报错及解决
Android CredentialManager Google登录:getCredentialAsync方法不可识别问题解决
问题背景
实现Android Google登录功能时,在app模块的build.gradle中引入以下依赖:
implementation "androidx.credentials:credentials:1.3.0" implementation "androidx.credentials:credentials-play-services-auth:1.3.0" implementation "com.google.android.libraries.identity.googleid:googleid:1.1.1"
在MainActivity.java中编写startGoogleLogin方法,当判断SDK版本≥Android 14(UpsideDownCake)时,创建CredentialManager实例并构建GetCredentialRequest,调用getCredentialAsync方法时出现方法不可识别的报错。即使确认参数类型与方法签名完全匹配,问题仍未解决,最终通过将CancellationSignal参数设为null解决了问题。
修正后的完整代码
import androidx.core.os.CancellationSignal; import androidx.credentials.Credential; import androidx.credentials.CredentialManager; import androidx.credentials.CredentialManagerCallback; import androidx.credentials.CustomCredential; import androidx.credentials.GetCredentialRequest; import androidx.credentials.GetCredentialResponse; import androidx.credentials.PasswordCredential; import androidx.credentials.PublicKeyCredential; import androidx.credentials.exceptions.GetCredentialException; import com.google.android.libraries.identity.googleid.GetGoogleIdOption; import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.security.SecureRandom; import android.content.Context; import android.util.Log; public void startGoogleLogin() { String serverClientId = ""; // 替换为你的服务器客户端ID // 生成Nonce用于安全验证 SecureRandom secureRandom = new SecureRandom(); byte[] randomBytes = new byte[24]; // 24字节=192位 secureRandom.nextBytes(randomBytes); String nonce = java.util.Base64.getUrlEncoder().withoutPadding().encodeToString(randomBytes); // 构建Google登录选项 GetGoogleIdOption googleIdOption = new GetGoogleIdOption.Builder() .setFilterByAuthorizedAccounts(false) .setServerClientId(serverClientId) .setAutoSelectEnabled(true) .setNonce(nonce) .build(); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { CredentialManager credentialManager = CredentialManager.create(this); Context context = getApplicationContext(); GetCredentialRequest request = new GetCredentialRequest.Builder() .addCredentialOption(googleIdOption) .build(); Executor executor = Executors.newSingleThreadExecutor(); CredentialManagerCallback<GetCredentialResponse, GetCredentialException> callback = new CredentialManagerCallback<GetCredentialResponse, GetCredentialException>() { @Override public void onResult(GetCredentialResponse result) { Log.v(TAG, "获取凭证成功: " + result.getCredential().toString()); } @Override public void onError(GetCredentialException e) { Log.v(TAG, "获取凭证失败: " + e.toString()); } }; try { // 将CancellationSignal参数设为null解决方法不可识别问题 credentialManager.getCredentialAsync(context, request, null, executor, callback); } catch (GetCredentialException e) { Log.v(TAG, "获取凭证异常: " + e.toString()); } } }
关键说明
- 原代码中创建
CancellationSignal实例并传入方法是触发报错的原因,即便参数类型匹配,部分环境下仍会出现方法识别异常。 - 传入
null作为CancellationSignal参数是官方允许的合法用法,不会影响功能正常运行,同时彻底解决了方法不可识别的报错。
内容的提问来源于stack exchange,提问作者John
相关产品推荐
相关产品推荐

