UnityServices.InitializeAsync + ConfigurationBuilder.Instance Crash on Android

I am using Unity 2020.3.43f1 and Unity IAP 4.7.0

I did a test app where I have two buttons, the first button calls the UnityServices initialization:

public void InitUnityServices()
    {
        try
        {
            var options = new InitializationOptions().SetEnvironmentName("production");
            UnityServices.InitializeAsync(options).ContinueWith(task => OnUnityServicesInitializeSuccess());
        }
        catch (Exception e)
        {
            OnUnityServicesInitializeError(e.Message);
        }
    }

    private void OnUnityServicesInitializeSuccess()
    {
        Debug.Log($"UnityServices Initialization Success");
    }

    private void OnUnityServicesInitializeError(string message)
    {
        Debug.Log($"UnityServices Initialization Error: {message}");
    }

The second button calls the IAP initialization:

public void InitializePurchasing()
    {
        ConfigurationBuilder builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());

        builder.AddProduct("com.myappdomain.coinpack1", ProductType.Consumable);

        UnityPurchasing.Initialize(this, builder);
    }

Everything works fine if I click the buttons one after another, but if I put the IAP initialization on the Unity Services initialization callback the app crashes on Android:

private void OnUnityServicesInitializeSuccess()
    {
        Debug.Log($"UnityServices Initialization Success");

        InitializePurchasing();
    }

The crash happens on ConfigurationBuilder.Instance line.

Any idea what is happening? Should I workaround with some kind of delay?

Any help is appreciated, I am running out of time to publish the app :S

I think you can just await “UnityServices.InitializeAsync(options).” This is what I do. Note the async in the init. Then await the UnityServices.

private async void Init()
        {
            var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());

              ... add products here.

            await UnityServices.InitializeAsync();
            UnityPurchasing.Initialize(this, builder);
        }
2 Likes

I tried with an async method the result is the same - native crash on Android. Not exactly the same, the crash looks to be intermittent.

public async void InitUnityServices()
    {
        Debug.Log($"InitUnityServices");
        var options = new InitializationOptions().SetEnvironmentName("production");
        await UnityServices.InitializeAsync(options);

        InitializePurchasing();
    }

    public void InitializePurchasing()
    {
        Debug.Log($"InitializePurchasing");

        if (_storeController != null) return;

        ConfigurationBuilder builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());

        builder.AddProduct("com.myappdomain.coinpack1", ProductType.Consumable);

        UnityPurchasing.Initialize(this, builder);
    }

In the editor it works just fine.

Are you using a button to set all this up? I have a class that I set up on Awake(), set it to DontDestroyOnLoad, and store the instance in a public singleton. If you press both buttons too fast, the await might not have finished yet.

I am trying a different approach @NoPants . I am initializing the Unity Services on the first scene of my game, and the Unity Purchasing on the second one, I think this is going to work. Thank you a lot for trying to help me. I do not know, I think this may be an issue with Unity Services + Unity Purchasing on Android, who knows.

I don’t think that’s the problem tbh. Async can be a bit tricky. You may find it works sometimes, but not always with the way you are talking about setting it up. But if you test it and it does work all the time, I guess it doesn’t matter.

It’s working now… I was using async at first, and I had to change because the apps was rejected once it was crashing on the reviewers devices… This is weird, but at least it seems to work all the times. Thank you again man!

Hi, Sorry to resurrect this thread. I’m having the exact same problem. How did you fix this?