I found another way to use custom Java classes:
If I am not mistaken, the unreal engine _APL.xml file is associated with the ANT build system.
So, as in _APL.xml files, in addition to defining Java code in gameActivityClassAdditions, there is a tag that copies our .java files to the Unreal Engine build directory.
I took the idea from this Unreal Engine plugin: https://github.com/jeevcat/GoogleMapsUE4Plugin
So, I made the plugin in 4 stages:
- Copy the custom Java classes to the plugin source directory with the folder order of the package, so the resulting folder structure should look something like this.
2.- Add a tag of pre-created copies to apply the Java class in the assembly directory:
<prebuildCopies> <copyDir src="$S(PluginDir)/Java" dst="$S(BuildDir)" /> </prebuildCopies>
3.- Add import to gameActivityImportAdditions:
import org.samples.camera2.CameraHandler;
4.- use custom class
public void AndroidThunkJava_startCamera() { m_camHandler = new CameraHandler(); m_camHandler.setCallback(new CameraCallback() { @Override public void onGetFrame(byte[] data, int width, int height) { Log.d(LOG_TAG,"MY CUSTOM CALLBACK"+width); nativeGetFrameData(width, height, data); } }); m_camHandler.init(_activity, 0, 320, 240); m_camHandler.start(); }
Finally, I will show the resulting _APL.xml file:
<?xml version="1.0" encoding="utf-8"?> <root xmlns:android="http://schemas.android.com/apk/res/android"> <init> <log text="AndroidCamera init"/> </init> <androidManifestUpdates> <addPermission android:name="android.permission.CAMERA" /> <addFeature android:name="android.hardware.camera" /> <addFeature android:name="android.hardware.camera.autofocus" /> <addFeature android:name="android.hardware.camera2" /> </androidManifestUpdates> <prebuildCopies> <copyDir src="$S(PluginDir)/Java" dst="$S(BuildDir)" /> </prebuildCopies> <gameActivityImportAdditions> <insert> import android.widget.Toast; import android.hardware.Camera; import android.hardware.Camera.CameraInfo; import android.hardware.Camera.Parameters; import android.hardware.Camera.PreviewCallback; import android.graphics.SurfaceTexture; import android.graphics.ImageFormat; import android.graphics.PixelFormat; import java.util.List; import java.io.IOException; import android.util.Log; import org.samples.camera2.CameraHandler; </insert> </gameActivityImportAdditions> <gameActivityClassAdditions> <insert> static String msg = "yes i am a rock!"; SurfaceTexture surfaceTexture; Camera camera; CameraHandler m_camHandler; public native boolean nativeGetFrameData(int frameWidth, int frameHeight, byte[] data); public void AndroidThunkJava_Toast() { try { _activity.runOnUiThread(new Runnable() { public void run() { Toast.makeText(_activity.getApplicationContext(), "cam o yeah!", Toast.LENGTH_SHORT).show(); } }); } catch (Exception e) { Log.debug("Toast failed with exception " + e.getMessage()); } } public void AndroidThunkJava_startCamera() { m_camHandler = new CameraHandler(); m_camHandler.setCallback(new CameraCallback() { @Override public void onGetFrame(byte[] data, int width, int height) { Log.d(LOG_TAG,"MY CUSTOM CALLBACK"+width); nativeGetFrameData(width, height, data); } }); m_camHandler.init(_activity, 0, 320, 240); m_camHandler.start(); } public void AndroidThunkJava_stopCamera() { } </insert> </gameActivityClassAdditions> <gameActivityOnCreateAdditions> <insert> </insert> </gameActivityOnCreateAdditions> </root>