Skip to main content

How to call Java method

The syntax for calling a Java method from C++ is the following:

AndroidNativeUtils::CallJavaStaticMethod<RETURN_TYPE>(CLASS_NAME, METHOD_NAME, ARGUMENTS);

Where

  • RETURN_TYPE: the return type of the method
  • CLASS_NAME: the name of the Java class containing the method
  • METHOD_NAME – the name of the method to call
  • ARGUMENTS – the arguments to pass to the method (list of supported arguments

Specific example

Suppose we have a simple method in a Java file named StringOperations.java here to concatenate two strings:

@Keep
public class StringOperations {
@Keep
public static String ConcatenateStrings(String Str1, String Str2)
{
String ReturnString = Str1 + Str2;
return ReturnString;
}
}

To call this method, use the following C++ code:

FString Str1 = TEXT("String 1");
FString Str2 = TEXT("String 2");
FString ConcatenatedString = AndroidNativeUtils::CallJavaStaticMethod<FString>("com/Plugins/AndroidNative/StringOperations", "ConcatenateStrings", Str1, Str2);

P.S. If you encounter a runtime error when calling your function, please refer to this page.