ビルド環境の設定

GCM を使用するには、プロジェクトから Google Play services が提供するライブラリーを使用できるように設定する必要があります。

ライブラリーのダウンロード

まず、Android Studio から Google Play services のライブラリー(Google Repository)をダウンロードします。

Android Studio 上部のツールバーから "SDK Manager" を起動します。

SDK の設定画面が開きます。"SDK Tools" タブを開いて、一覧から "Google Repository" を選択し、"Apply" ボタンで反映します。ダウンロードが開始されてライブラリーが開発環境にインストールされます。

すでにインストールされている場合は、そのまま閉じてください。

build.gradle (Project) の設定

プロジェクトのルートにある build.gradle を開き、buildscript / dependencies に classpath 'com.google.gms:google-services:3.0.0' を追加します。

追加後は下記のようになります。

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.google.gms:google-services:3.0.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
   ...

3.0.0 はドキュメント更新時点で動作を確認できている版です。後方互換があるため、これより新しいバージョンを指定しても問題なく動作するはずですが、下記の play-services-gcm との組み合わせでエラーになることがあります。+ を指定するとビルド時点での最新版が反映されますが、予期しない更新が入る可能性もあるため、ご注意ください。

build.gradle (Module) の設定

ビルド対象のモジュール直下(デフォルトでは app ディレクトリの直下)にある build.gradle を開き、以下を編集します。

  • dependencies セクションに implementation 'com.google.android.gms:play-services-gcm:9.0.2' または compile 'com.google.android.gms:play-services-gcm:9.0.2' を追加
  • ルートレベルに apply plugin: 'com.google.gms.google-services' を追加

追加する内容は Android Studio のバージョンによって異なります。追加後は下記のようになります。

  • apply plugin: 'com.android.application'
    
    android {
        ...
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        ...
        implementation 'com.google.android.gms:play-services-gcm:9.0.2'
    }
    
    apply plugin: 'com.google.gms.google-services'
  • apply plugin: 'com.android.application'
    
    android {
        ...
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        ...
        compile 'com.google.android.gms:play-services-gcm:9.0.2'
    }
    
    apply plugin: 'com.google.gms.google-services'

9.0.2 はドキュメント更新時点で動作を確認できている版です。google-services と同様に、設定を変更しても動作するはずです。

以下のように GCM 以外のすべての機能が入った all-in-one の Google Play services を使用することもできます。ただし、ライブラリの組み合わせによってはメソッド数の上限を超えてビルドエラーになる可能性があるため、必要に応じて Multidex などの対応を行ってください。

  • dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        ...
        implementation 'com.google.android.gms:play-services:9.0.2'
    }
  • dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        ...
        compile 'com.google.android.gms:play-services:9.0.2'
    }

設定の反映

これらの設定を変更後、画面上部の "Sync Now" をクリックして編集結果を反映します。ライブラリーが自動的にダウンロードされて、プログラムから参照できるようになります。

以上で設定の操作は完了です。マニフェストの設定 に進みましょう。


<< Kii Cloud の設定 マニフェストの設定 >>