Android Java Libraries in UE4 Game (OUYA SDK, Google Play Game Services, etc.)

Unfortunately, the JNI_OnLoad event seems like the ONLY time certain classes can be looked up. (I left some details about this in the previous post.)

Using the callback method and using a global variable constructor to register the JNI_OnLoad callback is fast enough to register the event and detect the classes successfully, all from a UE4 plugin.

There are some issues with bundling JAR/Java in a UE4 plugin, however. The Java and JAR file is not detected when provided by the plugin.

I had to move the JAR/Java into the game module.
https://github.com/tgraupmann/UE4_Plugin_Cortex_App_Store/blob/master/ExampleProject/Build/Android/libs/ouya-sdk.jar
https://github.com/tgraupmann/UE4_Plugin_Cortex_App_Store/blob/master/ExampleProject/Build/Android/src/tv/ouya/sdk/PluginTestGameActivity.java
https://github.com/tgraupmann/UE4_Plugin_Cortex_App_Store/blob/master/ExampleProject/Build/Android/src/tv/ouya/sdk/TestOnGameActivityListener.java

Here is the UE4 plugin code using a global variable constructor to register the JNI_OnLoad callback.

I’ll try to incorporate the other suggestions to minimize how much code is in the PRs.

I’ll have to look into that. Maybe that’s how I can avoid needing to copy the JAR/Java to the game module…

The AndroidPluginLanguage is quite powerful. The best docs can be found here.
https://github.com/EpicGames/UnrealEngine/blob/4.11/Engine/Source/Programs/UnrealBuildTool/Android/AndroidPluginLanguage.cs

The best example can be found here:
https://github.com/EpicGames/UnrealEngine/blob/4.11/Engine/Plugins/Runtime/GearVR/Source/GearVR/GearVR_APL.xml

I was able to create my own UE4 plugin APL.xml file.

And reference the APL.xml from the UE4 plugin build script.

And now I have the JAR and JAVA being used directly from the UE4 plugin.

I’ll see if I can make use of the injected Java using the AndroidPluginLanguage next…

Reducing the number of changes in the PR…
https://github.com/EpicGames/UnrealEngine/compare/4.11...tgraupmann:4.11-Cortex?expand=1

I have the changes I need slimmed down to the Android JNI_OnLoad event callback and Android Input callback.
https://github.com/EpicGames/UnrealEngine/pull/2313/files
https://github.com/EpicGames/UnrealEngine/pull/2314/files

This is all so I can roll my Cortex/OUYA fork into a UE4 plugin. Developers don’t want to have to build the engine from source. Also as a plugin, it can be activated with the menu toggle.

I found one more issue. If I want to add android:game to the AndroidManifest.xml I need to make sure the APK is being built with API-21 or better build tools. How might I configure UE4 to do that? I’m already specifying a target version of 21. That should be enough.???

I am getting the expected manifest changes. It’s just the packaging tool needs to use the newer API???

[QUOTE=tgraupmann]

That worked for exactly the reason said. It uses the class loader from JNI_OnLoad.

That means I can close the PR that added the JNI_OnLoad callback.
https://github.com/EpicGames/UnrealEngine/pull/2313/files

I still need the input callback to do input remapping. There’s no existing functionality that exposes that.
https://github.com/EpicGames/UnrealEngine/pull/2314/files

Accessing JAR/Java from a UE4 plugin is working like a charm.

You can see I only needed to use FAndroidApplication::FindJavaClass from the UE4 StartupModule to be able to find classes from the JAR/Java. After finding the class I can use JNIEnv normally to find methods, fields, static methods, and invoke per the usual.

You can set the SDK API level to android-21 in Android SDK project settings in the editor. You can set it as high as android-22 if you have it installed, but do not set it to android-23 or android-24 yet; I need to update the APK expansion library code to remove depreciated APIs first.

Ah yes. That worked like a charm. I was looking in the Android project settings, but there’s the Android SDK project settings with NDK API Level which needed to be set to android-21. I did noticed when I saved changes that this setting doesn’t appear to serialize to disk.

And here is where I enabled the android:isGame setting.

That leaves the last PR. I need the Android input callback to do input remapping. I can’t see any existing functionality that exposes that.
https://github.com/EpicGames/UnrealEngine/pull/2314/files

With this input callback is where I pass the input to Java, pass it through a framework, and then return the remapped input back to the UE4 plugin. This allows games to publish in the Cortex store and as the framework adds support for new controllers, the games won’t need to republish to get all the new mappings.

With the PR request I’ve been able to intercept all the controller KeyEvent and MotionEvent input and redirect that to the UE4 plugin.

Turns out I don’t need the input PR. A plugin can reroute the Android input with the following.

First get the extern.

// reassign android input callback

No PR needed!

Would this information work with implementing Facebook or NativeX SDKs? I’m attempting to setup pay per install advertising but I’m having a terrible time at doing so. I’m starting to regret spending time learning the engine because it’s near impossible to profit off any mobile makings.

This information is useful for any Android UE4 plugin.

But what I’m referring to is an SDK, not specifically a plugin. To run pay-per-install ads on Facebook you need to use the Facebook SDK and activate your app when it starts



@Override
public void onCreate() {
    super.onCreate();
    FacebookSdk.sdkInitialize(getApplicationContext());
    AppEventsLogger.activateApp(this);
}


They have a decent looking guide but I’m not sure how to import only the part of the SDK I need, and how to call the java code from UE4. If this thread covers that I can look more thoroughly through it; I’m still learning things here – so I wanted to know if my request is supported with this method before I go about researching it.

