I have written How To Add Firebase To Android (with existing Google Project) back in Aug 2017, but I will write an updated guide for new project here.
NOTE: the documentation mentioned prerequisites of Android 4.0 (Ice Cream Sandwich) or newer, and Google Play services 15.0.0 or higher.
In Android Studio, goto Tools -> Firebase -> Analytics -> Log an Analytics Event.

NOTE: It might seems strange to add Analytics when we want just want to add Firebase, but going through the Analytics wizard will setup Firebase.
Click Connect your app to Firebase. You can Create new Firebase project or Choose an existing Firebase or Google project.

Upon success, you should see Connected for Step 1.

NOTE: A file named google-services.json should exist in app/google-services.json. You can't see it in Android view (left project files panel in Android Studio), select Project Files view instead.
NOTE: It seems like the Android Studio wizard automatically create project id (e.g. projectname-eaa23) and storage_bucket (e.g. projectname-eaa23.appspot.com). I believe there is no way change these once created. If the project id matter to you, you might want to explore Manually Add Firebase (I didn't tried this method before) or create your Google Cloud Project first then import the project into Firebase.
Click Add Analytics to your app for Step 2.

The following configrations shall be added:
build.gradle (project-level)
Add Firebase Gradle buildscript dependency
classpath 'com.google.gms:google-services:3.1.1'
app/build.gradle
Add Firebase plugin for Gradle
apply plugin: 'com.google.gms.google-services'
build.gradle will include these new dependencies:
compile 'com.google.firebase:firebase-core:11.8.0'
Edit project build.gradle (the wizard should have auto inserted the following).
buildscript {
// ...
dependencies {
// https://bintray.com/android/android-tools/com.google.gms.google-services/
classpath 'com.google.gms:google-services:3.2.1'
}
}Edit module/app build.gradle (the wizard should have auto inserted the following).
dependencies {
// https://firebase.google.com/support/release-notes/android
implementation 'com.google.firebase:firebase-core:12.0.1'
}
apply plugin: 'com.google.gms.google-services'NOTE: I believe classpath 'com.google.gms:google-services and apply plugin: 'com.google.gms.google-services' is required for all Firebase components.
NOTE: com.google.firebase:firebase-core is a recommended alias for the com.google.firebase:firebase-analytics library. I suspect com.google.firebase:firebase-core is optional if you are not using Firebase Analytics, but I might be wrong or things might change in the future.
NOTE: check Firebase Android Latest Releases