[Firebase]Android Studioでbuild.gradleにFirebaseライブラリ10.0.1を設定した時のエラー対処


はじめに

最近はじめたAndroidアプリの開発に、噂のFirebaseを使い始めています。手始めに、Firebase libraryをガイダンスに従って色々試してみたいと、FirebaseのHPを確認したのですが、libraryのversionが

日本語HPだと、9.6.1(https://firebase.google.com/docs/android/setup
英語HPだと、10.0.1(https://firebase.google.com/docs/analytics/android/start/

と示されていました。両方試すと、10.0.1でエラーが…。
どうせやるなら折角なので最新版で設定しておきたいと思ったのですが、ちょっと詰まってしまったので、メモとして簡単に書いておくことにしました。

環境

  • MacOS 10.11.6
  • Android Studio: ver 2.2.3
  • Firebaseアカウント作成済み
  • Firebase consoleで、AndroidアプリへFirebaseを追加済み

Firebaseの設定に関しては、こちらを参考にさせて頂きました。
1. Androidでfirebaseを利用してみた
2. Android Studio 2.2 PreviewでFirebase Analyticsを利用してみる

build.gradleの設定

とりあえず、Firebase日本語版公式ページ-Setupに書いてあるように、build.gradleを設定。

build.gradle
プロジェクト直下の build.gradle

dependencies {
        :
        // <-- 追記部分 begin --> Add this line for firebase 
        classpath 'com.google.gms:google-services:3.0.0'
        // <-- 追記部分 end -->
        :
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
app/build.gradle
appフォルダ下の build.gradle
dependencies {
    :
    :
    // <-- 追記部分 begin --> Add this line in the last of depencencies for firebase 
    compile 'com.google.firebase:firebase-core:10.0.1'
    compile 'com.google.firebase:firebase-database:10.0.1'
    // <-- 追記部分 end -->  
}

// <-- 追記部分 begin --> Add to the bottom of the file (the revision is mine)
apply plugin: 'com.google.gms.google-services'
// <-- 追記部分 end -->

以上。
これでSyncronizeして、appを動かしてみるのですが…。
エラーが続々と出てきました。

エラー内容と対応

1. dependenciesのconfigurationが解決できない。

具体的なエラー内容はこんな感じ。

ErrorLog
Error:A problem occurred configuring project ':app'. 
Could not resolve all dependencies for configuration ':app:_debugApkCopy'.
Could not find com.google.firebase:firebase-core:10.0.1. Required by: DGApp:app:unspecified

このエラーに関しては、こちらのstackoverflowで解決済み。
Unable to update the Firebase dependency(com.google.firebase)

内容を要約すると、Android Studioで Google Play Services と Google Repository を Standalone SDK Manager から Update してあげれば良いらしい。
Update方法の詳細については、先程示したstackoverflowの回答どおりにすれば問題なくできる。
これで再度Syncronizeするわけですが、、、また異なるエラーが発生。

2. E/AndroidRuntime: FATAL EXCEPTION: main

FATALときた。笑
具体的なエラー内容は、

ErrorLog
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.appname, PID: 5652
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appname/com.appname.MainActivity}: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class android.support.design.widget.CollapsingToolbarLayout
                      at :
                      at :
                      at :

見たことがないエラーだったので、???としばらくハマってしまいましたが、Error inflating class android.support.design.widget.CollapsingToolbarLayoutこちらの解決法を適応してエラーが解消されました。原因は、support libraryのバージョンのよう。
support libraryとcompileSdkVersion、targetSdkVersionを変更しました。

app/build.gradle
    :
    :
android {
    compileSdkVersion 25 //<-- 23から25に変更 -->
    buildToolsVersion "21.1.2"
    defaultConfig {
        :
        minSdkVersion 15
        targetSdkVersion 25 //<-- 23から25に変更 -->
        :
    }
    buildTypes {
        release {
            :
            :
        }
    }
}

dependencies {
    :
    :
    //compile 'com.android.support:appcompat-v7:23.4.0'
    //compile 'com.android.support:design:23.4.0'
    // <-- 上記compile部分を、下記に変更 begin -->
    compile 'com.android.support:appcompat-v7:25.1.0' // <-最新版に変更
    compile 'com.android.support:design:25.1.0' //<-最新版に変更
    // <-- 上記compile部分を、下記に変更 end -->
    testCompile 'junit:junit:4.12'
    // the compile command is to use firebase function 'Analytics' and 'Realtime Database' (the revision is mine)
    compile 'com.google.firebase:firebase-core:10.0.1'
    compile 'com.google.firebase:firebase-database:10.0.1'
}

// Add to the bottom of the file (the revision is mine)
apply plugin: 'com.google.gms.google-services'

これでコンパイル等、色々通って無事にFirebaseが使えるようになりました。
良かったよかった :)

参考