google / play-unity-plugins

The Google Play Plugins for Unity provide C# APIs for accessing various Play services
Other
440 stars 113 forks source link

Error with script used on "Use the Google Play Billing Library with Unity" guide #52

Open salvobunetto opened 4 years ago

salvobunetto commented 4 years ago

The script in the guide

var storeModule = StandardPurchasingModule.Instance(); if (Application.platform == RuntimePlatform.Android) { storeModule = Google.Play.Billing.GooglePlayStoreModule.Instance(); } var configurationBuilder = ConfigurationBuilder.Instance(storeModule);

Result in this error

Assets\_Scripts\StoreManager.cs(201,22): error CS0266: Cannot implicitly convert type 'UnityEngine.Purchasing.Extension.AbstractPurchasingModule' to 'UnityEngine.Purchasing.StandardPurchasingModule'. An explicit conversion exists (are you missing a cast?)

if I use this script

// Create a builder using the GooglePlayStoreModule. var configurationBuilder = ConfigurationBuilder.Instance(Google.Play.Billing.GooglePlayStoreModule.Instance());

app crash and logcat result is cannot find IIapbilling class

I'll give more details when I have the project in front

Unity Editor - 2019.1.14 Unity IAP - 1.20 Google Play Core / Common / Billing - Latest versions

Edit: logcat error after reimport all Unity IAP and Play Billing

Logcat

Edit 2: The problem persists even if the proguard files are up to date

Qing451800 commented 4 years ago

Thanks for pointing it out. This is a problem in our code snippet. Although both GooglePlayStoreModule and StandardPurchasingModule implements the AbstractPurchasingModule, using a var makes it impossible to assign a GooglePlayStoreModule to StandardPurchasingModule.

As a fix, you can replace "var storeModule" with "AbstractPurchasingModule storeModule", or explicitly declare the variable type in the if statement like:

 ConfigurationBuilder builder;
 if (Application.platform == RuntimePlatform.Android)
 {
    builder = ConfigurationBuilder.Instance(Google.Play.Billing.GooglePlayStoreModule.Instance());
 }
 else
 {
    builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
 }

We will update the documentation.

Qing451800 commented 4 years ago

For the app crash, it shouldn't happen if you successfully imported the plugin. Make sure you have GooglePlayBilling.aar under Packages/Google Play Billing Library/Runtime/Plugins.

salvobunetto commented 4 years ago

For the app crash, it shouldn't happen if you successfully imported the plugin. Make sure you have GooglePlayBilling.aar under Packages/Google Play Billing Library/Runtime/Plugins.

Yes i have the file GoogleBillingAAR

Everiting works if i buld without proguard, the error is with proguard see my file proguard-user.txt

Edit: Any Solution? Proguard file or GradleTemplate?

Qing451800 commented 4 years ago

Your proguard file looks correct. It saves all classes in PBL by "com.android.billing.api.**".

Could you double check the dex file of your build? You should have a constructor method "private BillingClientImpl(String var1)" in com.android.billing.api.BillingClientImpl.class.

salvobunetto commented 4 years ago

As I can "check the dex file of your build", I'm trying to export the project to android studio for the time being, any help is welcome

PS. Any trick like useAndroidX on / off or enabled jetifier on / off?

EDIT: Anything work with Apk Generated with Android Studio! Unity bug or Gradle or what? 🤔

I only fix this "Android Resolve problem?" AndroidStudio

EDIT 2: @Qing451800 False alarm, Android Studio worked because the debug apk is executed without proguard just like unity, the problem persists ( is Proguard )

Qing451800 commented 4 years ago

I mean once you have your release apk, in Android Studio you can open it with Build -> Analyze an APK, and you will see the classes. You should have a constructor method "private BillingClientImpl(String var1)" in com.android.billing.api.BillingClientImpl.class.

salvobunetto commented 4 years ago

Problem Solved! Added to my file proguard these billing rules in addition to the provided proguard of the billing plugin, the end result is this

# Keep the AIDL interface
-keep class com.android.vending.billing.**
-keepclassmembers class com.android.vending.billing.** { *; }

# Keep the PBL public APIs
-keep class com.android.billingclient.api.**
-keepclassmembers class com.android.billingclient.api.** { *; }

I don't know if it would work by just adding { *; } but for the moment it's okay so

I made a pull request #54

Qing451800 commented 4 years ago

Thanks. Yup, looks like we only preserved the class not the methods. Left a comment in your PR.