Skip to content

Commit 4f4ee27

Browse files
authored
Merge pull request #357 from AppsFlyerSDK/DELIVERY-63011/fix-android-null-pointer-exception
This should fix the NullPointerException
2 parents a58a49b + 25fb530 commit 4f4ee27

File tree

3 files changed

+69
-52
lines changed

3 files changed

+69
-52
lines changed
Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
package com.appsflyer.appsflyersdk;
22

3-
public class AppsFlyerConstants {
4-
final static String PLUGIN_VERSION = "6.15.1";
5-
final static String AF_APP_INVITE_ONE_LINK = "appInviteOneLink";
6-
final static String AF_HOST_PREFIX = "hostPrefix";
7-
final static String AF_HOST_NAME = "hostName";
8-
final static String AF_IS_DEBUG = "isDebug";
9-
final static String AF_MANUAL_START = "manualStart";
10-
final static String AF_DEV_KEY = "afDevKey";
11-
final static String AF_EVENT_NAME = "eventName";
12-
final static String AF_EVENT_VALUES = "eventValues";
13-
final static String AF_ON_INSTALL_CONVERSION_DATA_LOADED = "onInstallConversionDataLoaded";
14-
final static String AF_ON_APP_OPEN_ATTRIBUTION = "onAppOpenAttribution";
15-
final static String AF_SUCCESS = "success";
16-
final static String AF_FAILURE = "failure";
17-
final static String AF_GCD = "GCD";
18-
final static String AF_UDL = "UDL";
19-
final static String AF_VALIDATE_PURCHASE = "validatePurchase";
20-
final static String AF_GCD_CALLBACK = "onInstallConversionData";
21-
final static String AF_OAOA_CALLBACK = "onAppOpenAttribution";
22-
final static String AF_UDL_CALLBACK = "onDeepLinking";
23-
final static String DISABLE_ADVERTISING_IDENTIFIER = "disableAdvertisingIdentifier";
3+
public final class AppsFlyerConstants {
4+
final static String PLUGIN_VERSION = "6.15.1";
5+
final static String AF_APP_INVITE_ONE_LINK = "appInviteOneLink";
6+
final static String AF_HOST_PREFIX = "hostPrefix";
7+
final static String AF_HOST_NAME = "hostName";
8+
final static String AF_IS_DEBUG = "isDebug";
9+
final static String AF_MANUAL_START = "manualStart";
10+
final static String AF_DEV_KEY = "afDevKey";
11+
final static String AF_EVENT_NAME = "eventName";
12+
final static String AF_EVENT_VALUES = "eventValues";
13+
final static String AF_ON_INSTALL_CONVERSION_DATA_LOADED = "onInstallConversionDataLoaded";
14+
final static String AF_ON_APP_OPEN_ATTRIBUTION = "onAppOpenAttribution";
15+
final static String AF_SUCCESS = "success";
16+
final static String AF_FAILURE = "failure";
17+
final static String AF_GCD = "GCD";
18+
final static String AF_UDL = "UDL";
19+
final static String AF_VALIDATE_PURCHASE = "validatePurchase";
20+
final static String AF_GCD_CALLBACK = "onInstallConversionData";
21+
final static String AF_OAOA_CALLBACK = "onAppOpenAttribution";
22+
final static String AF_UDL_CALLBACK = "onDeepLinking";
23+
final static String DISABLE_ADVERTISING_IDENTIFIER = "disableAdvertisingIdentifier";
2424

25-
final static String AF_EVENTS_CHANNEL = "af-events";
26-
final static String AF_METHOD_CHANNEL = "af-api";
27-
final static String AF_CALLBACK_CHANNEL = "callbacks";
25+
final static String AF_EVENTS_CHANNEL = "af-events";
26+
final static String AF_METHOD_CHANNEL = "af-api";
27+
final static String AF_CALLBACK_CHANNEL = "callbacks";
2828

29-
final static String AF_BROADCAST_ACTION_NAME = "com.appsflyer.appsflyersdk";
29+
final static String AF_BROADCAST_ACTION_NAME = "com.appsflyer.appsflyersdk";
30+
31+
final static String AF_PLUGIN_TAG = "AppsFlyer_FlutterPlugin";
3032
}

android/src/main/java/com/appsflyer/appsflyersdk/AppsflyerSdkPlugin.java

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
import static com.appsflyer.appsflyersdk.AppsFlyerConstants.AF_EVENTS_CHANNEL;
5151
import static com.appsflyer.appsflyersdk.AppsFlyerConstants.AF_FAILURE;
52+
import static com.appsflyer.appsflyersdk.AppsFlyerConstants.AF_PLUGIN_TAG;
5253
import static com.appsflyer.appsflyersdk.AppsFlyerConstants.AF_SUCCESS;
5354

5455
/**
@@ -203,7 +204,7 @@ private void startListening(Object arguments, Result rawResult) {
203204
@Override
204205
public void onMethodCall(MethodCall call, Result result) {
205206
if (activity == null) {
206-
Log.d("AppsFlyer", "Activity isn't attached to the flutter engine");
207+
Log.d(AF_PLUGIN_TAG, LogMessages.ACTIVITY_NOT_ATTACHED_TO_ENGINE);
207208
return;
208209
}
209210
final String method = call.method;
@@ -362,11 +363,11 @@ private void performOnDeepLinking(MethodCall call, Result result) {
362363
AppsFlyerLib.getInstance().performOnDeepLinking(intent, mApplication);
363364
result.success(null);
364365
} else {
365-
Log.d("AppsFlyer", "performOnDeepLinking: intent is null!");
366+
Log.d(AF_PLUGIN_TAG, "performOnDeepLinking: intent is null!");
366367
result.error("NO_INTENT", "The intent is null", null);
367368
}
368369
} else {
369-
Log.d("AppsFlyer", "performOnDeepLinking: activity is null!");
370+
Log.d(AF_PLUGIN_TAG, "performOnDeepLinking: activity is null!");
370371
result.error("NO_ACTIVITY", "The current activity is null", null);
371372
}
372373
}
@@ -379,34 +380,37 @@ private void anonymizeUser(MethodCall call, Result result) {
379380

380381
private void startSDKwithHandler(MethodCall call, final Result result) {
381382
try {
382-
final AppsFlyerLib instance = AppsFlyerLib.getInstance();
383-
instance.start(activity, null, new AppsFlyerRequestListener() {
383+
final AppsFlyerLib appsFlyerLib = AppsFlyerLib.getInstance();
384+
385+
appsFlyerLib.start(activity, null, new AppsFlyerRequestListener() {
384386
@Override
385387
public void onSuccess() {
386-
uiThreadHandler.post(new Runnable() {
387-
@Override
388-
public void run() {
389-
mMethodChannel.invokeMethod("onSuccess", null);
390-
}
391-
});
388+
if (mMethodChannel != null) {
389+
uiThreadHandler.post(() -> mMethodChannel.invokeMethod("onSuccess", null));
390+
} else {
391+
Log.e(AF_PLUGIN_TAG, LogMessages.METHOD_CHANNEL_IS_NULL);
392+
result.error("NULL_OBJECT", LogMessages.METHOD_CHANNEL_IS_NULL, null);
393+
}
392394
}
393395

394396
@Override
395397
public void onError(final int errorCode, final String errorMessage) {
396-
uiThreadHandler.post(new Runnable() {
397-
@Override
398-
public void run() {
398+
if (mMethodChannel != null) {
399+
uiThreadHandler.post(() -> {
399400
HashMap<String, Object> errorDetails = new HashMap<>();
400401
errorDetails.put("errorCode", errorCode);
401402
errorDetails.put("errorMessage", errorMessage);
402403
mMethodChannel.invokeMethod("onError", errorDetails);
403-
}
404-
});
404+
});
405+
} else {
406+
Log.e(AF_PLUGIN_TAG, LogMessages.METHOD_CHANNEL_IS_NULL);
407+
result.error("NULL_OBJECT", LogMessages.METHOD_CHANNEL_IS_NULL, null);
408+
}
405409
}
406410
});
407411
result.success(null);
408-
} catch (Exception e) {
409-
result.error("UNEXPECTED_ERROR", e.getMessage(), null);
412+
} catch (Throwable t) {
413+
result.error("UNEXPECTED_ERROR", t.getMessage(), null);
410414
}
411415
}
412416

@@ -532,14 +536,14 @@ private void sendPushNotificationData(MethodCall call, Result result) {
532536
Bundle bundle;
533537

534538
if (pushPayload == null) {
535-
Log.d("AppsFlyer", "Push payload is null");
539+
Log.d(AF_PLUGIN_TAG, "Push payload is null");
536540
return;
537541
}
538542

539543
try {
540544
bundle = this.jsonToBundle(new JSONObject(pushPayload));
541545
} catch (JSONException e) {
542-
Log.d("AppsFlyer", "Can't parse pushPayload to bundle");
546+
Log.d(AF_PLUGIN_TAG, "Can't parse pushPayload to bundle");
543547
return;
544548
}
545549

@@ -557,7 +561,7 @@ private void sendPushNotificationData(MethodCall call, Result result) {
557561
}
558562

559563
if (errorMsg != null) {
560-
Log.d("AppsFlyer", errorMsg);
564+
Log.d(AF_PLUGIN_TAG, errorMsg);
561565
return;
562566
}
563567

@@ -963,8 +967,8 @@ private void logAdRevenue(MethodCall call, Result result) {
963967
try {
964968
String monetizationNetwork = requireNonNullArgument(call, "monetizationNetwork");
965969
String currencyIso4217Code = requireNonNullArgument(call, "currencyIso4217Code");
966-
double revenue = requireNonNullArgument(call,"revenue");
967-
String mediationNetworkString = requireNonNullArgument(call,"mediationNetwork");
970+
double revenue = requireNonNullArgument(call, "revenue");
971+
String mediationNetworkString = requireNonNullArgument(call, "mediationNetwork");
968972

969973
MediationNetwork mediationNetwork = MediationNetwork.valueOf(mediationNetworkString.toUpperCase());
970974

@@ -984,10 +988,9 @@ private void logAdRevenue(MethodCall call, Result result) {
984988
} catch (IllegalArgumentException e) {
985989
// The IllegalArgumentException could come from either requireNonNullArgument or valueOf methods.
986990
result.error("INVALID_ARGUMENT_PROVIDED", e.getMessage(), null);
987-
}
988-
catch (Throwable t) {
991+
} catch (Throwable t) {
989992
result.error("UNEXPECTED_ERROR", "[logAdRevenue]: An unexpected error occurred: " + t.getMessage(), null);
990-
Log.e("AppsFlyer", "Unexpected exception occurred: [logAdRevenue]", t);
993+
Log.e(AF_PLUGIN_TAG, "Unexpected exception occurred: [logAdRevenue]", t);
991994
}
992995
}
993996

@@ -1004,7 +1007,7 @@ private void logAdRevenue(MethodCall call, Result result) {
10041007
private <T> T requireNonNullArgument(MethodCall call, String argumentName) throws IllegalArgumentException {
10051008
T argument = call.argument(argumentName);
10061009
if (argument == null) {
1007-
Log.e("AppsFlyer", "Exception occurred when trying to: " + call.method + "->" + argumentName + " must not be null");
1010+
Log.e(AF_PLUGIN_TAG, "Exception occurred when trying to: " + call.method + "->" + argumentName + " must not be null");
10081011
throw new IllegalArgumentException("[" + call.method + "]: " + argumentName + " must not be null");
10091012
}
10101013
return argument;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.appsflyer.appsflyersdk;
2+
3+
public final class LogMessages {
4+
5+
// Prevent the instantiation of this utilities class.
6+
private LogMessages() {
7+
throw new IllegalStateException("LogMessages class should not be instantiated");
8+
}
9+
10+
public static final String METHOD_CHANNEL_IS_NULL = "mMethodChannel is null, cannot invoke the callback";
11+
public static final String ACTIVITY_NOT_ATTACHED_TO_ENGINE = "Activity isn't attached to the flutter engine";
12+
}

0 commit comments

Comments
 (0)