env->GetMethodID returns null

I need to use JNI to start an NSD service on mobile (For NDI android plugin).
I can package my app but it crashes when I call JNI because it can not find MethodID.

Other C++ codes (for example NSI SDK) works because I can use it with NDI HX Camera’s NSD service without a problem.

My Java code in UPL.xml is this.

	<gameActivityImportAdditions>
		<if condition="bCpuArchSupported">
			<insert>
				import android.os.IBinder;
				import android.app.Service;
				import android.net.nsd.NsdManager;
				import android.net.nsd.NsdServiceInfo;
			</insert>
		</if>
	</gameActivityImportAdditions>

	<gameActivityClassAdditions>
		<if condition="bCpuArchSupported">
			<insert>
				public NsdManager nsdManager;
				public NsdServiceInfo nsdServiceInfo;
				NsdManager.RegistrationListener listener;

				public void startNsdService(int Port, String ServiceName) {
				nsdManager = (NsdManager)Get().getApplicationContext().getSystemService(Context.NSD_SERVICE);

				nsdServiceInfo = new NsdServiceInfo();
				nsdServiceInfo.setServiceName(ServiceName);
				nsdServiceInfo.setServiceType("_ndi._tcp.");
				nsdServiceInfo.setPort(Port);

				nsdManager.registerService(nsdServiceInfo, NsdManager.PROTOCOL_DNS_SD, new NsdManager.RegistrationListener() {

				@Override
				public void onRegistrationFailed(NsdServiceInfo nsdServiceInfo, int i) {}

				@Override
				public void onUnregistrationFailed(NsdServiceInfo nsdServiceInfo, int i) {}

				@Override
				public void onServiceRegistered(NsdServiceInfo nsdServiceInfo) {}

				@Override
				public void onServiceUnregistered(NsdServiceInfo nsdServiceInfo) {}
				});
				}
			</insert>
		</if>
	</gameActivityClassAdditions>

	<proguardAdditions>
		<if condition="bCpuArchSupported">
			<insert>
				<![CDATA[
				-keepattributes Signature,MethodParameters,InnerClasses
				-dontskipnonpubliclibraryclassmembers
				-keepclassmembers class com.epicgames.unreal.GameActivity
				]]>
			</insert>
		</if>
	</proguardAdditions>

In order to call it I use this C++ code.

	JNIEnv* env = FAndroidApplication::GetJavaEnv(true);

	if (FJavaWrapper::GameActivityClassID == nullptr)
	{
		OutCode = "Unable to get GameActivity class.";
		return false;
	}

	jmethodID MethodID = env->GetMethodID(FJavaWrapper::GameActivityClassID, "startNsdService", "(ILjava/lang/String;)V");

	if (MethodID == nullptr)
	{
		OutCode = "Unable to find \"startNsdService\" Java method in GameActivity class. Look at NDI_UPL_Android.xml";
		return false;
	}

	auto ServiceName = env->NewStringUTF(TCHAR_TO_UTF8(*In_Name_Stream));
	
	FJavaWrapper::CallVoidMethod(env, FJavaWrapper::GameActivityClassID, MethodID, In_Port, *ServiceName);

Also tried these

My log’s last section.

[2023.02.08-22.33.41:076][427]LogBlueprintUserMessages: [UI_Capture_Devices_C_2147482447] Unable to find "startNsdService" Java method in GameActivity class. Look at NDI_UPL_Android.xml

I did some research and found out that proguard can change codes in order to protect it. So, it can be my problem but I don’t know how to disable it for my method.

Let us know if this fixes it :slight_smile:

I am not an expert about mobile development. So, I can’t tell specific solution. But I will try Java native codes plugins.

i solved the NSD problem with gtreshchev/AndroidNative. Right now I can call static methods and use NDI android