Android Native ProとAdMobの競合の解決方法【Unity】

March 14, 2019, 3:02 a.m. edited March 14, 2019, 3:20 a.m.

#AdMob  #Unity  #Android 

Unityはクロスプラットフォーム開発ができ,Android,iOS,Windows,...と様々なプラットフォーム向けにアプリをビルドすることができます.しかし,その分個別のプラットフォーム独自の機能にはどうしても弱くなってしまいます.そこで,Asset Storeにはこれを補うようなプラグインが数多くリリースされています.その中でAndroid向けのネイティブプラグインとして有名なのがSTAN'S ASSETSAndroid Native Proです1.(普段は結構お高いアセットなのですが,いつかのセールのときにすごく安くなっていたので買ってしまっていました)

ここで,Android向けにアプリをリリースするUnityを利用する開発者の多くが導入するのがAdMob、Googleの広告です.

そして,この2つを一緒に使って開発し,いざビルドしてみると,

CommandInvokationFailure: Gradle build failed. 
/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/bin/java -classpath "/Applications/Unity/PlaybackEngines/AndroidPlayer/Tools/gradle/lib/gradle-launcher-4.0.1.jar" org.gradle.launcher.GradleMain "-Dorg.gradle.jvmargs=-Xmx2048m" "assembleRelease"

