2020年08月14日

[Unity] Unity2019.4のエディタで動くのにAndroidの実機でDynamoDB(AWS Mobile SDK for Unity)が動かないときの対処

Unity2017で作ったアプリをUnity2019で更新した時の話。エディタでは動くのにAndroidの実機でDynamoDBの関数を実行しようとしたら動かないことがあったのでメモ。

OS : macOS Catalina(10.15.4)
使用環境 : Unity2019.4.0f1

事の発端は、DynamoDBの関数を呼ぶところで謎のエラーが出たところから。
------------------------
ArgumentException: Object of type 'System.Object[]' cannot be converted to type 
'UnityEngine.AndroidJavaObject[]'.
at System.RuntimeType.CheckValue (System.Object value, System.Reflection.Binder binder,
System.Globalization.CultureInfo culture, System.Reflection.BindingFlags invokeAttr)
[0x00000] in <00000000000000000000000000000000>:0
at System.Reflection.MonoMethod.ConvertValues (System.Reflection.Binder binder,
System.Object[] args, System.Reflection.ParameterInfo[] pinfo, System.Globalization.CultureInfo
culture, System.Reflection.BindingFlags invokeAttr) [0x00000] in
<00000000000000000000000000000000>:0
------------------------

調べてみたら、AWSの公式プラグインではUnity2019には対応していないらしい。
参考: https://qiita.com/kosuke1113/items/904df92d444804d496c3

できるだけ簡単な対処方法を探していたところ、良いページを発見。
https://lhkmarcus.com/2020/04/05/how-to-use-aws-sdk-in-unity-2019/

このページを参考に、自分がやった事は以下の通り。

@link.xmlを確認。(変更なし)
実際に使ったlink.xmlはこの通り。

------------------------
<linker>
<assembly fullname="UnityEngine">
<type fullname="UnityEngine.Networking.UnityWebRequest" preserve="all" />
<type fullname="UnityEngine.Networking.UploadHandlerRaw" preserve="all" />
<type fullname="UnityEngine.Networking.UploadHandler" preserve="all" />
<type fullname="UnityEngine.Networking.DownloadHandler" preserve="all" />
<type fullname="UnityEngine.Networking.DownloadHandlerBuffer" preserve="all" />
</assembly>
<assembly fullname="mscorlib">
<namespace fullname="System.Security.Cryptography" preserve="all"/>
</assembly>
<assembly fullname="System">
<namespace fullname="System.Security.Cryptography" preserve="all"/>
<namespace fullname="System.ComponentModel" preserve="all" />
</assembly>
<assembly fullname="System.Configuration">
<namespace fullname="System.Configuration" preserve="all" />
</assembly>
<assembly fullname="AWSSDK.Core" preserve="all">
<namespace fullname="Amazon.Util.Internal.PlatformServices" preserve="all"/>
</assembly>
<assembly fullname="AWSSDK.DynamoDBv2" preserve="all"/>
<assembly fullname="AWSSDK.CognitoIdentity" preserve="all"/>
<assembly fullname="AWSSDK.CognitoIdentityProvider" preserve="all"/>
<assembly fullname="AWSSDK.SecurityToken" preserve="all"/>
</linker>
------------------------

AAWS for Unityのプラグインをダウンロード&インポート。
ダウンロード元: ※2020年8月13日現在 = aws-sdk-unity_3.3.796.0
https://docs.aws.amazon.com/ja_jp/mobile/sdkforunity/developerguide/setup-unity.html

zipファイルを解凍すると、各サービス用のUnityパッケージがあるので、その中の
「AWSSDK.DynamoDBv2.3.3.106.45.unitypackage」
をWクリックして、Unityプロジェクトにインポートする。

BAssets/Plugins/Android/AndroidManifest.xml を修正。
------------------------
<activity android:name="com.unity3d.player.UnityPlayerNativeActivity"
android:label="@string/app_name">
 ↓赤い箇所を削除
<activity android:name="com.unity3d.player.UnityPlayerActivity"
android:label="@string/app_name">
------------------------

CUnity2019用に修正されたSDKをダウンロード&解凍して、既存ライブラリを上書き。
このページにある「3.Download the fixed SDK here」から、Unity2019用に修正されたSDKをダウンロードする。
解凍すると、各サービス用にUnityパッケージがあるので、必要な分を選んで上書きする。
自分の場合は、link.xmlに記載されてあったサービスのを選んだ。

001.png

上書きされるのは、Unityプロジェクト内のAssets/AWSSDK にあるライブラリ。

002.png

これでビルドして実行したら、Androidの実機で無事DynamoDBが動作した!
本当にありがたい。

こうなるとUnity2020は、怖くて使えないなー。
posted by be-style at 00:45| Comment(0) | Unity

2020年08月12日

[Unity] Androidプラグインにおける依存関係の解決と重複クラスの解消方法

------ 後日談 ------
ビルドエラーはなくなったものの、AdMob広告が表示されなかったりGooglePlayServiceが動かなかったり、問題は残っています。
本ページの内容が何かしらのヒントになれば幸いです。
---------------------