Absolutely. You would make use of your plugins APL.xml file to inject the onCreate code and to copy the facebook-sdk.jar.

You could pretty much copy and paste most of that and just rename your JAR file to whatever the FacebookSDK JAR is.

I’ll bookmark this page and potentially get around to it. Feels like I spend more time hacking things together than actual game designing. Maybe someone will make a plugin and throw it on the market for activating common pay-per-install services :wink:

jajaja Mr @tgraupmann you are a rock!, i dont know what can i do without your powers, thanks to you i understand quick the new system, i update my old plugin to access the camera now without modify the engine source, i never say thanks before, the new system is very cool now i can do what ever i want

check this out:

Best Regards tgraupmann

Excellent work sir! How is the Unreal API for accessing video? Or is that through Android using a View?

I plan on using the Unreal API for accessing the Microphone and playing recorded sounds at some point…

hi tgraupmann right now unreal API cant access the camera, even with c++ is impossible, like the oficial android studio page says, this is only possible in java side, im using a Android View and pass my data to unreal, i read from some post, looks like some guy is integrating this, so maybe the android camera will be accesible from the oficial unreal API soon, meanwhile i have mine ^_^, really i dont know about the microphone, sounds like a great feature! i dont research about that in the unreal API

Media Framework 2.0 will allow for camera input sources. It may not be in 4.13 with first release but the necessary hooks will be in place. I have a plugin I did back with 4.10 using the existing media system, but it is a bit messy (it pretends to be a movie player for files with extension .acam and uses the filename for selecting camera, resolution, and framerate). I’ll update it to work with the new system soon.

hi [MENTION=11153] Babcock[/MENTION] so when we can spec to see that? in 4.14? im using the android.hardware.camera2 api right now, there is a way to access the camera from c++? or really is just java side? because sometime ago i research a lot, and i never see a example, but poeple on the wild internet told me this is posible with unofficial APIs, i made this in the early 4.5 i think, but when see this post i decide upgrade to use the android new plugin system based in GearVR plugin, i never use the media framework, i will take a look

If not in for 4.13, it will likely be available before 4.14 as a plugin. I used android.hardware.Camera in Java. Native camera APIs on Android are a mess (different APIs for different Android versions).

I tried to compile GitHub - tgraupmann/UE4_Plugin_Cortex_App_Store and it doesn’t.
After all fixes on CPP i have:
====6/17/2016 1:15:13 PM====PERFORMING FINAL APK PACKAGE OPERATION================================================
MainFrameActions: Packaging (Android (ATC)): mono: Making .apk with Ant… (note: it’s safe to ignore javac obsolete warnings)
MainFrameActions: Packaging (Android (ATC)): mono: Duplicated project name in import. Project custom_rules defined first in /Users/AlekseiKanash/Projects/UE4_Plugin_Cortex_App_Store/ExampleProject/Intermediate/Android/APK/custom_rules.xml and again in /Users/AlekseiKanash/Projects/UE4_Plugin_Cortex_App_Store/ExampleProject/Intermediate/Android/APK/JavaLibs/g
oogle-play-services_lib_rev19/custom_rules.xml
MainFrameActions: Packaging (Android (ATC)): mono: [javac] warning: [options] source value 1.5 is obsolete and will be removed in a future release
MainFrameActions: Packaging (Android (ATC)): mono: [javac] warning: [options] target value 1.5 is obsolete and will be removed in a future release
MainFrameActions: Packaging (Android (ATC)): mono: [javac] warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.
MainFrameActions: Packaging (Android (ATC)): mono: [javac] 3 warnings
MainFrameActions: Packaging (Android (ATC)): mono: Duplicated project name in import. Project custom_rules defined first in /Users/AlekseiKanash/Projects/UE4_Plugin_Cortex_App_Store/ExampleProject/Intermediate/Android/APK/custom_rules.xml and again in /Users/AlekseiKanash/Projects/UE4_Plugin_Cortex_App_Store/ExampleProject/Intermediate/Android/APK/JavaLibs/d
ownloader_library/custom_rules.xml
MainFrameActions: Packaging (Android (ATC)): mono: [javac] warning: [options] source value 1.5 is obsolete and will be removed in a future release
MainFrameActions: Packaging (Android (ATC)): mono: [javac] warning: [options] target value 1.5 is obsolete and will be removed in a future release
MainFrameActions: Packaging (Android (ATC)): mono: [javac] warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.
MainFrameActions: Packaging (Android (ATC)): mono: [javac] 3 warnings
MainFrameActions: Packaging (Android (ATC)): mono:
MainFrameActions: Packaging (Android (ATC)): mono: BUILD FAILED
MainFrameActions: Packaging (Android (ATC)): mono: /Users/AlekseiKanash/NVPACK/android-sdk-macosx/tools/ant/build.xml:649: The following error occurred while executing this line:
MainFrameActions: Packaging (Android (ATC)): mono: /Users/AlekseiKanash/NVPACK/android-sdk-macosx/tools/ant/build.xml:694: null returned: 1
MainFrameActions: Packaging (Android (ATC)): mono:
MainFrameActions: Packaging (Android (ATC)): mono: Total time: 2 seconds
MainFrameActions: Packaging (Android (ATC)): mono: UnrealBuildTool Exception: ERROR: /bin/sh failed with args -c ‘"/Users/AlekseiKanash/NVPACK/apache-ant-1.9.7/bin/ant" -quiet debug’
MainFrameActions: Packaging (Android (ATC)): CommandUtils.Run: Run: Took 6.539012s to run mono, ExitCode=5

What does it means? Help please.