Skip to content

Fix Amazon purchase tracking setup #1860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 6, 2023

Conversation

nan-li
Copy link
Contributor

@nan-li nan-li commented Oct 4, 2023

Description

One Line Summary

Fixes crash "java.lang.IllegalArgumentException: Expected receiver of type com.amazon.device.iap.internal.d, but got kotlin.Unit" and update to allow using appstore v3.0.4.

Details

Motivation

We have many reports of this crash and I was able to reproduce after adding v3.0.3 of the Amazon AppStore SDK.

Additionally,v3.0.4 of the Amazon AppStore SDK released in March 2023 changed the name of an internal method we rely on, so update to handle this version. We had no reports of this crashing so no one seems to be using this version so far.

Scope

  1. We did not correctly set a listenerHandlerObject in the startup code for tracking Amazon purchases, and this is the object that got a type of kotlin.Unit. The reason is because of the way we were setting it via shorthand. However, let's not use this shorthand since it is easy to make this mistake if we add code in the future.
// Incorrect, listenerHandlerObject equals the last line of the block, registerListenerOnMainThread = true
listenerHandlerObject = try {
    listenerHandlerClass.getMethod("d").invoke(null)
} catch (e: NullPointerException) {
    listenerHandlerClass.getMethod("e").invoke(null)
    registerListenerOnMainThread = true
}

// Correct, listenerHandlerObject equals the last line of the block, listenerHandlerClass.getMethod("e").invoke(null)
listenerHandlerObject = try {
    listenerHandlerClass.getMethod("d").invoke(null)
} catch (e: NullPointerException) {
    registerListenerOnMainThread = true
    listenerHandlerClass.getMethod("e").invoke(null)
}
  1. Second, we make a cast to PurchasingListener when it can be optional, so cast to PurchasingListener? instead or else we will receive a different NPE crash.

I tested on player model, and see it can be nullable. The casting worked differently with Java so we did not encounter this crash.

  1. There is no change to apps not using Amazon AppStore, as we return early and don't go down these lines of code.

  2. An internal method of the appstore SDK was renamed in v3.0.4, and we are now compatible with this version.

package com.amazon.device.iap.internal;

// 3.0.3
public static d e() {
    return c;
}

// 3.0.4
public static d g() {
    return c;
}

Testing

Unit testing

None

Manual testing

Tested on Android emulator API 33

  1. Add dependency 'com.amazon.device:amazon-appstore-sdk:3.0.3' to Demo App
  2. Reproduced the "Expected receiver of type com.amazon.device.iap.internal.d, but got kotlin.Unit" crash
  3. Then test everything is working after this fix
  4. Add dependency 'com.amazon.device:amazon-appstore-sdk:3.0.4' to Demo App and see there is a crash
  5. Make v3.0.4 fix and everything works.
  6. Then confirm both 3.0.3 and 3.0.4 work with the code changes.

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries
  • Outcomes
  • Sessions
  • In-App Messaging
  • REST API requests
  • Public API changes

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
  • I have personally tested this on my device, or explained why that is not possible

Final pass

  • Code is as readable as possible.
  • I have reviewed this PR myself, ensuring it meets each checklist item

This change is Reviewable

* Correctly set the `listenerHandlerObject` object when it was previously always equal to Kotlin.Unit due to the order the statements within the catch statement.
* Cast to PurchasingListener? since it is a optional type.
* Amazon Appstore SDK v3.0.4 released March 13, 2023 changed the name of a method we utilize.
@nan-li nan-li force-pushed the fix/amazon_purchase_tracking_startup branch from 4abe7ca to 9794d5e Compare October 5, 2023 18:24
@nan-li nan-li requested a review from emawby October 5, 2023 18:28
@nan-li nan-li merged commit c864d7d into user-model/main Oct 6, 2023
@nan-li nan-li deleted the fix/amazon_purchase_tracking_startup branch October 6, 2023 19:27
@nan-li nan-li mentioned this pull request Oct 6, 2023
jinliu9508 pushed a commit that referenced this pull request Jan 31, 2024
…startup

Fix Amazon purchase tracking setup
jinliu9508 pushed a commit that referenced this pull request Jan 31, 2024
…startup

Fix Amazon purchase tracking setup
jinliu9508 pushed a commit that referenced this pull request Feb 6, 2024
…startup

Fix Amazon purchase tracking setup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants