LoginSignup
74
64

More than 5 years have passed since last update.

UnityでGooglePlayServicesを入れるにはPlayServicesResolver(unity-jar-resolver)が便利

Last updated at Posted at 2016-02-14

前置き

Unity+Androidで、Google Play Servicesのライブラリを使いたい場合、
google-play-services_libを丸ごと入れてしまうと、メソッド数が多すぎて
65K超えエラーになることが良くあります。

こんな感じのエラーですね。
http://developer.android.com/intl/ja/tools/building/multidex.html

trouble writing output:
Too many field references: 131000; max is 65536.
You may try using --multi-dex option.

AndroidStudioでビルドする場合は、Google Play Servicesのライブラリの中から、
必要なAPIのみをbuild.gradleで指定して追加することができます。
https://developers.google.com/android/guides/setup#split

しかし、Unityのビルドではbuild.gradleを使用することはできないため、
Unityで同じことをするにはどうすれば良いのかという話です。

本題

GooglePlayServicesの中から、必要なAPIのみを追加するためのUnityライブラリを
Googleが公開してくれています。

PlayServicesResolver (unity-jar-resolver)
https://github.com/googlesamples/unity-jar-resolver

これはGoogle公式の以下のUnityプラグインに含まれているため、
これらの機能をUnityで使用したい場合は、公式プラグインをそのまま使えば良いです。

PlayServicesResolverの使い方

上記のリポジトリを持ってきて、play-services-resolver-x.x.x.unitypackageをインポートします。

SampleDependencies.csというサンプルが用意されているので、
実装方法はこれを見ればだいたいわかるかと思います。

試しにGoogle Adsのライブラリを追加するソースを追加してみます。

AdsDependencies.cs
using Google.JarResolver;
using UnityEditor;

[InitializeOnLoad]
public static class AdsDependencies {

    static AdsDependencies() {

        PlayServicesSupport svcSupport = PlayServicesSupport.CreateInstance(
            "AdsSample", EditorPrefs.GetString("AndroidSdkRoot"), "ProjectSettings");

        svcSupport.DependOn("com.google.android.gms", "play-services-ads", "8+");
    }
}

こんな感じで、PlayServicesSupport#DependOn を必要なだけ呼び出せばOKです。
3つの引数は、見ての通りbuild.gradleで指定する際のコロン区切りの値です。
ここに書いてあるやつです。)

パッケージをインポートした時に、Assets -> Google Play Services というメニューが追加されています。
(Build SettingsでAndroidにSwitchしていないと表示されません)

image

Resolve Client Jars を実行すると、指定したライブラリが依存関係を含めて追加されます。
なお、上記メニューの Settings -> Enable Background resolution がONになっていると、
Resolve Client Jars を実行しなくても勝手に追加してくれます。(デフォルトON)

さて、実行結果ですが、以下のようにaarとjarファイルが、Assets/Plugins/Android 配下に追加されました。

image

以上。

74
64
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
74
64