# env-\>GetMethodID returns null

**URL:** <https://forums.unrealengine.com/t/env-getmethodid-returns-null/766844>\
**Category:** Mobile\
**Tags:** unreal-engine, java, CPP, question\
**Created:** [February 8, 2023, 11:02pm UTC](https://forums.unrealengine.com/t/env-getmethodid-returns-null/766844 "2023-02-08T23:02:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eray\_ozr](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@eray\_ozr](https://forums.unrealengine.com/u/eray_ozr)\
**Post date:** [February 8, 2023, 11:02pm UTC](https://forums.unrealengine.com/t/env-getmethodid-returns-null/766844/1 "2023-02-08T23:02:16Z")

</div>

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.

```auto
	<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.

```auto
	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

- I added UPL.xml and PrivateDependencyModuleNames.Add(“Launch”); to my build.cs for Android platform.
- I tried FJavaWrapper::GetMethod()
- I got method’s signature from “javac -h” and “javap -s”.
- I changed method’s arguments and signature to “()V”
- I tried to delete proguard section from UPL.xml
- I tried to add my java code in a class but FindClass functions didn’t find that class.  
There are samples for additions without class.  
[https://github.com/EpicGames/UnrealEngine/blob/46544fa5e0aa9e6740c19b44b0628b72e7bbd5ce/Engine/Plugins/Online/OnlineSubsystemGoogle/Source/OnlineSubsystemGoogle\_UPL.xml](https://github.com/EpicGames/UnrealEngine/blob/46544fa5e0aa9e6740c19b44b0628b72e7bbd5ce/Engine/Plugins/Online/OnlineSubsystemGoogle/Source/OnlineSubsystemGoogle_UPL.xml)
- I tried Shipping Build

My log’s last section.

```auto
[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

```

---

<div class="post-metadata">

**Author:** ![eray\_ozr](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@eray\_ozr](https://forums.unrealengine.com/u/eray_ozr)\
**Post date:** [February 8, 2023, 11:32pm UTC](https://forums.unrealengine.com/t/env-getmethodid-returns-null/766844/2 "2023-02-08T23:32:09Z")

</div>

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.

> [@Run Java code from C++ using JNI (Android)](https://forums.unrealengine.com/t/run-java-code-from-c-using-jni-android/340143/3):
>
> My code compiles, it crashes during runtime, probably you have an other issue

---

<div class="post-metadata">

**Author:** ![EliasWick](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/eliaswick/32/1175208_2.png) [@EliasWick](https://forums.unrealengine.com/u/EliasWick)\
**Post date:** [February 9, 2023, 10:25am UTC](https://forums.unrealengine.com/t/env-getmethodid-returns-null/766844/3 "2023-02-09T10:25:38Z")

</div>

Let us know if this fixes it 🙂

---

<div class="post-metadata">

**Author:** ![eray\_ozr](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@eray\_ozr](https://forums.unrealengine.com/u/eray_ozr)\
**Post date:** [February 9, 2023, 5:48pm UTC](https://forums.unrealengine.com/t/env-getmethodid-returns-null/766844/4 "2023-02-09T17:48:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eray\_ozr](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@eray\_ozr](https://forums.unrealengine.com/u/eray_ozr)\
**Post date:** [February 10, 2023, 10:19am UTC](https://forums.unrealengine.com/t/env-getmethodid-returns-null/766844/5 "2023-02-10T10:19:12Z")

</div>

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