Android Firebase登录注册应用启动崩溃,求AndroidX配置修复方案
问题描述
使用Firebase开发了登录、注册及主界面Activity,启动应用时出现大量异常,判断并非语法问题而是配置问题。
注册界面代码
class RegistrationActivity : AppCompatActivity() { private lateinit var auth: FirebaseAuth //Email und Passwort Eingabefelder val regEmail: EditText = findViewById(R.id.registration_email) val regPassword: EditText = findViewById(R.id.registration_password) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_registration) auth = FirebaseAuth.getInstance() //Button der gedrückt wird zum Registrieren nach dem man seine Daten eingegeben hat val registration_button: Button = findViewById(R.id.registration_button) registration_button.setOnClickListener { //WENN Button gedrückt DANN rufe FUNKTION auf createUser() } //Zurück zum login val login_texView: TextView = findViewById(R.id.loginLink_textView) login_texView.setOnClickListener { val intent_registration_activiy = Intent(this, RegistrationActivity::class.java) startActivity(intent_registration_activiy) } } /** * Function: createUser * Eingegebene Email und Passwort werden in Strings umgewandelt * Danach wird überprüft ob die Email und Passwort Felder leer sind * IF Leer DANN Error und Focus * SONST: * User wird erstellt mit eingegebener email und passwort * Es wird danach überprüft ob die Registrierung erfolgreich war oder nicht * WENN erfolgreich: Toast, Weiterleitung zum Login * SONST: Toast * */ private fun createUser(){ val email: String = regEmail.text.toString() val password: String = regPassword.text.toString() if(TextUtils.isEmpty(email)){ regEmail.setError("E-mail kann nicht leer sein") regEmail.requestFocus() } else if(TextUtils.isEmpty(password)){ regPassword.setError("Passwort kann nicht leer sein") regPassword.requestFocus() } else{ auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener { task -> if (task.isSuccessful){ val successfull_toast = Toast.makeText(this, "Registrierung erfolgreich", Toast.LENGTH_LONG) successfull_toast.show() val intent_logIn_activity = Intent(this, LoginActivity::class.java) startActivity(intent_logIn_activity) } else { val unsuccsessfull_toast = Toast.makeText(this, "Ein Fehler ist aufgetreten " + task.exception?.message, Toast.LENGTH_LONG) } } } } }
登录界面代码
class LoginActivity : AppCompatActivity() { private lateinit var auth: FirebaseAuth val logEmail: EditText = findViewById(R.id.userLogInEmail) //email zum einloggen val logPassword: EditText = findViewById(R.id.userLogInPassword) //passwort zum einloggen val logButton: Button = findViewById(R.id.logIn_button) //Button zum einloggen val regLink: TextView = findViewById(R.id.registration_textView) //Zur Registration Activity override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_login) auth = FirebaseAuth.getInstance() //Login Button OnClick logButton.setOnClickListener { loginUser() } //Klicke und komme zur Registrations Activity regLink.setOnClickListener { val intent_regActivity = Intent(this, RegistrationActivity::class.java) startActivity(intent_regActivity) } } private fun loginUser(){ val email: String = logEmail.text.toString() val password: String = logPassword.text.toString() if (TextUtils.isEmpty(email)){ logEmail.setError("Email kann nicht leer sein") logEmail.requestFocus() } else if (TextUtils.isEmpty(password)){ logPassword.setError("Passwort kann nicht leer sein") logPassword.requestFocus() } else { auth.signInWithEmailAndPassword(email, password).addOnCompleteListener { task -> if(task.isSuccessful){ val successfull_toast = Toast.makeText(this, "Login erfolgreich", Toast.LENGTH_LONG) successfull_toast.show() val intent_mainActivity = Intent(this, MainActivity::class.java) startActivity(intent_mainActivity) } else { val unsuccessfull_toast = Toast.makeText(this, "Login nicht möglich " + task.exception?.message, Toast.LENGTH_LONG) unsuccessfull_toast.show() } } } } }
主界面代码
class MainActivity : AppCompatActivity() { private lateinit var auth: FirebaseAuth override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) auth = FirebaseAuth.getInstance() } /** * Wenn die App startet wird überprüft ob der User angemeldet ist oder nicht bzw. einen Account hat * IF NOT gehe zur Login-Acitiviy */ override fun onStart() { super.onStart() val user = auth.currentUser if(user == null){ val intent = Intent(this, LoginActivity::class.java) startActivity(intent) } } }
启动异常信息
Caused by: com.android.builder.errors.EvalIssueException: Configuration `:app:debugRuntimeClasspath` contains AndroidX dependencies, but the `android.useAndroidX` property is not enabled, which may cause runtime issues. Caused by: java.lang.RuntimeException: Manifest merger failed with multiple errors, see logs Caused by: java.lang.RuntimeException: Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.2.0-runtime (androidx.core:core:1.2.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Configuration `:app:debugRuntimeClasspath` contains AndroidX dependencies, but the `android.useAndroidX` property is not enabled, which may cause runtime issues. Set `android.useAndroidX=true` in the `gradle.properties` file and retry. The following AndroidX dependencies are detected: :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.browser:browser:1.4.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.collection:collection:1.1.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.browser:browser:1.4.0 -> androidx.annotation:annotation:1.1.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.browser:browser:1.4.0 -> androidx.concurrent:concurrent-futures:1.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.browser:browser:1.4.0 -> androidx.interpolator:interpolator:1.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.browser:browser:1.4.0 -> androidx.core:core:1.2.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.browser:browser:1.4.0 -> androidx.core:core:1.2.0 -> androidx.lifecycle:lifecycle-runtime:2.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.browser:browser:1.4.0 -> androidx.core:core:1.2.0 -> androidx.lifecycle:lifecycle-runtime:2.0.0 -> androidx.lifecycle:lifecycle-common:2.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.browser:browser:1.4.0 -> androidx.core:core:1.2.0 -> androidx.lifecycle:lifecycle-runtime:2.0.0 -> androidx.arch.core:core-common:2.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.browser:browser:1.4.0 -> androidx.core:core:1.2.0 -> androidx.versionedparcelable:versionedparcelable:1.1.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.fragment:fragment:1.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.fragment:fragment:1.0.0 -> androidx.legacy:legacy-support-core-ui:1.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.fragment:fragment:1.0.0 -> androidx.legacy:legacy-support-core-utils:1.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.fragment:fragment:1.0.0 -> androidx.legacy:legacy-support-core-utils:1.0.0 -> androidx.documentfile:documentfile:1.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.fragment:fragment:1.0.0 -> androidx.loader:loader:1.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.fragment:fragment:1.0.0 -> androidx.loader:loader:1.0.0 -> androidx.lifecycle:lifecycle-livedata:2.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.fragment:fragment:1.0.0 -> androidx.loader:loader:1.0.0 -> androidx.lifecycle:lifecycle-livedata:2.0.0 -> androidx.arch.core:core-runtime:2.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.fragment:fragment:1.0.0 -> androidx.loader:loader:1.0.0 -> androidx.lifecycle:lifecycle-livedata:2.0.0 -> androidx.lifecycle:lifecycle-livedata-core:2.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.fragment:fragment:1.0.0 -> androidx.lifecycle:lifecycle-viewmodel:2.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.localbroadcastmanager:localbroadcastmanager:1.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.fragment:fragment:1.0.0 -> androidx.legacy:legacy-support-core-utils:1.0.0 -> androidx.print:print:1.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.fragment:fragment:1.0.0 -> androidx.legacy:legacy-support-core-ui:1.0.0 -> androidx.customview:customview:1.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.fragment:fragment:1.0.0 -> androidx.legacy:legacy-support-core-ui:1.0.0 -> androidx.viewpager:viewpager:1.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.fragment:fragment:1.0.0 -> androidx.legacy:legacy-support-core-ui:1.0.0 -> androidx.coordinatorlayout:coordinatorlayout:1.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.fragment:fragment:1.0.0 -> androidx.legacy:legacy-support-core-ui:1.0.0 -> androidx.drawerlayout:drawerlayout:1.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.fragment:fragment:1.0.0 -> androidx.legacy:legacy-support-core-ui:1.0.0 -> androidx.slidingpanelayout:slidingpanelayout:1.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.fragment:fragment:1.0.0 -> androidx.legacy:legacy-support-core-ui:1.0.0 -> androidx.swiperefreshlayout:swiperefreshlayout:1.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.fragment:fragment:1.0.0 -> androidx.legacy:legacy-support-core-ui:1.0.0 -> androidx.asynclayoutinflater:asynclayoutinflater:1.0.0 :app:debugRuntimeClasspath -> com.google.firebase:firebase-auth:21.0.6 -> androidx.fragment:fragment:1.0.0 -> androidx.legacy:legacy-support-core-ui:1.0.0 -> androidx.cursoradapter:cursoradapter:1.0.0
请问该如何解决此问题?
解决方案
一、解决AndroidX与Support库冲突问题
异常核心是项目同时引入AndroidX和旧版Support库,且未开启AndroidX支持,按以下步骤处理:
- 修改gradle.properties配置
在项目根目录的gradle.properties文件中添加或修改以下两行:
android.useAndroidX=true android.enableJetifier=true
android.useAndroidX=true:开启项目对AndroidX的支持android.enableJetifier=true:自动将第三方依赖中的旧版Support库引用转换为AndroidX版本
迁移项目到AndroidX
打开Android Studio,依次点击Refactor->Migrate to AndroidX,按照向导完成迁移操作。这会自动替换代码、布局文件中所有旧版Support库的引用为AndroidX对应类。清理依赖
打开app/build.gradle文件,移除所有com.android.support开头的依赖,替换为对应的AndroidX依赖。例如:
- 旧版
com.android.support:appcompat-v7替换为androidx.appcompat:appcompat:1.6.1(版本号可根据需求选择最新稳定版)
二、修复代码逻辑错误
在RegistrationActivity的返回登录按钮点击事件中,错误地跳转到了自身,需修改为:
// 原错误代码 // val intent_registration_activiy = Intent(this, RegistrationActivity::class.java) // startActivity(intent_registration_activiy) // 修改后代码 val intent_login_activity = Intent(this, LoginActivity::class.java) startActivity(intent_login_activity)
三、后续验证操作
- 清理项目缓存:点击
File->Invalidate Caches / Restart,选择Invalidate and Restart - 重新构建项目:点击
Build->Rebuild Project - 确保Firebase Auth依赖使用最新稳定版,避免版本兼容问题
内容的提问来源于stack exchange,提问作者KayraOz
相关产品推荐
相关产品推荐

