升级targetSDK至31(Android 12)时PendingIntent参数缺失问题求助
修复React Native升级targetSDK至31后react-native-sms-retriever的PendingIntent问题
Android 12(targetSDK 31)强制要求所有PendingIntent必须指定FLAG_IMMUTABLE或FLAG_MUTABLE标记,而你使用的react-native-sms-retriever包中,旧版Auth.CredentialsApi.getHintPickerIntent()生成的PendingIntent未携带这些标记,导致报错。
解决方案
直接替换旧版Google Play服务API调用,改用新版Credentials.getHintPickerIntent()并手动指定标记,修改PhoneNumberHelper.java中的代码如下:
// 移除原有的GoogleApiClient相关代码 HintRequest request = new HintRequest.Builder() .setPhoneNumberIdentifierSupported(true) // 保留你原来的其他request配置 .build(); PendingIntent pendingIntent; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { // Android 12及以上必须指定FLAG_IMMUTABLE(此处不需要可变Intent,用IMMUTABLE更安全) pendingIntent = Credentials.getHintPickerIntent(context, request, PendingIntent.FLAG_IMMUTABLE); } else { // 低版本无需标记 pendingIntent = Credentials.getHintPickerIntent(context, request); } try { activity.startIntentSenderForResult(pendingIntent.getIntentSender(), REQUEST_PHONE_NUMBER_REQUEST_CODE, null, 0, 0, 0); } catch (IntentSender.SendIntentException e) { promiseReject(SEND_INTENT_ERROR_TYPE, SEND_INTENT_ERROR_MESSAGE); callAndResetListener(); }
必要依赖更新
确保app/build.gradle中play-services-auth依赖版本不低于19.2.0(新版API要求),建议更新到最新稳定版:
implementation 'com.google.android.gms:play-services-auth:20.7.0'
为什么之前的方法无效
你添加的androidx.work:work-runtime和play-services-base依赖与当前问题无关,问题核心是旧版Auth.CredentialsApi生成的PendingIntent未满足Android 12的标记强制要求,而非依赖缺失。
内容的提问来源于stack exchange,提问作者Rizwan Ahmed Shivalli
相关产品推荐
相关产品推荐

