From b2d878ff441d1cbf0c409b4afe3e48090b2a7e5e Mon Sep 17 00:00:00 2001 From: qiuguohua Date: Thu, 12 Dec 2024 17:57:06 +0800 Subject: [PATCH] Fix the problem of release crash on android platform. --- .../android/libcocos2dx/proguard-rules.pro | 2 + .../vendor/google/billing/GoogleBilling.cpp | 62 ++++++++++++++++++- native/vendor/google/billing/JniBilling.cpp | 2 +- native/vendor/google/billing/JniBilling.h | 2 +- .../billing/build-params/ConsumeParams.h | 2 +- .../billing/build-params/InAppMessageParams.h | 2 +- .../build-params/QueryProductDetailsParams.h | 2 +- 7 files changed, 66 insertions(+), 8 deletions(-) diff --git a/native/cocos/platform/android/libcocos2dx/proguard-rules.pro b/native/cocos/platform/android/libcocos2dx/proguard-rules.pro index 031a158bb42..60c0ad5c562 100644 --- a/native/cocos/platform/android/libcocos2dx/proguard-rules.pro +++ b/native/cocos/platform/android/libcocos2dx/proguard-rules.pro @@ -9,6 +9,8 @@ # Add any project specific keep options here: -keep public class com.google.** { *; } +-keep public class com.android.** { *; } +-keep public class google.billing.** { *; } -keep public class androidx.** { *; } -keep class com.cocos.lib.CocosActivity { public ; diff --git a/native/vendor/google/billing/GoogleBilling.cpp b/native/vendor/google/billing/GoogleBilling.cpp index a2c7c4b6f07..df8c23c4c5a 100644 --- a/native/vendor/google/billing/GoogleBilling.cpp +++ b/native/vendor/google/billing/GoogleBilling.cpp @@ -26,6 +26,7 @@ #include "cocos/bindings/jswrapper/SeApi.h" #include "platform/java/jni/JniHelper.h" #include "platform/java/jni/JniImp.h" +#include "vendor/google/billing/result-values/BillingResult.h" #include "vendor/google/billing/GoogleBillingHelper.h" #include "vendor/google/billing/GoogleBillingManager.h" #include "vendor/google/billing/build-params/AcknowledgePurchaseParams.h" @@ -133,12 +134,16 @@ bool BillingClient::isReady() const { } void BillingClient::queryProductDetailsAsync(QueryProductDetailsParams* params, se::Object* listener) { + if(!params || !listener) { + CC_LOG_WARNING("params or listener can't be null"); + return; + } int listenerId = addListener(listener); - std::vector productIds; std::vector productTypes; - productIds.reserve(16); - productTypes.reserve(16); + size_t size = params->_productList.size(); + productIds.reserve(size); + productTypes.reserve(size); for (auto product : params->_productList) { productIds.push_back(product->_productId); productTypes.push_back(product->_productType); @@ -147,25 +152,45 @@ void BillingClient::queryProductDetailsAsync(QueryProductDetailsParams* params, } void BillingClient::launchBillingFlow(BillingFlowParams* params) { + if(!params) { + CC_LOG_WARNING("Params can't be null"); + return; + } GoogleBillingHelper::launchBillingFlow(_tag, params); } void BillingClient::consumeAsync(ConsumeParams* params, se::Object* listener) { + if(!params || !listener) { + CC_LOG_WARNING("Params or listener can't be null"); + return; + } int listenerId = addListener(listener); GoogleBillingHelper::consumeAsync(_tag, listenerId, params); } void BillingClient::acknowledgePurchase(AcknowledgePurchaseParams* params, se::Object* listener) { + if(!params || !listener) { + CC_LOG_WARNING("Params or listener can't be null"); + return; + } int listenerId = addListener(listener); GoogleBillingHelper::acknowledgePurchase(_tag, listenerId, params); } void BillingClient::queryPurchasesAsync(QueryPurchasesParams* params, se::Object* listener) { + if(!params || !listener) { + CC_LOG_WARNING("Params or listener can't be null"); + return; + } int listenerId = addListener(listener); GoogleBillingHelper::queryPurchasesAsync(_tag, listenerId, params->_productType); } void BillingClient::getBillingConfigAsync(GetBillingConfigParams* params, se::Object* listener) { + if(!params || !listener) { + CC_LOG_WARNING("Params or listener can't be null"); + return; + } int listenerId = addListener(listener); GoogleBillingHelper::getBillingConfigAsync(_tag, listenerId); } @@ -175,36 +200,67 @@ BillingResult* BillingClient::isFeatureSupported(const std::string& feature) { } void BillingClient::createAlternativeBillingOnlyReportingDetailsAsync(se::Object* listener) { + if(!listener) { + CC_LOG_WARNING("Listener can't be null"); + return; + } int listenerId = addListener(listener); GoogleBillingHelper::createAlternativeBillingOnlyReportingDetailsAsync(_tag, listenerId); } void BillingClient::isAlternativeBillingOnlyAvailableAsync(se::Object* listener) { + if(!listener) { + CC_LOG_WARNING("Listener can't be null"); + return; + } int listenerId = addListener(listener); GoogleBillingHelper::isAlternativeBillingOnlyAvailableAsync(_tag, listenerId); } void BillingClient::createExternalOfferReportingDetailsAsync(se::Object* listener) { + if(!listener) { + CC_LOG_WARNING("Listener can't be null"); + return; + } int listenerId = addListener(listener); GoogleBillingHelper::createExternalOfferReportingDetailsAsync(_tag, listenerId); } void BillingClient::isExternalOfferAvailableAsync(se::Object* listener) { + if(!listener) { + CC_LOG_WARNING("Listener can't be null"); + return; + } int listenerId = addListener(listener); GoogleBillingHelper::isExternalOfferAvailableAsync(_tag, listenerId); } BillingResult* BillingClient::showAlternativeBillingOnlyInformationDialog(se::Object* listener) { + if(!listener) { + // Extending the return value of BillingResult.Normal behavior wouldn't get in here. + auto* builder = BillingResult::newBuilder(); + return (*builder).setResponseCode(6).setDebugMessage("Listener can't be null").build(); + } int listenerId = addListener(listener); return GoogleBillingHelper::showAlternativeBillingOnlyInformationDialog(_tag, listenerId); } BillingResult* BillingClient::showExternalOfferInformationDialog(se::Object* listener) { + if(!listener) { + // Extending the return value of BillingResult.Normal behavior wouldn't get in here. + auto* builder = BillingResult::newBuilder(); + return (*builder).setResponseCode(6).setDebugMessage("Listener can't be null").build(); + } int listenerId = addListener(listener); return GoogleBillingHelper::showExternalOfferInformationDialog(_tag, listenerId); } BillingResult* BillingClient::showInAppMessages(InAppMessageParams* params, se::Object* listener) { + if(!params || !listener) { + // Extending the return value of BillingResult.Normal behavior wouldn't get in here. + auto* builder = BillingResult::newBuilder(); + return (*builder).setResponseCode(6).setDebugMessage("Listener can't be null").build(); + } int listenerId = addListener(listener); return GoogleBillingHelper::showInAppMessages(_tag, listenerId, params->_inAppMessageCategoryIds); } diff --git a/native/vendor/google/billing/JniBilling.cpp b/native/vendor/google/billing/JniBilling.cpp index 36634d785ad..a49b90cbd65 100644 --- a/native/vendor/google/billing/JniBilling.cpp +++ b/native/vendor/google/billing/JniBilling.cpp @@ -407,7 +407,7 @@ jobject JniBilling::newProductDetailsParamsObject(int tag, const BillingFlowPara return env->CallObjectMethod(builder, buildMethodId); } -jobject JniBilling::newProductDetailsParamsListObject(int tag, std::vector listParams) { +jobject JniBilling::newProductDetailsParamsListObject(int tag, const std::vector& listParams) { JNIEnv *env = cc::JniHelper::getEnv(); jclass listClass = env->FindClass("java/util/ArrayList"); jmethodID methodInit = env->GetMethodID(listClass, "", "()V"); /* 无参构造 */ diff --git a/native/vendor/google/billing/JniBilling.h b/native/vendor/google/billing/JniBilling.h index 556b2f920b4..ca34f16a447 100644 --- a/native/vendor/google/billing/JniBilling.h +++ b/native/vendor/google/billing/JniBilling.h @@ -72,7 +72,7 @@ class CC_DLL JniBilling { private: static jobject newSubscriptionUpdateParamsObject(const BillingFlowParams::SubscriptionUpdateParams* params); static jobject newProductDetailsParamsObject(int tag, const BillingFlowParams::ProductDetailsParams* params); - static jobject newProductDetailsParamsListObject(int tag, std::vector listParams); + static jobject newProductDetailsParamsListObject(int tag, const std::vector& listParams); static jobject newPurchaseUpdateListenerObject(int tag); static jobject newUserChoiceBillingListenerObj(int tag); diff --git a/native/vendor/google/billing/build-params/ConsumeParams.h b/native/vendor/google/billing/build-params/ConsumeParams.h index 9dbf5f03a39..ba4b04d514b 100644 --- a/native/vendor/google/billing/build-params/ConsumeParams.h +++ b/native/vendor/google/billing/build-params/ConsumeParams.h @@ -57,7 +57,7 @@ class ConsumeParams : public cc::RefCounted { } private: - ConsumeParams(const std::string&& purchaseToken) : _purchaseToken(purchaseToken) { + ConsumeParams(std::string&& purchaseToken) : _purchaseToken(purchaseToken) { } std::string _purchaseToken; diff --git a/native/vendor/google/billing/build-params/InAppMessageParams.h b/native/vendor/google/billing/build-params/InAppMessageParams.h index 4a39357e763..c6db3684b62 100644 --- a/native/vendor/google/billing/build-params/InAppMessageParams.h +++ b/native/vendor/google/billing/build-params/InAppMessageParams.h @@ -58,7 +58,7 @@ class InAppMessageParams : public cc::RefCounted { } private: - InAppMessageParams(const std::vector&& inAppMessageCategoryIds):_inAppMessageCategoryIds(inAppMessageCategoryIds) { + InAppMessageParams(std::vector&& inAppMessageCategoryIds):_inAppMessageCategoryIds(inAppMessageCategoryIds) { } friend class BillingClient; std::vector _inAppMessageCategoryIds; diff --git a/native/vendor/google/billing/build-params/QueryProductDetailsParams.h b/native/vendor/google/billing/build-params/QueryProductDetailsParams.h index 35b7efb2ad1..04bea7d53e8 100644 --- a/native/vendor/google/billing/build-params/QueryProductDetailsParams.h +++ b/native/vendor/google/billing/build-params/QueryProductDetailsParams.h @@ -86,7 +86,7 @@ class QueryProductDetailsParams : public cc::RefCounted { } private: - QueryProductDetailsParams(const std::vector&& productLists) : _productList(productLists) { + QueryProductDetailsParams(std::vector&& productLists) : _productList(productLists) { } private: