Amazon In app purchase

Tag: in-app-purchase , amazon Author: terrcy Date: 2013-07-01

Trying to implement in-app purchase in my Amazon android app. Found this tutorial

http://www.techotopia.com/index.php/A_Simple_Amazon_In-App_Purchasing_Example_Application#Creating_the_Example_Application

And here is what I implemented

There is an activity called PurchaseActivity in my app with an unlock button in it.

Firstly I updated my manifest and added the reciever code.

Next, Created an InAppObserver.java class in my src which extends PurchaseActivity.

Next, Inside PurchaseActivity added onstart and onresume methods. (which is what is giving error as per logcat)

Added a consumable_sku string in strings.xml

Updated onstart method with following code :

 Set<String>skuList = new HashSet<String>(1);
 skuList.add(getResources().getString(R.string.consumable_sku));
 PurchasingManager.initiateItemDataRequest(skuList);

Added ItemDataResponse code in InAppObserver.java class I created.

Added makePurchase function in PurchaseActivity which is called on click of unlock button.

Finally Added PurchaseResponse method in InAppObserver.java class.

However, my app crashes before starting the PurchaseActivity giving following error : java.lang.NoClassDefFounderror at com.xyz.xyz.InAppObserver

I am trying this for the first time, sorry for bad english and the length of the question.

Best Answer

I can't say for sure what the problem is. I think you are likely to have to post more code. However, here are a couple of issues (neither of which I think caused the issue you have but they might be worth trying).

  • You say InAppObserver extends PurchaseActivity. Typo? It should extent PurchasingObserver or BasePurchasingObserver
  • You call itemGetDataRequest immediately in onStart. A better sequence is
    • onStart calls registerObserver
    • (observer) onSdkAvailable calls initiateGetUserIdRequest
    • (observer) onGetUserIdResponse calls initiatePurchaseUpdatesRequest - do you call this?
    • (observer) purchaseUpdatesResponse calls initiateItemDataRequest if the upgrade hasn't already been purchased

Overall, I'm not that impressed with the tutorial you linked to - might want to just use the Amazon docs.

comments:

Thanks DrC. It really helped. Now I have one more question. How do I check whether the app is purchased or not on second visit. What function do I use?
I assume you mean how do you determine if the upgrade has been purchased. The call to initiatePurchaseUpdatesRequest tells you if it was purchased by the account on another machine. However, properly coded, it will only tell you once so you need to save that information somewhere. In theory, you could just call initiateUpdatesRequest with the initial offset every time. I'm not sure if that is considered acceptable or if it has other, bad, side-effects. I save the information with some token attempts at security.