Kotlinを使ってFirebaseでログイン処理をしようと思っています。
下記のコードを試しましたが、常にログインが失敗します。
FireBase側の設定では、メール認証を有効にしています。
同様の問題の知見のある方、ご教示のほどよろしくお願い致します。
エラーメッセージ
2022-01-16 11:20:38.271 3895-3895/com.example.myapplication D/Create: com.google.firebase.FirebaseApiNotAvailableException: API: InternalFirebaseAuth.FIREBASE_AUTH_API is not available on this device.
Kotlin
1package com.example.myapplication 2 3import android.os.Bundle 4import android.util.Log 5import androidx.appcompat.app.AppCompatActivity 6import android.widget.Button 7import android.widget.EditText 8import android.widget.Toast 9import com.google.firebase.FirebaseApp 10import com.google.firebase.auth.FirebaseAuth 11import com.google.firebase.database.DatabaseReference 12import com.google.firebase.database.FirebaseDatabase 13 14 15class MainActivity : AppCompatActivity() { 16 17 private lateinit var auth: FirebaseAuth 18 19 override fun onCreate(savedInstanceState: Bundle?) { 20 super.onCreate(savedInstanceState) 21 setContentView(R.layout.activity_main) 22 23 auth = FirebaseAuth.getInstance() 24 25 val buttonSignUp = findViewById<Button>(R.id.SignUpButton) 26 val buttonLogin = findViewById<Button>(R.id.LoginButton) 27 28 buttonSignUp.setOnClickListener { 29 30 val emailEditText = findViewById<EditText>(R.id.emailEditText) 31 val emailText = emailEditText.text.toString() 32 33 val passEditText = findViewById<EditText>(R.id.passEditText) 34 val passText = passEditText.text.toString() 35 36 37 auth.createUserWithEmailAndPassword(emailText, passText) 38 .addOnCompleteListener(this) { task -> 39 if (task.isSuccessful) { 40 Toast.makeText( 41 baseContext, "SignUp 成功", 42 Toast.LENGTH_SHORT 43 ).show() 44 } else { 45 Toast.makeText( 46 baseContext, "SignUp 失敗", 47 Toast.LENGTH_SHORT 48 ).show() 49 Log.d("Create",task.exception.toString()) 50 } 51 } 52 } 53 54 buttonLogin.setOnClickListener { 55 56 val emailEditText = findViewById<EditText>(R.id.emailEditText) 57 val emailText = emailEditText.text.toString() 58 59 val passEditText = findViewById<EditText>(R.id.passEditText) 60 val passText = passEditText.text.toString() 61 62 auth.signInWithEmailAndPassword(emailText, passText) 63 .addOnCompleteListener(this) { task -> 64 if (task.isSuccessful) { 65 Toast.makeText( 66 baseContext, "Login 成功", 67 Toast.LENGTH_SHORT 68 ).show() 69 } else { 70 Toast.makeText( 71 baseContext, "Login 失敗", 72 Toast.LENGTH_SHORT 73 ).show() 74 Log.d("Login",task.exception.toString()) 75 } 76 77 } 78 } 79 80 } 81}
mctivity_main.xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 xmlns:app="http://schemas.android.com/apk/res-auto" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".MainActivity"> 9 10 <Button 11 android:text="string/signup" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:id="@+id/SignUpButton" android:layout_marginTop="13dp" 15 app:layout_constraintTop_toBottomOf="@+id/guideline" app:layout_constraintStart_toStartOf="parent" 16 android:layout_marginStart="30dp"/> 17 <Button 18 android:text="string/login" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:id="@+id/LoginButton" android:layout_marginTop="13dp" 22 app:layout_constraintTop_toBottomOf="@+id/guideline" app:layout_constraintEnd_toEndOf="parent" 23 android:layout_marginEnd="30dp"/> 24 <TextView 25 android:text="e_mail" 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content" 28 android:id="@+id/emailLabel" app:layout_constraintTop_toTopOf="parent" 29 android:layout_marginTop="88dp" app:layout_constraintEnd_toStartOf="@+id/emailEditText" 30 android:layout_marginEnd="14dp" app:layout_constraintStart_toStartOf="parent" 31 android:layout_marginStart="40dp" android:labelFor="@+id/emailEditText"/> 32 <TextView 33 android:text="password" 34 android:layout_width="wrap_content" 35 android:layout_height="wrap_content" 36 android:id="@+id/passLabel" android:layout_marginTop="32dp" 37 app:layout_constraintTop_toBottomOf="@+id/emailLabel" app:layout_constraintStart_toStartOf="parent" 38 android:layout_marginStart="40dp" android:labelFor="@+id/passEditText"/> 39 <EditText 40 android:layout_width="wrap_content" 41 android:layout_height="wrap_content" 42 android:inputType="textEmailAddress" 43 android:ems="10" 44 android:id="@+id/emailEditText" 45 app:layout_constraintBottom_toBottomOf="@+id/emailLabel" 46 app:layout_constraintTop_toTopOf="@+id/emailLabel" app:layout_constraintEnd_toEndOf="parent" 47 android:layout_marginEnd="58dp" android:autofillHints=""/> 48 <EditText 49 android:layout_width="wrap_content" 50 android:layout_height="wrap_content" 51 android:inputType="textPassword" 52 android:ems="10" 53 android:id="@+id/passEditText" 54 app:layout_constraintTop_toTopOf="@+id/passLabel" app:layout_constraintBottom_toBottomOf="@+id/passLabel" 55 app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="58dp" android:autofillHints=""/> 56 <androidx.constraintlayout.widget.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content" 57 android:id="@+id/guideline" app:layout_constraintGuide_begin="204dp" 58 android:orientation="horizontal"/> 59</androidx.constraintlayout.widget.ConstraintLayout>
plugins { id 'com.android.application' id 'kotlin-android' } android { compileSdk 31 defaultConfig { applicationId "com.example.myapplication" minSdk 23 targetSdk 31 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } } dependencies { implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.appcompat:appcompat:1.4.0' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.2' implementation 'com.android.support:support-v4:28.0.0' // この行を追加 implementation 'com.android.support:support-media-compat:28.0.0' // この行を追加 implementation 'com.google.firebase:firebase-auth:16.0.1' // この行を追加 implementation 'com.google.firebase:firebase-database:16.0.1' // この行を追加 testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' } apply plugin: 'com.google.gms.google-services'
回答1件
あなたの回答
tips
プレビュー