본문 바로가기
FireBase

Firebase - 안드로이드프로젝트와 파이어베이스 연동 (1)

by 봄석 2019. 1. 7.

Firebase - 안드로이드프로젝트와 파이어베이스 연동

Firebase 프로젝트와 안드로이드 프로젝트를 연결하기 위해서는 몇 가지 준비가 필요합니다.

안드로이드 프로젝트가 Firebase에 접근하기 위해서는 Firebase에서 발급한 증명서가 필요한데,

그 증명서는 "google-service.json" 파일 입니다. 이 파일 안에 Firebase에 필요한 여러 API 키가 담겨있습니다. 




google-service.json 파일과 안드로이드 프로젝트 연결하기

1) 안드로이드 프로젝트 생성

안드로이드 프로젝트를 생성한 뒤 build.gradle(Project)에 아래코드를 넣어줍니다.

buildscript {
    ....
    repositories {
        google()
        jcenter()
    }
    dependencies {
           ....
        classpath 'com.google.gms:google-services:4.2.0' // google-services plugin
    }
}
allprojects {
    repositories {
        ....
        maven {
            url "https://maven.google.com" //Google's Maven repository
        }
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}
 




build.gradle(Module:App)에 아래코드를 넣어줍니다.

apply plugin: 'com.android.application'
 
apply plugin: 'kotlin-android'
 
apply plugin: 'kotlin-android-extensions'
 
 
 
android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.qjatj.chobeombookinit"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
 
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    //Firebase
    implementation 'com.google.firebase:firebase-database:16.0.5'
    //Anko
    implementation "org.jetbrains.anko:anko:$anko_version"
}

apply plugin: "com.google.gms.google-services"



Sync Now를 클릭하면 "File google-service.json is missing." 에러가 발생합니다.


그럴경우 Firebase 콘솔로 이동하여 프로젝트를 만들고 

Google-service.json 파일을 받아와 넣어줘야 합니다.


https://console.firebase.google.com/   <-- Firebase 콘솔 경로


먼저 프로젝트를 만들어줍니다.

프로젝트가 만들어지면 [계속] 버튼을 눌러 이동합니다


톱니바퀴 모양을 클릭한 후 [프로젝트 설정]창으로 이동합니다.


그리고 [안드로이드 앱에 Firebase추가] 를 눌러줍니다.


Android Package 이름을 입력하고 앱 등록 버튼을 눌러줍니다.


클릭해서 Google-service.json을 다운받고 프로젝트 폴더에 넣어줍니다 .



2)라이브러리 설치(Gradle) -인증(Auth)

모듈수준의 gradle.builde 파일의 dependencies에 아래와 같이 입력하고 Sync Now를 클릭

1
implementation 'com.google.firebase:firebase-auth:16.0.5'
cs

3)라이브러리 사용하기 - 인증(Auth)
우선 Firebase 서버에 이메일 로그인 기능 사용 설정을 해줘야 이메일 로그인을 진행할 수 있습니다. Authentication -> 로그인방법 -> 이메일/ 비밀번호 기능을 사용 설정한 후 확인 클릭해줍니다.



FirebaseAuth는 Authentication를 관리하는 변수입니다. 메모리 절약을 위한 Singleton 패턴의 클래스이며 , 로그인 정보를 관리할 때 사용하는 메인 클래스 라고 생각하면 됩니다. 


FireBaseAuth의 기능들

 명칭

기능 

  createUserWithEmailAndPassword

 회원가입 

  signlnWithEmailAndPassword

 로그인 

  sendEmailVerification

 회원 가입한 이메일 유효 확인 

  updateEmail 

 회원 가입한 아이디 이메일 변경 

  updatePassword 

 회원 가입한 아이디 패스워드 변경 

  sendPasswordResetEmail 

 회원 가입한 비밀번호 재설정 

  delete 

 회원 가입한 아이디 삭제 

  reauthenticate 

 아이디 재 인증 



댓글