LoginSignup
2
3

More than 3 years have passed since last update.

kotlinでFirebaseAuthenticationを使ってみる

Last updated at Posted at 2021-04-16

はじめに

現在kotlinでチャットアプリを作成中でアカウント作成機能とログイン機能などをつけるためにFireBaseを使用しています。
本記事ではAndroidプロジェクトにFirebaseを追加する方法について記載しています。(未来の自分のためのメモです)
アプリ開発ほぼ初心者で現在勉強中またはバック側の知識が薄い自分みたいな人はメモ程度ですが何かの参考にしていただければ

開発環境

・使用言語:kotlin
・AndroidStudio 4.1.2
・Android Emulator (自分はm1 Macでの開発をしているので以下のものをダウンロードして使用)
https://github.com/google/android-emulator-m1-preview

Firebase側の設定

下記画面のプロジェクトの追加を選択し、手順に沿ってプロジェクトを作成する(自分が使っているFireBaseプロジェクトは右側のmessage app)
image.png

プロジェクトの作成が完了すると下記のような画面に移動する。
自分のチャットアプリではメールアドレスとパスワードで会員登録することを想定して開発を行なっているので 'メール/パスワード' が有効になっていますが、Google認証を使用したい場合などは 'Google' のステータスを有効にすることで使用できるかと思います。
image.png

こうして作成したアカウントは下記画像のように 'Users' に追加されます。
image.png

Firebaseの追加

プロジェクトの設定からマイアプリ内のgoogle-services.jsonのファイルをダウンロード

image.png

ダウンロードしたjsonファイルをAndroidStudioのapp内に格納し、アプリに Firebase SDK を追加するためgradleファイルで宣言。宣言方法は以下の通り(バージョンにもよる)

dependencies {
// ...
// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:26.8.0')
// When using the BoM, you don't specify versions in Firebase library dependencies
// Declare the dependency for the Firebase SDK for Google Analytics
implementation 'com.google.firebase:firebase-analytics-ktx'
// Declare the dependencies for any other desired Firebase products
// For example, declare the dependencies for Firebase Authentication and Cloud Firestore
implementation 'com.google.firebase:firebase-auth-ktx'
implementation 'com.google.firebase:firebase-firestore-ktx'
}

image.png

とここまで説明してきましたが公式のドキュメントにもAndroidプロジェクトにFirebaseを追加する方法が記載されています。
自分もこれを見て頑張って作業していたのですが、なかなか実装できず苦労しました。
これを読めば一撃でわかる人が多数だと思いますので参考にしてください
https://firebase.google.com/docs/android/setup?hl=ja#kotlin+ktx

まとめ

環境構築って嫌になっちゃっていつの間にか部屋の模様替えとか始めちゃいますよね・・・

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3