UnityのAndroid用ビルドするとき、いつもプラグインにおける依存関係で問題があったりクラス宣言が重複したりとかで困っていたので、対処方法をメモ。


AdMobのプラグイン(2020年8月11日現在 v5.3.0)
https://github.com/googleads/googleads-mobile-unity/releases/tag/v5.3.0

Google Play Serviceのプラグイン(2020年8月11日現在 v0.10.09)
https://github.com/playgameservices/play-games-plugin-for-unity/releases

まずはビルドのエラーメッセージ。

001.png

Google Play Serviceにおけるクラス宣言の重複があるというエラー(たくさん表示される中から一部を抜粋)
---------------------------------------------
Duplicate class com.google.android.gms.common.data.DataBufferObserver found in modules classes.jar (:com.google.android.gms.play-services-base-17.1.0:) and classes.jar (:play-services-base-9.4.0:)
---------------------------------------------

これからUnityの機能を使って依存関係を解決するんだけど、ビルド時に自動でjarファイルを追加されないように設定しておく。

Assets > External Dependency Manager > Android Resolver > Settings

002.png

上から3つ目の「Enable Resolution On Build」のチェックを外す。そうしないと、この後の操作で消したはずのjarファイルがビルド時に復活してビルドエラーになる。

003.png

準備ができたところで、Project内を確認してみると、Assets > Plugins の中にたくさんのjarファイルがあって、この中のいくつかでクラス宣言が重複していたりするらしい。

004.png

なので、依存関係を解決してみる。
Assets > External Dependency Manager > Android Resolver > Resolve

005.png

うまくいくとこんなメッセージが表示される。

006.png

この状態でビルドしてもエラーになる。
なので、依存関係を解決したjarファイル(ライブラリ)を削除する。
Assets > External Dependency Manager > Android Resolver > Delete resolved Libraries

007.png

すると、たくさんあったプロジェクト内のjarファイルのいくつかが削除された。

008.png

これで終わったと思いきや、まだビルドエラーが出る。
今度はAdMobにおけるクラス宣言の重複らしい。

---------------------------------------------
Duplicate class com.google.unity.ads.RewardBasedVideo$2 found in modules classes.jar (:googlemobileads-unity:) and unity-plugin-library.jar (unity-plugin-library.jar)
---------------------------------------------

プロジェクトで、エラーメッセージにあった「googlemobileads-unity」で検索をかけてみると、Inspector画面の「Select platforms for plugin」にてAndroidにチェックが入っていたので、このライブラリではAndroid用に使用しないようにチェックを外し、Applyを押して更新する。

これでビルドが通った。
Unityってこんなに便利になってたんだ、と驚きました。
posted by be-style at 16:09| Comment(0) | Unity

[Unity] AndroidManifest.xml is missing. の対処法

UnityのAndroid用ビルドをしようとして、AdMobのエラーが出たときの対処法をメモで残しておく。

Unity用のAdMobSDKのパッケージ  ※Ver. 5.3.0 = 2020年8月12日現在
https://developers.google.com/admob/unity/start?hl=ja

エラーメッセージ:
-------------------------------------
BuildMethodException: [GoogleMobileAds] AndroidManifest.xml is missing. Try re-importing the plugin.
ManifestProcessor.StopBuildWithMessage (System.String message) (at Assets/GoogleMobileAds/Editor/ManifestProcessor.cs:175)
ManifestProcessor.OnPreprocessBuild (UnityEditor.Build.Reporting.BuildReport report) (at Assets/GoogleMobileAds/Editor/ManifestProcessor.cs:52)
UnityEditor.Build.BuildPipelineInterfaces+<>c__DisplayClass15_0.b__1 (UnityEditor.Build.IPreprocessBuildWithReport bpp) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/BuildPipelineInterfaces.cs:411)
UnityEditor.Build.BuildPipelineInterfaces.InvokeCallbackInterfacesPair[T1,T2] (System.Collections.Generic.List`1[T] oneInterfaces, System.Action`1[T] invocationOne, System.Collections.Generic.List`1[T] twoInterfaces, System.Action`1[T] invocationTwo, System.Boolean exitOnFailure) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/BuildPipelineInterfaces.cs:391)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr) (at /Users/builduser/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:197)
-------------------------------------

「AndroidManifest.xml 」が見つからないということで、パスが誤っているらしい。

Assets > GoogleMobileAds > ManifestProcessor.cs
を開き、40行目あたりにある赤い部分を削除する。

-------------------------------------
string manifestPath = Path.Combine(Application.dataPath, "Plugins/Android/GoogleMobileAdsPlugin.androidlib/AndroidManifest.xml");

string manifestPath = Path.Combine(Application.dataPath, "Plugins/Android/GoogleMobileAdsPlugin/AndroidManifest.xml");
-------------------------------------

これでビルドが通った。
公式のプラグインに誤りがあるなんて!

参考:
https://stackoverflow.com/questions/57920250/androidmanifest-xml-is-missing-try-reimporting-the-plugin
posted by be-style at 14:57| Comment(0) | Unity