jamesmontemagno / InAppBillingPlugin

Cross-platform In App Billing Plugin for .NET
MIT License
651 stars 152 forks source link

Compatibility with Android OS version 4.x? 5.x? 6? #212

Closed najak3d closed 5 years ago

najak3d commented 5 years ago

James, thank you for this awesome plugin. It looks like you've created an excellent comprehensive mutli-platform solution! I appreciate your good work here.

My chief concern is this:

What is the oldest version of Android for which this plugin is compatible?

I see that the example code makes use of .NET 4.8 "Task" feature (which requires Android 7.1, or iOS 10.8), and the target API for Android is supposed to be set at Level 25 (Android 7.1 as well).

We have an app established on Android since 2014, and we used the old "Xamarin.InAppBilling.dll" which no longer works (we were forced to change our target API Level, which is what broke it). Our application runs on Android 4.0.3, and so we'd prefer to maintain Android 4.x compatibility while still providing those devices with the ability to do InApp purchases. We currently have about 1000 subscribers running on Android 4.x.

We've forced to move to something new in a hurry, and that is why I am here. I need to get something new working in a hurry.

Details about Old Plugin: The error now being thrown by the old Xamarin.InAppBilling.dll is "Service intent must be explicit: Intent".

Googling that error is what has lead me here to use this plugin. But before I rewrite our purchasing logic, I wanted to know about the Android OS version compatibility.

jamesmontemagno commented 5 years ago

You can use this on Android 4.x device. What you are seeing with "Task" is a feature of the Xamarin.Android SDK that has supported it for 4 or 5 years now or even longer, I can't remember. That is pure compilation and nothing to do with the Android operating system itself.

najak3d commented 5 years ago

Wow, very speedy response, thank you! And this is good news to us.

For the time being, we need a very quick fix to push to production with minimal change, and so we used ILSpy to decompile the old plugin that we're using and for the "Connect()" call we were able to insert the one needed line of code to make this work.

We've added it to our plan, though, to switch over to your plugin, since it's clearly superior, and well-supported.

In case anyone else out there reading this is stuck in a similar situation as we were, here is the simple fix that I applied to make the old plugin function:

        // Create a new connection to the Google Play Service
        _serviceConnection = new InAppBillingServiceConnection (CurrentState.App.MainContext, value);
        _serviceConnection.OnConnected += () => {
            // Load inventory or available products
            GetInventory();

            // Load any items already purchased
            _ValidatePreviousPurchases();

            PurchaseLog.Info("Store Connected.");
            IsReadyForUse = true;
        };

        // Instead of calling Connect - do it ourselves, since this Plugin no longer works with Android 5+ otherwise!
        if (true)
        {  // NEW METHOD, we pulled in this code from ILSpy, so we could insert one line of code below!
            var activity = CurrentState.App.MainContext;
            Intent intent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
            intent.SetPackage("com.android.vending"); // <<<==== THIS, IS THE MAGIC LINE OF CODE THAT WAS MISSING!

            int count = activity.PackageManager.QueryIntentServices(intent, 0).Count;
            if (count != 0)
            {
                bool result = 
                activity.BindService(intent, _serviceConnection, Bind.AutoCreate);
            }
        }
        else
        {  /// BROKEN-METHOD - Connect() - use the code above instead.
            // Attempt to connect to the service
            _serviceConnection.Connect();
        }