새소식

Android/Firebase

[Firebase] Android 프로젝트 연동하기

  • -

이번 글에서는 Firebase와 안드로이드 프로젝트를 연동하는 것을 보여드리겠습니다. 


1. Firebase 콘솔 접속 

https://console.firebase.google.com/?hl=ko

 

로그인 - Google 계정

이메일 또는 휴대전화

accounts.google.com

 

 

2. 프로젝트 생성

Firebase 프로젝트 생성

 

 

3. 안드로이드 프로젝트 선택 

중앙에 안드로이드 마커를 선택하시면 됩니다.

Firebase 안드로이드 프로젝트 선택

 

 

 

4. 정보 입력 

패키지 이름은 여러분의 안드로이드 스튜디오에서 확인하실 수 있습니다. 

 

SHA-1값 구하기

1. 안드로이드 스튜디오에서 우측 상단 Gradle을 클릭합니다. 

2. 만약 1번 그림처럼 Gradle이 열린다면

File - Settings - Experimental 에 가서 Do not build Gradle task list during Gradle sync 체크박스를 해제하고

File - Sync Project with Gradle Files 를 한번 실행시켜 주면 됩니다. 

 

3. signingReport 더블클릭하면 하단 콘솔에 SHA-1 값을 확인하실 수 있습니다. 

 

 

 

5. google-services.json 다운로드 

"google-service.json"파일은 안드로이드 프로젝트가 Firebase에 접근하기 위해서 필요합니다.
이 파일은 Firebase에서 발급하는 증명서로 이 안에는 Firebase에 필요한 여러 API 키가 담겨있습니다.

 

다운로드 받은 "google-service.json"파일을 다음과 같이 안드로이드 앱 모듈 루투 디렉터리에 넣어주시면 됩니다. 

 

 

 

6. Firebase SDK 추가하기 

프로젝트와 모듈단위의 gradle 파일에 구글 플러그인을 buildscript 종속성으로 추가합니다. 

build.gradle(Project.appname)
buildscript {

    repositories {
      // Make sure that you have the following two repositories
      google()  // Google's Maven repository
      mavenCentral()  // Maven Central repository
    }

    dependencies {
      ...

      // Add the dependency for the Google services Gradle plugin
      classpath 'com.google.gms:google-services:4.3.14'
    }
}

allprojects {
  ...

  repositories {
    // Make sure that you have the following two repositories
    google()  // Google's Maven repository
    mavenCentral()  // Maven Central repository
  }
}

 


build.gradle(Module.app)
plugins {
    id 'com.android.application'

    // Add the Google services Gradle plugin
    id 'com.google.gms.google-services'
    ...
}


....
....
....


dependencies {
  // ...

  // Import the Firebase BoM
  implementation platform('com.google.firebase:firebase-bom:31.1.1')

  // When using the BoM, you don't specify versions in Firebase library dependencies

  // Add the dependency for the Firebase SDK for Google Analytics
  implementation 'com.google.firebase:firebase-analytics'

  // TODO: Add the dependencies for any other Firebase products you want to use
  // See https://firebase.google.com/docs/android/setup#available-libraries
  // For example, add the dependencies for Firebase Authentication and Cloud Firestore
  implementation 'com.google.firebase:firebase-auth'
  implementation 'com.google.firebase:firebase-firestore'
}

 

 

[TIP]

여기서 전 Arctic Fox버전이기 때문에 build.gradle(Project.appname) 파일에 위 코드를 넣을 수가 없었습니다. 
따라서  settings.gradle( )파일에 코드를 다음과 같이 작성했습니다. 

pluginManagement{
  .....
}


buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.gms:google-services:4.3.14'
    }
}


dependencyResolutionManagement {
	.....
}

 

 

 

 

7. gradle 파일을 변경했다면 안드로이드 스튜디오 상단의 Sync Now를 클릭합니다. 

안드로이드 스튜디오 Sync now

 

 

 

 

8. 연동을 완료하면 다음과 같이 연동이 되었음을 확인할 수 있습니다. 

 

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.