How C++ and Java work together for Android system ?

Aqshay
Fri, 22 Sep 2023 12:14:24 +0000
Platform & Builds

Hello everyone ! Hope your projects are doing well !

We are currently working on a mobile game, and I’m starting to implement platform related logic (notification, ads, in-app purchases).

For now, I managed to implement ads with a plugin, and basic notification with Unreal BlueprintPlatformLibrary. But I’m not satisfied, my current quest on implementing this notification left a void in my heart, a missing piece in my UnrealLearningAdvendurePuzzle™.

How Unreal link C++ and Java logic ?

How can I implement my own Java logic if I need to do some modification / update ?

I have found multiple clues to resolve these mysteries:

1. The Beginning, AndroidJNI.cpp.
It’s where my knowledge lead me, the last function of c++, and the first I don’t fully understand.

int32 AndroidThunkCpp_ScheduleLocalNotificationAtTime(const FDateTime& FireDateTime, bool LocalTime, const FText& Title, const FText& Body, const FText& Action, const FString& ActivationEvent)
{
	//Convert FireDateTime to yyyy-MM-dd HH:mm:ss in order to pass to java
	FString FireDateTimeFormatted = FString::FromInt(FireDateTime.GetYear()) + "-" + FString::FromInt(FireDateTime.GetMonth()) + "-" + FString::FromInt(FireDateTime.GetDay()) + " " + FString::FromInt(FireDateTime.GetHour()) + ":" + FString::FromInt(FireDateTime.GetMinute()) + ":" + FString::FromInt(FireDateTime.GetSecond());

	JNIEnv* Env = FAndroidApplication::GetJavaEnv();
	if (Env != NULL)
	{
		auto jFireDateTime = FJavaHelper::ToJavaString(Env, FireDateTimeFormatted);
		auto jTitle = FJavaHelper::ToJavaString(Env, Title.ToString());
		auto jBody = FJavaHelper::ToJavaString(Env, Body.ToString());
		auto jAction = FJavaHelper::ToJavaString(Env, Action.ToString());
		auto jActivationEvent = FJavaHelper::ToJavaString(Env, ActivationEvent);

		return FJavaWrapper::CallIntMethod(Env, FJavaWrapper::GameActivityThis, FJavaWrapper::AndroidThunkJava_LocalNotificationScheduleAtTime, *jFireDateTime, LocalTime, *jTitle, *jBody, *jAction, *jActivationEvent);
	}

	return -1;
}

I found the word JNI a lot of time, and if I’m correct, it’s an interface to link C++ and Java, but I’m not sure, and I don’t understand how.

2. The End, LocalNotificationReceiver.java.
I’m sorry if I’m wide of the mark, Java is not my first language. But it seems like here are the good stuff, I can found the notification builder just like in the Android documentation.

		NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
			.setSmallIcon(notificationIconID)
			.setContentIntent(pendingNotificationIntent)
			.setWhen(System.currentTimeMillis())
			.setTicker(details)		// note: will not show up on Lollipop up except for accessibility
			.setContentTitle(title)
			.setStyle(new NotificationCompat.BigTextStyle().bigText(details));
		if (android.os.Build.VERSION.SDK_INT >= 21)
		{
			builder.setContentText(details);
			builder.setColor(0xff0e1e43);
		}

But the file is located ProjectName\Intermediate\Android\arm64\src\com\epicgames\unreal\LocalNotificationReceiver.java
And in the Engine it’s
Engine/Build/Android/Java/src/com/epicgames/unreal/LocalNotificationReceiver.java

So where this file come from ? Can I modify the file in the engine without risk ? Does it woks the same for IOS (This one scars me) ?

If you have some knowledge to share, some documentation in this subject, I would love to hear about it.

1 post - 1 participant