You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android真机调用createUserWithEmailAndPassword无法在Firebase创建用户

Firebase Auth在Android真机创建用户失败问题

我正在学习Firebase与Kotlin,以下创建用户的代码在Android模拟器上可成功创建用户,但在有网络连接的Android真机上无法创建用户。

示例代码

class MainActivity : AppCompatActivity() {
    private lateinit var auth: FirebaseAuth
    fun signup(){
        val email = "john.doe6@yahoo.com"
        val password = "1234567890"
        auth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this) { task ->
                Log.d("Signup",task.toString())
                if (task.isSuccessful) {
                    Log.d("Signup", "createUserWithEmail:success")
                } else {
                    Log.d("Signup", "createUserWithEmail:failure", task.exception)
                    Toast.makeText(
                        baseContext,
                        "Authentication failed.",
                        Toast.LENGTH_SHORT,
                    ).show()
                }
            }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        auth = Firebase.auth
        signup()
    }
}

Logcat输出

Creating user with john.doe6@yahoo.com with empty reCAPTCHA token
Connection attempt already in progress
Ignoring header X-Firebase-Locale because its value was null.
 tagSocket(80) with statsTag=0xffffffff, statsUid=-1
Activity resumed, time: 24954722
Tag Manager is not found and thus will not be used
Connection attempt already in progress
Connection attempt already in progress
Screen exposed for less than 1000 ms. Event not sent. time: 21
Connection attempt already in progress
Activity paused, time: 24954743
Connected to remote service
Processing queued up service tasks: 6
Application going to the background
Application backgrounded at: timestamp_millis: 1686640215952
Inactivity, disconnecting from the service

AndroidManifest.xml内容

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:theme="@style/Theme.Auth"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

问题分析与解决步骤

从Logcat中的Creating user with john.doe6@yahoo.com with empty reCAPTCHA token日志可以确定,核心问题是reCAPTCHA验证未完成,导致用户创建请求失败。以下是针对性的解决方法:

  1. 核对Firebase项目配置

    • 登录Firebase控制台,确认Auth模块已启用邮箱/密码登录方式。
    • 检查项目设置中是否添加了真机调试环境的SHA-1和SHA-256指纹(模拟器使用Android Studio默认指纹,真机需要本地开发环境的指纹)。
  2. 修复reCAPTCHA验证流程

    • 确保Firebase Auth依赖为最新版本,在build.gradle(Module level)中更新:
      implementation 'com.google.firebase:firebase-auth-ktx:22.3.1'
      
    • 将signup()方法的调用从onCreate()移至onStart()或用户主动触发(比如按钮点击),避免Activity生命周期未完全就绪时中断验证流程。
  3. 补充错误监听获取详细信息

    • 在创建用户的代码中添加addOnFailureListener,捕获更具体的异常原因:
      auth.createUserWithEmailAndPassword(email, password)
          .addOnCompleteListener(this) { /* 现有逻辑 */ }
          .addOnFailureListener { exception ->
              Log.e("SignupError", exception.message ?: "Unknown authentication error")
          }
      
  4. 排查网络与缓存问题

    • 确认真机网络能正常访问Firebase服务器,可通过浏览器测试https://firebase.google.com的连通性。
    • 清除应用缓存后卸载重装,避免旧配置残留影响验证流程。

内容的提问来源于stack exchange,提问作者user1927603

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 19:24:56