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

Is it possible to use Java Libraries in UE4 Game such as OUYA SDK or Google Play Services and Google Play Services?

P.S.: Is there support for a Eclipse project for your Android UE4 game?

It is possible, though the workflow isn’t great (not sure we can make it any better either, due to how JNI works).

Check out how Android’s LaunchURL implementation works in AndroidJNI.cpp and GameActivity.java for an example of how C++ thunks across to Java.

We will have limited (leaderboards and achievements) Google Play support coming in the 4.2 release (and you should be able to see the work-in-progress in the unstable branch on GitHub once the live mirroring turns on).

We don’t currently have any Eclipse support, not sure if there are any plans for it either. UBT could certainly be extended to generate Eclipse project files in addition to or instead of Visual Studio / Xcode projects.

Cheers,

Would it be possible for you to explain to me how I would go about adding a .jar to a UE4 game. Or point me to a resource.

So, this is more involved than you probably want, but an early (really, don’t use it quite yet :)) version of our Google Play services integration can be found at:

https://github.com/EpicGames/UnrealEngine/commit/122667ec999c55b8369c42e25bc4b473cfa5239f

Some of the key changes related to adding a .jar are in UEDeployAndroid.cs, where we copy the Google Play services library to our Intermediates, and then run ‘android.bat update’ on it.

Other important files are AndroidJNI.h/.cpp for calls across the C++/Java boundary, and AndroidManifest.xml, where we let the Android SDK build system know that we’re integrating the Google Play services library.

There may be a simpler approach for what you’re looking to do, and if you find something that works better, please let us know.

Creating an Android Plugin and calling jar files from c++

I had a look at the source files you suggested,
but I still have multiple questions

If I want to create a simple Android plugin:

where do I need to place the .jar files In my Android game project?

do I need to create UEDeployAndroid.cs, AndroidJNI.h/cpp equivalents for my plugin?

how are you loading the jar files in UEDeployAndroid?

I just need to load the android plugin in my c++ code and call a function from the jar file.

If it is possible can you please explain what are the steps to follow for making this kind of Plugin
so I have a clear understanding of how this is done in UE4

thanks

Thanks, looking at those commits was very helpful and finally managed to communicate Java <-> C++.
But then I’m facing the problem that the manifest and Android project folder are being rewritten every time the project is built, as explained here: AndroidManifest and Java files are rewritten everytime the project is built - Mobile - Epic Developer Community Forums

I just put all jar, libraries under C:\Program Files\Unreal Engine\4.1\Engine\Build\Android\Java

Full directory structure:
Engine\Build\Android\Java\jni\libmylib.so
Engine\Build\Android\Java\libs\mylib.jar
Engine\Build\Android\Java\libs\mylib.jar.properties (if you have)

[EDIT]
Also modified jni\Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := UE4
LOCAL_SRC_FILES := libUE4.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := mylib
LOCAL_SRC_FILES := libmylib.so
include $(PREBUILT_SHARED_LIBRARY)

[/EDIT]

So they are always copied to Intermediate directory during build time.
I also edited GameActivity.java there to load my library.

I’m afraid this is not a recommended way because I edited default installation.
Please let me know better and recommended way.

I do not believe there is a “better” way to do this.

Nobody has updated this for a while.

I managed to communicate between Blueprints (C++) and a external java library file. However, I only achieve this modifying AndroidJNI.cpp/h files. These files are part of the Engine and we don’t want to modify the Engine, just the Game project files.

There was another change related to the game’s java files (GameActivity.java). But that was easy. You just have your modified GameActivity.java in a replicated Build folder in your project’s folder. For example the Android.mk file to add more .so files, as Keewon Seo suggests. And that;s what I am looking at now. If I am able to create a .so file (library) from a similar implementation of the AndroidJNI.cpp/h files I will have this working without modifying the Engine code.

Has anybody achieved Java-C++ communication without modifying the engine code?

That GitHub - aajiwani/EasyNDK-for-cocos2dx: NDK is always a problem. Writing such a code that can become a breeze in connecting with other platforms is always a problem when working with cocos2dx. This repo is a step made forward to make this pain a simple step. is good example for communication platform specific native code and C++.

As far as the OUYA SDK, yes UE4 is now supported on OUYA. Check out the latest plugin and docs.

As for the eclipse support, after building your APK, there is an immediate output folder that contains the Android project that you can import and build from Eclipse after you’ve built from the UE4 editor. You can find the place in the output log. I didn’t find it necessary to do this other than to quickly fix any Java errors in the plugin during development.

i am not able to find the admob integration at the link provided .

Please provide the updated link for admob integration with unreal.

Bumping this thread
Has anyone been able to use their own Java library in UE4 by the book? I need to use Android level gyroscope functions. Still looking for the solution.

Yes I’ve been able to add Java and native libraries to Unreal. I documented and checked in the source here.

Let me know if you have any questions.

Thanks for the information. I’m about to look into the code from github. You have modified the Engine files to load Java libraries, but can you do the same with project files, so the changes remain between Engine versions?

I put my JAR libraries in the UnrealEngine\Engine\Build\Android\Java\libs folder.

You can put assets and res in your ProjectName\Build\Android subfolder. So maybe you could drop a jar in ProjectName\Build\Android\libs.

Then you would need to add JNI to your C++ code. And I didn’t do it that way because it was adding 20 minutes to my compile times. Adding to the engine code was much faster.

I decided to use JNI in my project. So I included Android NDK, but can’t compile it.

PublicIncludePaths.Add(“D:/NVPACK/android-ndk-r9c/platforms/android-19/arch-arm/usr/include”);

D:\NVPACK\android-ndk-r9c\platforms\android-19\arch-arm\usr\include\sys/cdefs.h(252): fatal error C1189: #error : “No function renaming possible”

Is it the right aproach to create a communication bridge between C++ and Android Java?

You’ll need to set the Application.mk to whatever platform you used.
https://github.com/tgraupmann/UnrealEngine/blob/4.7-OUYA/Engine/Build/Android/Java/jni/Application.mk

You put the Android-NDK in your path and run ndk-build on the command-line from:
https://github.com/tgraupmann/UnrealEngine/tree/4.7-OUYA/Engine/Build/Android/Java

I’ve finally found a solution to using JNI in my C++ code

Now I need to solve Java side of the riddle. Where to write my Java functions and how to include them in the build package…