Android中getCredentialAsync错误抛出NoCredentialException问题求助
CredentialManager抛出异常类型不符的问题
我正在使用Java开发一款Android游戏,通过CredentialManager提示用户选择Google账号。代码运行正常,但在以下两种场景下,系统抛出的是NoCredentialException,而非更合适的异常(如ConnectionException或通用InterruptedException):
- 出现连接问题时(例如超时、地铁内无网络);
- 调用
getCredentialAsync后按下设备Home键,随后返回应用(整个操作仅需数秒)。
相关代码
public void signIn_credentialManager() { String nonce = randomNonce(); GetCredentialRequest request; googleIdOption = new GetGoogleIdOption.Builder() .setFilterByAuthorizedAccounts(false) .setServerClientId("...") .setAutoSelectEnabled(true) .setNonce(nonce) .build(); request = new GetCredentialRequest.Builder() .addCredentialOption(googleIdOption) .build(); log(LOG_TAG, "credentialManager.getCredentialAsync"); credentialManager.getCredentialAsync( context, request, new CancellationSignal(), executor, new CredentialManagerCallback<GetCredentialResponse, GetCredentialException>() { @Override public void onResult(GetCredentialResponse result) { log(LOG_TAG, "CredentialManagerCallback onResult"); } @Override public void onError(@NonNull GetCredentialException e) { log(LOG_TAG, "CredentialManagerCallback Error!"); e.printStackTrace(); if(e instanceof NoCredentialException) { // HERE } } } ); }
Android模块build.gradle配置
plugins { id "org.jetbrains.kotlin.android" version "1.9.24" apply false } apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' apply plugin: 'com.google.firebase.crashlytics' android { compileSdk 35 sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs('src/main/java') aidl.srcDirs('src/main/java') renderscript.srcDirs('src/main/java') res.srcDirs('res') assets.srcDirs('../assets') jniLibs.srcDirs('libs') } } packagingOptions { resources.with { excludes += ['META-INF/robovm/ios/robovm.xml', 'META-INF/DEPENDENCIES.txt', 'META-INF/DEPENDENCIES', 'META-INF/dependencies.txt', '**/*.gwt.xml'] pickFirsts += ['META-INF/LICENSE.txt', 'META-INF/LICENSE', 'META-INF/license.txt', 'META-INF/LGPL2.1', 'META-INF/NOTICE.txt', 'META-INF/NOTICE', 'META-INF/notice.txt'] } } defaultConfig { applicationId 'app.mywebsite.myappname' minSdkVersion 24 targetSdkVersion 35 versionCode 1 versionName "1.0.0 test" } namespace "app.mywebsite.myappname" compileOptions { sourceCompatibility "11" targetCompatibility "11" coreLibraryDesugaringEnabled true } buildTypes { debug { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' debuggable true } release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' debuggable false } } buildFeatures { buildConfig = true } } repositories { google() } configurations { natives } dependencies { coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.5' implementation "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion" implementation project(':core') natives "com.badlogicgames.gdx:..." implementation 'androidx.appcompat:appcompat:1.7.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation platform('com.google.firebase:firebase-bom:33.10.0') implementation 'com.google.firebase:firebase-crashlytics:19.4.1' implementation 'com.google.firebase:firebase-analytics:22.3.0' implementation "androidx.credentials:credentials:1.5.0" implementation "androidx.credentials:credentials-play-services-auth:1.5.0" implementation "com.google.android.libraries.identity.googleid:googleid:1.1.1" implementation "com.google.android.gms:play-services-games-v2:20.1.2" implementation 'com.google.android.gms:play-services-auth:21.3.0' } tasks.register('run', Exec) { def path def localProperties = project.file("../local.properties") if (localProperties.exists()) { Properties properties = new Properties() localProperties.withInputStream { instr -> properties.load(instr) } def sdkDir = properties.getProperty('sdk.dir') if (sdkDir) { path = sdkDir } else { path = "$System.env.ANDROID_SDK_ROOT" } } else { path = "$System.env.ANDROID_SDK_ROOT" } def adb = path + "/platform-tools/adb" commandLine "$adb", 'shell', 'am', 'start', '-n', 'app.mywebsite.myappname/app.mywebsite.myappname.android.MainActivity' } eclipse.project.name = appName + "-android"
设备信息
- 设备:三星Galaxy M53 5G
- 系统:Android 14
- Google Play系统更新:2024年5月1日
- One UI版本:6.0
疑问点
- 为何会抛出错误的异常类型?
- 获取可用账号是否需要发起网络请求?按常理它应仅读取本地设备数据,但恢复网络后重试即可正常运行,这似乎矛盾。
- 应用切换后台后是否应恢复该操作?
我需要正确处理所有错误场景,但异常类型错误导致无法实现。请问我是否存在操作失误?
补充说明
无法通过getCause()获取根源异常,因为该NoCredentialException并非由其他异常引发,栈信息如下:
androidx.credentials.exceptions.NoCredentialException: No credentials available at androidx.credentials.internal.ConversionUtilsKt.toJetpackGetException(ConversionUtils.kt:82) at androidx.credentials.CredentialProviderFrameworkImpl.convertToJetpackGetException$credentials_release(CredentialProviderFrameworkImpl.kt:295) at androidx.credentials.CredentialProviderFrameworkImpl$onGetCredential$outcome$2.onError(CredentialProviderFrameworkImpl.kt:162) at androidx.credentials.CredentialProviderFrameworkImpl$onGetCredential$outcome$2.onError(CredentialProviderFrameworkImpl.kt:150) at android.credentials.CredentialManager$GetCredentialTransport.lambda$onError$2(CredentialManager.java:694) at android.credentials.CredentialManager$GetCredentialTransport.$r8$lambda$DFYUIuyfauIGF(Unknown Source:0) at android.credentials.CredentialManager$GetCredentialTransport$$ExternalSyntheticLambda2.run(Unknown Source:6) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644) at java.lang.Thread.run(Thread.java:1012)
内容的提问来源于stack exchange,提问作者user2342558
相关产品推荐
相关产品推荐