stderr[

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':transformClassesWithDexForRelease'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Landroid/arch/core/internal/SafeIterableMap;

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED in 1m 46s
]
stdout[
Starting a Gradle Daemon (subsequent builds will be faster)
The setTestClassesDir(File) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use the setTestClassesDirs(FileCollection) method instead.
The getTestClassesDir() method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use the getTestClassesDirs() method instead.
The ConfigurableReport.setDestination(Object) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use the method ConfigurableReport.setDestination(File) instead.
:preBuild UP-TO-DATE
:preReleaseBuild UP-TO-DATE
:checkReleaseManifest
:preDebugBuild UP-TO-DATE
:prepareAn_coreLibrary
:prepareAndroidArchCoreRuntime110Library
:prepareAndroidArchLifecycleLivedataCore110Library
:prepareAndroidArchLifecycleRuntime100Library
:prepareAndroidArchLifecycleRuntime110Library
:prepareAndroidArchLifecycleViewmodel110Library
:prepareComAndroidSupportAnimatedVectorDrawable2711Library
:prepareComAndroidSupportAppcompatV72711Library
:prepareComAndroidSupportCardviewV72711Library
:prepareComAndroidSupportCustomtabs2610Library
:prepareComAndroidSupportCustomtabs2711Library
:prepareComAndroidSupportSupportCompat2610Library
:prepareComAndroidSupportSupportCompat2711Library
:prepareComAndroidSupportSupportCoreUi2610Library
:prepareComAndroidSupportSupportCoreUi2711Library
:prepareComAndroidSupportSupportCoreUtils2610Library
:prepareComAndroidSupportSupportCoreUtils2711Library
:prepareComAndroidSupportSupportFragment2610Library
:prepareComAndroidSupportSupportFragment2711Library
:prepareComAndroidSupportSupportMediaCompat2610Library
:prepareComAndroidSupportSupportMediaCompat2711Library
:prepareComAndroidSupportSupportV42610Library
:prepareComAndroidSupportSupportV42711Library
:prepareComAndroidSupportSupportVectorDrawable2711Library
:prepareComGoogleAndroidGmsPlayServicesAds1501Library
:prepareComGoogleAndroidGmsPlayServicesAdsBase1501Library
:prepareComGoogleAndroidGmsPlayServicesAdsIdentifier1501Library
:prepareComGoogleAndroidGmsPlayServicesAdsLite1501Library
:prepareComGoogleAndroidGmsPlayServicesBasement1501Library
:prepareComGoogleAndroidGmsPlayServicesGass1501Library
:GoogleMobileAdsPlugin:preBuild UP-TO-DATE
:GoogleMobileAdsPlugin:preReleaseBuild UP-TO-DATE
:GoogleMobileAdsPlugin:checkReleaseManifest
:GoogleMobileAdsPlugin:prepareReleaseDependencies
:GoogleMobileAdsPlugin:compileReleaseAidl
:GoogleMobileAdsPlugin:compileReleaseNdk NO-SOURCE
:GoogleMobileAdsPlugin:compileLint
:GoogleMobileAdsPlugin:copyReleaseLint NO-SOURCE
:GoogleMobileAdsPlugin:mergeReleaseShaders
:GoogleMobileAdsPlugin:compileReleaseShaders
:GoogleMobileAdsPlugin:generateReleaseAssets
:GoogleMobileAdsPlugin:mergeReleaseAssets
:GoogleMobileAdsPlugin:mergeReleaseProguardFiles
:GoogleMobileAdsPlugin:packageReleaseRenderscript NO-SOURCE
:GoogleMobileAdsPlugin:compileReleaseRenderscript
:GoogleMobileAdsPlugin:generateReleaseResValues
:GoogleMobileAdsPlugin:generateReleaseResources
:GoogleMobileAdsPlugin:packageReleaseResources
:GoogleMobileAdsPlugin:processReleaseManifest
:GoogleMobileAdsPlugin:generateReleaseBuildConfig
:GoogleMobileAdsPlugin:processReleaseResources
:GoogleMobileAdsPlugin:generateReleaseSources
:GoogleMobileAdsPlugin:incrementalReleaseJavaCompilationSafeguard
:GoogleMobileAdsPlugin:javaPreCompileRelease
:GoogleMobileAdsPlugin:compileReleaseJavaWithJavac
:GoogleMobileAdsPlugin:processReleaseJavaRes NO-SOURCE
:GoogleMobileAdsPlugin:transformResourcesWithMergeJavaResForRelease
:GoogleMobileAdsPlugin:transformClassesAndResourcesWithSyncLibJarsForRelease
:GoogleMobileAdsPlugin:mergeReleaseJniLibFolders
:GoogleMobileAdsPlugin:transformNativeLibsWithMergeJniLibsForRelease
:GoogleMobileAdsPlugin:transformNativeLibsWithStripDebugSymbolForRelease
:GoogleMobileAdsPlugin:transformNativeLibsWithSyncJniLibsForRelease
:GoogleMobileAdsPlugin:bundleRelease
:an_library.bundle:preBuild UP-TO-DATE
:an_library.bundle:preReleaseBuild UP-TO-DATE
:an_library.bundle:checkReleaseManifest
:an_library.bundle:prepareReleaseDependencies
:an_library.bundle:compileReleaseAidl
:an_library.bundle:compileReleaseNdk NO-SOURCE
:an_library.bundle:compileLint
:an_library.bundle:copyReleaseLint NO-SOURCE
:an_library.bundle:mergeReleaseShaders
:an_library.bundle:compileReleaseShaders
:an_library.bundle:generateReleaseAssets
:an_library.bundle:mergeReleaseAssets
:an_library.bundle:mergeReleaseProguardFiles
:an_library.bundle:packageReleaseRenderscript NO-SOURCE
:an_library.bundle:compileReleaseRenderscript
:an_library.bundle:generateReleaseResValues
:an_library.bundle:generateReleaseResources
:an_library.bundle:packageReleaseResources
:an_library.bundle:processReleaseManifest
:an_library.bundle:generateReleaseBuildConfig
:an_library.bundle:processReleaseResources
:an_library.bundle:generateReleaseSources
:an_library.bundle:incrementalReleaseJavaCompilationSafeguard
:an_library.bundle:javaPreCompileRelease
:an_library.bundle:compileReleaseJavaWithJavac
:an_library.bundle:processReleaseJavaRes NO-SOURCE
:an_library.bundle:transformResourcesWithMergeJavaResForRelease
:an_library.bundle:transformClassesAndResourcesWithSyncLibJarsForRelease
:an_library.bundle:mergeReleaseJniLibFolders
:an_library.bundle:transformNativeLibsWithMergeJniLibsForRelease
:an_library.bundle:transformNativeLibsWithStripDebugSymbolForRelease
:an_library.bundle:transformNativeLibsWithSyncJniLibsForRelease
:an_library.bundle:bundleRelease
:prepareReleaseDependencies
:compileReleaseAidl
:compileReleaseRenderscript
:generateReleaseBuildConfig
:generateReleaseResValues
:generateReleaseResources
:mergeReleaseResources
:processReleaseManifest
:processReleaseResources
:generateReleaseSources
:incrementalReleaseJavaCompilationSafeguard
:javaPreCompileRelease
:compileReleaseJavaWithJavac
:compileReleaseNdk NO-SOURCE
:compileReleaseSources
:lintVitalRelease
:mergeReleaseShaders
:compileReleaseShaders
:generateReleaseAssets
:mergeReleaseAssets
:transformClassesWithDexForRelease FAILED
95 actionable tasks: 95 executed
]
exit code: 1
UnityEditor.Android.Command.WaitForProgramToRun (UnityEditor.Utils.Program p, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <a798aa15a3b64b78b7b748372c1177e2>:0)
UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <a798aa15a3b64b78b7b748372c1177e2>:0)
UnityEditor.Android.AndroidJavaTools.RunJava (System.String args, System.String workingdir, System.Action`1[T] progress, System.String error) (at <a798aa15a3b64b78b7b748372c1177e2>:0)
UnityEditor.Android.GradleWrapper.Run (System.String workingdir, System.String task, System.Action`1[T] progress) (at <a798aa15a3b64b78b7b748372c1177e2>:0)
Rethrow as GradleInvokationException: Gradle build failed
UnityEditor.Android.GradleWrapper.Run (System.String workingdir, System.String task, System.Action`1[T] progress) (at <a798aa15a3b64b78b7b748372c1177e2>:0)
UnityEditor.Android.PostProcessor.Tasks.BuildGradleProject.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <a798aa15a3b64b78b7b748372c1177e2>:0)
UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <a798aa15a3b64b78b7b748372c1177e2>:0)
UnityEditor.BuildPlayerWindow:BuildPlayerAndRun()

というようなエラーが出てしまいます.Multiple dex files...とあるように同じjarやaarといったファイルがバージョン違えど複数あるのが原因です.そのため,例えばAssets/Plugins/Android/android.arch.core.common-1.0.0.jarというような被っているファイルを削除したいのですが,これを削除すると強制的にAdMobのPlay Services Resolverが走ってせっかく消したファイルを復活させてしまいます.

では,どうすればよいのかというと,Android Native Proの公式ホームページにヒントがありました.

これにしたがって以下の図に示すように,Unity Jar Resolverを動かします.

すると,Android Native ProとAdMobの競合が解決され,きれいにAssets/Plugins/Android/以下にプラグインが並べられました!

こうしてビルドすると問題なくアプリは動き,Android Native ProとAdMob双方の機能も問題なく動きました.


  1. iOS向けにはIOS Native Proがあります.