Unity IAPとGooglePlayBillingのclassが競合してAndroidがビルドできない問題の対策

こんにちは。もぐめっとです。

![IMG_9766.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/42234/e89de0ac-cb8c-1633-cf82-bd734d715f76.jpeg)
実はタキシードを着たことがあります。
だから何？という感じですね。すみません。調子乗りました。

今回は久々にUnityビルドの沼にハマったので解決策を掲示しておきます。

答えを知りたい場合は[対策](#対策)まで飛んじゃってください。

## エラー内容

アプリ内課金（In App Purchase）を導入したく、下記のドキュメントを見て導入をすすめてました。

https://developer.android.com/google/play/billing/unity

しかし、Unity IAPとplay-unity-pluginsを導入した状態でAndroid用にビルドすると、ひたすらにduplicateなエラーが出力されます。

```
Execution failed for task ':launcher:checkReleaseDuplicateClasses'.
> 1 exception was raised by workers:
  java.lang.RuntimeException: Duplicate class com.android.billingclient.BuildConfig found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.AccountIdentifiers found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.AcknowledgePurchaseParams found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.AcknowledgePurchaseParams$Builder found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.AcknowledgePurchaseResponseListener found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.BillingClient found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.BillingClient$BillingResponseCode found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.BillingClient$Builder found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.BillingClient$FeatureType found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.BillingClient$SkuType found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.BillingClientImpl found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.BillingClientStateListener found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.BillingFlowParams found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.BillingFlowParams$Builder found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.BillingFlowParams$ProrationMode found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.BillingResult found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.BillingResult$Builder found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.ConsumeParams found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.ConsumeParams$Builder found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.ConsumeResponseListener found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.PriceChangeConfirmationListener found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.PriceChangeFlowParams found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.PriceChangeFlowParams$Builder found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.ProxyBillingActivity found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.Purchase found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.Purchase$PurchaseState found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.Purchase$PurchasesResult found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.PurchaseHistoryRecord found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.PurchaseHistoryResponseListener found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.PurchasesUpdatedListener found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.SkuDetails found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.SkuDetailsParams found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.SkuDetailsParams$Builder found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.SkuDetailsResponseListener found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zza found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzaa found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzab found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzac found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzad found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzae found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzaf found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzag found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzah found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzai found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzaj found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzak found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzal found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzam found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzan found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzao found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzap found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzaq found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzar found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzas found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzat found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzb found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzc found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzd found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zze found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzf found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzg found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzh found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzi found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzj found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzk found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzl found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzm found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzn found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzo found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzp found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzq found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzr found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzs found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzt found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzu found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzv found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzw found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzx found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzy found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.android.billingclient.api.zzz found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.google.android.gms.internal.play_billing.zza found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.google.android.gms.internal.play_billing.zzb found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.google.android.gms.internal.play_billing.zzc found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.google.android.gms.internal.play_billing.zzd found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.google.android.gms.internal.play_billing.zze found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.google.android.gms.internal.play_billing.zzf found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
  Duplicate class com.google.android.gms.internal.play_billing.zzg found in modules jetified-GooglePlayBilling-runtime.jar (:GooglePlayBilling:) and jetified-billing-3.0.3-runtime.jar (:billing-3.0.3:)
```

こいつのおかげでandroidがビルドできません。

## 原因

googleのドキュメントはUnity IAPを手動でいれた前提で進めているのですが、最近はUnityPackageManagerを導入していれる手順になっております。
UnityPackageManagerでいれると ```./Library/PackageCache```のファイルを参照してビルドするのですが、この中にあるbilling-3.0.3.aarを抹消してもビルド時にしつこく復活してしまい、結果競合が起こってました。

ビルド後のファイルの様子

```shell
$ find . | grep billing-3
./Temp/StagingArea/aar/billing-3.0.3.aar
./Temp/gradleOut/unityLibrary/libs/billing-3.0.3.aar
./Library/PackageCache/com.unity.purchasing@3.1.0/Plugins/UnityPurchasing/Android/billing-3.0.3.aar.meta
./Library/PackageCache/com.unity.purchasing@3.1.0/Plugins/UnityPurchasing/Android/billing-3.0.3.aar
```



## 対策

ちなみにissueにも上がっておりますが、Googleの言うことはきくな！GooglePlayBillingは消せ！と言ってる人がいました。

https://github.com/google/play-unity-plugins/issues/78

しかし見ず知らずの意見を聞くのも微妙なので、頑張りました。
結果、Library/PackageCacheの中身をPackagesにコピーしてくることで解決しました！

手順は下記を実施するだけです！

```shell
$ cp -r ./Library/PackageCache/com.unity.purchasing@* ./Packages
$ rm com.unity.purchasing@3.1.0/Plugins/UnityPurchasing/Android/billing-*
```

この状態でビルドすると、PackageManagerがレポジトリからではなくローカルの方を参照するようになります。

```Packages/packages-lock.json
...
    "com.unity.purchasing": {
-     "version": "3.1.0",
+     "version": "file:com.unity.purchasing@3.1.0", 
      "depth": 0,
-     "source": "registry",
+     "source": "embedded",
      "dependencies": {
        "com.unity.ugui": "1.0.0",
        "com.unity.modules.unityanalytics": "1.0.0",
        "com.unity.modules.unitywebrequest": "1.0.0",
        "com.unity.modules.jsonserialize": "1.0.0",
        "com.unity.modules.androidjni": "1.0.0"
+      }
-      },
-      "url": "https://packages.unity.com"
...
```

これでduplicateな悩みからはさよならバイバイ！

※ただし、更新とかは手動でやらないといけなくなるので要注意です。

## まとめ

Library/PackageCache/com.unity.purchasing@*を./Packagesにコピってくれば解決！

UnityIAP側がGooglePlayBillingをフォローしてくれれば一番いいんですけどね。。悔やまれます。