Announcement

Collapse
No announcement yet.

JNI Issues

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    JNI Issues

    Hi Guys,

    Happy holidays.

    I am stuck with an issue. I am working on a project that needs me to communicate with GameActivity.Java from C++. I am calling a blueprint, that calls a Java function through C++ using JNI. I am having problems even declaring JNI objects like JNIEnv and JavaVM. I am using MacOS to develop this project and I am not modifying any source code. I am developing a plugin and am putting all my code into that plugin and right now, I am writing the code for Android.

    I am trying to something like this: https://answers.unrealengine.com/que...ckaging-a.html

    I am attaching the following:
    1) Error Message
    2) My plugin's build.cs file
    3) C++ Header File
    4) C++ source file


    Thanks Again
    d0d0

    Here is my error message:


    Code:
    Recompiling UObjectPlugin...
    Launching UnrealBuildTool... [/Users/Shared/UnrealEngine/4.13/Engine/Binaries/DotNET/UnrealBuildTool.exe MyProject -ModuleWithSuffix UObjectPlugin 1143 Mac Development -editorrecompile -canskiplink "/Users/d0d0/Documents/Projects/UnrealProjects/MyProject 4.13 - 6/MyProject.uproject" ]
    CompilerResultsLog: New page: Compilation - Dec 1, 2016, 2:24:34 AM
    CompilerResultsLog: Info 
    CompilerResultsLog: Info Running Mono...
    CompilerResultsLog: Info 
    CompilerResultsLog: Info Setting up Mono
    CompilerResultsLog: Info /Users/Shared/UnrealEngine/4.13/Engine /Users/Shared/UnrealEngine/4.13/Engine/Binaries/Mac
    CompilerResultsLog: Info Compiling game modules for hot reload
    CompilerResultsLog: Info Compiling with MacOSX SDK 10.12
    CompilerResultsLog: Info Parsing headers for MyProjectEditor
    CompilerResultsLog: Info   Running UnrealHeaderTool "/Users/d0d0/Documents/Projects/UnrealProjects/MyProject 4.13 - 6/MyProject.uproject" "/Users/d0d0/Documents/Projects/UnrealProjects/MyProject 4.13 - 6/Intermediate/Build/Mac/MyProjectEditor/Development/MyProjectEditor.uhtmanifest" -LogCmds="loginit warning, logexit warnin
    g, logdatabase error" -Unattended -WarningsAsErrors -installed
    CompilerResultsLog: Info Reflection code generated for MyProjectEditor in 6.6998022 seconds
    CompilerResultsLog: Info Performing 3 actions (4 in parallel)
    CompilerResultsLog: Info [1/3] Compile Module.UObjectPlugin.cpp
    CompilerResultsLog: Info [2/3] Compile UObjectPlugin.generated.cpp
    CompilerResultsLog: Info In file included from /Users/d0d0/Documents/Projects/UnrealProjects/MyProject 4.13 - 6/Plugins/UObjectPlugin/Intermediate/Build/Mac/UE4Editor/Inc/UObjectPlugin/UObjectPlugin.generated.cpp:9:
    CompilerResultsLog: Info In file included from ../../../../../d0d0/Documents/Projects/UnrealProjects/MyProject 4.13 - 6/Plugins/UObjectPlugin/Intermediate/Build/Mac/UE4Editor/Inc/UObjectPlugin/UObjectPlugin.generated.dep.h:9:
    CompilerResultsLog: Info ../../../../../d0d0/Documents/Projects/UnrealProjects/MyProject 4.13 - 6/Plugins/UObjectPlugin/Source/UObjectPlugin/Classes/MyPluginObject.h:19:8: error: unknown type name 'JavaVM'
    CompilerResultsLog: Info extern JavaVM *jvm;
    CompilerResultsLog: Info        ^
    CompilerResultsLog: Info In file included from /Users/d0d0/Documents/Projects/UnrealProjects/MyProject 4.13 - 6/Plugins/UObjectPlugin/Intermediate/Build/Mac/UE4Editor/Development/UObjectPlugin/Module.UObjectPlugin.cpp:2:
    CompilerResultsLog: Info In file included from /Users/d0d0/Documents/Projects/UnrealProjects/MyProject 4.13 - 6/Plugins/UObjectPlugin/Source/UObjectPlugin/Private/MyPluginObject.cpp:4:
    CompilerResultsLog: Info ../../../../../d0d0/Documents/Projects/UnrealProjects/MyProject 4.13 - 6/Plugins/UObjectPlugin/Source/UObjectPlugin/Classes/MyPluginObject.h:19:8: error: unknown type name 'JavaVM'
    CompilerResultsLog: Info extern JavaVM *jvm;
    CompilerResultsLog: Info        ^
    CompilerResultsLog: Info ../../../../../d0d0/Documents/Projects/UnrealProjects/MyProject 4.13 - 6/Plugins/UObjectPlugin/Source/UObjectPlugin/Classes/MyPluginObject.h:20:8: error: unknown type name 'JNIEnv'
    CompilerResultsLog: Info extern  JNIEnv *env;
    CompilerResultsLog: Info         ^
    CompilerResultsLog: Info ../../../../../d0d0/Documents/Projects/UnrealProjects/MyProject 4.13 - 6/Plugins/UObjectPlugin/Source/UObjectPlugin/Classes/MyPluginObject.h:20:8: error: unknown type name 'JNIEnv'
    CompilerResultsLog: Info extern  JNIEnv *env;
    CompilerResultsLog: Info         ^
    CompilerResultsLog: Info In file included from /Users/d0d0/Documents/Projects/UnrealProjects/MyProject 4.13 - 6/Plugins/UObjectPlugin/Intermediate/Build/Mac/UE4Editor/Development/UObjectPlugin/Module.UObjectPlugin.cpp:2:
    CompilerResultsLog: Info /Users/d0d0/Documents/Projects/UnrealProjects/MyProject 4.13 - 6/Plugins/UObjectPlugin/Source/UObjectPlugin/Private/MyPluginObject.cpp:32:2: error: unknown type name 'JavaVM'
    CompilerResultsLog: Info         JavaVM *jvm;
    CompilerResultsLog: Info         ^
    CompilerResultsLog: Info /Users/d0d0/Documents/Projects/UnrealProjects/MyProject 4.13 - 6/Plugins/UObjectPlugin/Source/UObjectPlugin/Private/MyPluginObject.cpp:33:2: error: unknown type name 'JNIEnv'
    CompilerResultsLog: Info         JNIEnv *env;
    CompilerResultsLog: Info         ^
    CompilerResultsLog: Info 2 errors generated.
    CompilerResultsLog: Info 4 errors generated.
    CompilerResultsLog: Info ERROR: UBT ERROR: Failed to produce item: /Users/d0d0/Documents/Projects/UnrealProjects/MyProject 4.13 - 6/Plugins/UObjectPlugin/Binaries/Mac/UE4Editor-UObjectPlugin-1143.dylib
    CompilerResultsLog: Info Total build time: 17.02 seconds
    LogMainFrame: MainFrame: Module compiling took 17.909 seconds
    Warning: HotReload failed, recompile failed
    Warning: RebindPackages failed because the compiler could not be started.
    2) Here is my projects build.cs file:

    Code:
    // Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
    
    namespace UnrealBuildTool.Rules
    {
    	public class UObjectPlugin : ModuleRules
    	{
    		public UObjectPlugin(TargetInfo Target)
    		{
    			PublicIncludePaths.AddRange(
    				new string[] {
    					// ... add public include paths required here ...
    					"/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/include/",
    					
    				}
    				);
    
    			PrivateIncludePaths.AddRange(
    				new string[] {
    					"Developer/UObjectPlugin/Private",
    					// ... add other private include paths required here ...
    						"../../../../Source/Runtime/Core/Private", 
    						"../../../../Source/Runtime/Launch/Private",
    						"../../../../Source/Runtime/Core/Private/Android", 
    						"../../../../Source/Runtime/Launch/Private/Android",
    						"/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/include/",
    				}
    				);
    
    			PublicDependencyModuleNames.AddRange(
    				new string[]
    				{
    					"Core",
    					"CoreUObject",
    					// ... add other public dependencies that you statically link with here ...
    					"Engine", 
    					"InputCore", 
    					"Launch", 
    				}
    				);
    
    			PrivateDependencyModuleNames.AddRange(
    				new string[]
    				{
    					// ... add private dependencies that you statically link with here ...
    					"Core",
    					"CoreUObject",
    					"Engine", 
    					"InputCore", 
    					"Launch", 
    				}
    				);
    
    			DynamicallyLoadedModuleNames.AddRange(
    				new string[]
    				{
    					// ... add any modules that your module loads dynamically here ...
    				}
    				);
    		}
    	}
    }
    3) Here is my Header file


    Code:
    // Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
    
    #pragma once
    
    #if PLATFORM_ANDROID
    	#include <jni.h>
    	#include "AndroidJNI.h"
    	#include "AndroidJava.h"
    	#include "AndroidApplication.h"
    	#include <Android/asset_manager.h>
    	#include <Android/asset_manager_jni.h>
    
    #endif
    
    
    #include "MyPluginObject.generated.h"
    
    
    extern JavaVM *jvm;
    extern	JNIEnv *env;
    
    
    /**
     * Example UStruct declared in a plugin module 
     */
    USTRUCT()
    struct FMyPluginStruct
    {
    	GENERATED_USTRUCT_BODY()
     
    	UPROPERTY()
    	FString TestString;
    };
     
    
    /**
     * Example of declaring a UObject in a plugin module
     */
    UCLASS()
    class UMyPluginObject : public UObject
    {
    	GENERATED_UCLASS_BODY()
    
    public:
    	
    	UFUNCTION( BlueprintCallable, Category = Karmik)
    		void callMapsActivity();
    		
    
    	
    		 void  AndroidThunkCpp_callMapsActivity();
    
    
    private:
    
    	UPROPERTY()
    	FMyPluginStruct MyStruct;
    
    };
    4) Here is my Source file

    Code:
    // Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
    
    #include "UObjectPluginPrivatePCH.h"
    #include "MyPluginObject.h"
    
    #if PLATFORM_ANDROID
    	#include <jni.h>
    	#include "AndroidJNI.h"
    	#include "AndroidJava.h"
    	#include "AndroidApplication.h"
    	#include <Android/asset_manager.h>
    	#include <Android/asset_manager_jni.h>
    
    #endif
    
    
    
    
    UMyPluginObject::UMyPluginObject( const FObjectInitializer& ObjectInitializer )
    	: Super( ObjectInitializer )
    {
    }
    
    
    void UMyPluginObject::callMapsActivity()
    {
    	// This is going to be the BluePrint function that calls AndroidThunkCpp_callMapsActivity()
    }
    
    
    
    	JavaVM *jvm;
    	JNIEnv *env;
    	
    void UMyPluginObject::AndroidThunkCpp_callMapsActivity()
    {
    	// This will call the java method to invoke MapsActivity
    	
    //   if (JNIEnv* Env = FAndroidApplication::GetJavaEnv())
    //   {
    //	       static jmethodID callMapsActivityMethod = FJavaWrapper::FindMethod(Env, FJavaWrapper::GameActivityClassID, "AndroidThunkJava_callMapsActivity", "(Z)V", false);
    //		   FJavaWrapper::CallVoidMethod(Env, FJavaWrapper::GameActivityThis, callMapsActivityMethod, false);
    //   }
    	
    }

    #2
    You should just need to include these:

    Code:
    #include "Android/AndroidJNI.h"
    #include "Android/AndroidApplication.h"
    And in the Build.cs you don't need the /Library, the Private/Android shouldn't be specified (you should do like above). I think this would do what you want:

    Code:
    			PublicDependencyModuleNames.AddRange(
    				new string[]
    				{
    					"Core",
    				}
    			);
    
    			PublicIncludePathModuleNames.AddRange(
    				new string[]
    				{
    					"Core",
    					"Launch",
    				}
    			);

    Comment


      #3
      Looks like you are compiling for MAC (and I'm not sure Android supports plugins does it?).

      jni.h is only included for Android.

      Or I'm missing something?

      Comment


        #4
        HI Chris,

        Thanks so much for your quick response.

        Like you suggested, I removed all the private dependencies and paths in the build.cs file and I also changed the includes in the header and source files to #include "Android/AndroidJNI.h"

        I am getting the same error as before.

        Here is the strange part, my other c++/Blueprint code is working fine. The moment, I try JNI related coding, I get these errors.

        Thanks
        d0d0


        Here are the code changes I made to the buil.cs file

        Code:
        // Copyright 1998-2016 HazardNetworking, Inc. All Rights Reserved.
        
        namespace UnrealBuildTool.Rules
        {
        	public class UObjectPlugin : ModuleRules
        	{
        		public UObjectPlugin(TargetInfo Target)
        		{
        			PublicIncludePaths.AddRange(
        				new string[] {
        					"Developer/UObjectPlugin/Public"
        					// ... add public include paths required here ...
        					//"/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/include/",
        					
        				}
        				);
        
        			PrivateIncludePaths.AddRange(
        				new string[] {
        					"Developer/UObjectPlugin/Private",
        					// ... add other private include paths required here ...
        					//	"../../../../Source/Runtime/Core/Private", 
        					//	"../../../../Source/Runtime/Launch/Private",
        					//	"../../../../Source/Runtime/Core/Private/Android", 
        					//	"../../../../Source/Runtime/Launch/Private/Android",
        					//	"/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/include/",
        				}
        				);
        
        			PublicDependencyModuleNames.AddRange(
        				new string[]
        				{
        					"Core",
        					"CoreUObject",
        					// ... add other public dependencies that you statically link with here ...
        					"Engine", 
        					"InputCore", 
        					"Launch", 
        				}
        				);
        
        				PublicIncludePathModuleNames.AddRange(
        					new string[]
        				{
        
        					"Core",
        					"CoreUObject",
        					// ... add other public dependencies that you statically link with here ...
        					"Engine", 
        					"InputCore", 
        					"Launch", 
        				}
        				);
        
        			PrivateDependencyModuleNames.AddRange(
        				new string[]
        				{
        					// ... add private dependencies that you statically link with here ...
        					//"Core",
        					//"CoreUObject",
        					//"Engine", 
        					//"InputCore", 
        					//"Launch", 
        				}
        				);
        
        			DynamicallyLoadedModuleNames.AddRange(
        				new string[]
        				{
        					// ... add any modules that your module loads dynamically here ...
        				}
        				);
        		}
        	}
        }

        Here is the error:

        Code:
        Recompiling UObjectPlugin...
        Launching UnrealBuildTool... [/Users/Shared/UnrealEngine/4.13/Engine/Binaries/DotNET/UnrealBuildTool.exe hazardnetworking -ModuleWithSuffix UObjectPlugin 1130 Mac Development -editorrecompile -canskiplink "/Users/d0d0/Documents/Projects/UnrealProjects/hazardnetworking 4.13 - 6/hazardnetworking.uproject" ]
        CompilerResultsLog: New page: Compilation - Dec 1, 2016, 5:09:06 AM
        CompilerResultsLog: Info 
        CompilerResultsLog: Info Running Mono...
        CompilerResultsLog: Info 
        CompilerResultsLog: Info Setting up Mono
        CompilerResultsLog: Info /Users/Shared/UnrealEngine/4.13/Engine /Users/Shared/UnrealEngine/4.13/Engine/Binaries/Mac
        CompilerResultsLog: Info Compiling game modules for hot reload
        CompilerResultsLog: Info Compiling with MacOSX SDK 10.12
        CompilerResultsLog: Info Parsing headers for hazardnetworkingEditor
        CompilerResultsLog: Info   Running UnrealHeaderTool "/Users/d0d0/Documents/Projects/UnrealProjects/hazardnetworking 4.13 - 6/hazardnetworking.uproject" "/Users/d0d0/Documents/Projects/UnrealProjects/hazardnetworking 4.13 - 6/Intermediate/Build/Mac/hazardnetworkingEditor/Development/hazardnetworkingEditor.uhtmanifest" -LogCmds="loginit warning, logexit warnin
        g, logdatabase error" -Unattended -WarningsAsErrors -installed
        CompilerResultsLog: Info Reflection code generated for hazardnetworkingEditor in 7.2823505 seconds
        CompilerResultsLog: Info Performing 3 actions (4 in parallel)
        CompilerResultsLog: Info [2/3] Compile UObjectPlugin.generated.cpp
        CompilerResultsLog: Info [1/3] Compile Module.UObjectPlugin.cpp
        CompilerResultsLog: Info In file included from /Users/d0d0/Documents/Projects/UnrealProjects/hazardnetworking 4.13 - 6/Plugins/UObjectPlugin/Intermediate/Build/Mac/UE4Editor/Inc/UObjectPlugin/UObjectPlugin.generated.cpp:9:
        CompilerResultsLog: Info In file included from ../../../../../d0d0/Documents/Projects/UnrealProjects/hazardnetworking 4.13 - 6/Plugins/UObjectPlugin/Intermediate/Build/Mac/UE4Editor/Inc/UObjectPlugin/UObjectPlugin.generated.dep.h:9:
        CompilerResultsLog: Info In file included from /Users/d0d0/Documents/Projects/UnrealProjects/hazardnetworking 4.13 - 6/Plugins/UObjectPlugin/Intermediate/Build/Mac/UE4Editor/Development/UObjectPlugin/Module.UObjectPlugin.cpp:2:
        CompilerResultsLog: Info In file included from /Users/d0d0/Documents/Projects/UnrealProjects/hazardnetworking 4.13 - 6/Plugins/UObjectPlugin/Source/UObjectPlugin/Private/MyPluginObject.cpp:4:
        CompilerResultsLog: Info ../../../../../d0d0/Documents/Projects/UnrealProjects/hazardnetworking 4.13 - 6/Plugins/UObjectPlugin/Source/UObjectPlugin/Classes/MyPluginObject.h:19:8: error: unknown type name 'JavaVM'
        CompilerResultsLog: Info ../../../../../d0d0/Documents/Projects/UnrealProjects/hazardnetworking 4.13 - 6/Plugins/UObjectPlugin/Source/UObjectPlugin/Classes/MyPluginObject.h:19:8: error: unknown type name 'JavaVM'
        CompilerResultsLog: Info extern JavaVM *jvm;
        CompilerResultsLog: Info        ^
        CompilerResultsLog: Info extern JavaVM *jvm;
        CompilerResultsLog: Info        ^
        CompilerResultsLog: Info ../../../../../d0d0/Documents/Projects/UnrealProjects/hazardnetworking 4.13 - 6/Plugins/UObjectPlugin/Source/UObjectPlugin/Classes/MyPluginObject.h:20:8: error: unknown type name 'JNIEnv'
        CompilerResultsLog: Info extern  JNIEnv *env;
        CompilerResultsLog: Info         ^
        CompilerResultsLog: Info ../../../../../d0d0/Documents/Projects/UnrealProjects/hazardnetworking 4.13 - 6/Plugins/UObjectPlugin/Source/UObjectPlugin/Classes/MyPluginObject.h:20:8: error: unknown type name 'JNIEnv'
        CompilerResultsLog: Info extern  JNIEnv *env;
        CompilerResultsLog: Info         ^
        CompilerResultsLog: Info In file included from /Users/d0d0/Documents/Projects/UnrealProjects/hazardnetworking 4.13 - 6/Plugins/UObjectPlugin/Intermediate/Build/Mac/UE4Editor/Development/UObjectPlugin/Module.UObjectPlugin.cpp:2:
        CompilerResultsLog: Info /Users/d0d0/Documents/Projects/UnrealProjects/hazardnetworking 4.13 - 6/Plugins/UObjectPlugin/Source/UObjectPlugin/Private/MyPluginObject.cpp:32:2: error: unknown type name 'JavaVM'
        CompilerResultsLog: Info         JavaVM *jvm;
        CompilerResultsLog: Info         ^
        CompilerResultsLog: Info /Users/d0d0/Documents/Projects/UnrealProjects/hazardnetworking 4.13 - 6/Plugins/UObjectPlugin/Source/UObjectPlugin/Private/MyPluginObject.cpp:33:2: error: unknown type name 'JNIEnv'
        CompilerResultsLog: Info         JNIEnv *env;
        CompilerResultsLog: Info         ^
        CompilerResultsLog: Info 2 errors generated.
        CompilerResultsLog: Info 4 errors generated.
        CompilerResultsLog: Info ERROR: UBT ERROR: Failed to produce item: /Users/d0d0/Documents/Projects/UnrealProjects/hazardnetworking 4.13 - 6/Plugins/UObjectPlugin/Binaries/Mac/UE4Editor-UObjectPlugin-1130.dylib
        CompilerResultsLog: Info Total build time: 21.04 seconds
        LogMainFrame: MainFrame: Module compiling took 22.082 seconds

        Comment


          #5
          Hi theonecalledatom,

          From what I understand: there are two c++ compilers used by unreal. Unreal uses clang to compile itself including plugins and other c++ coding. Then for sections that says, android only, it uses another compiler for android NDK, g++.

          I understand what you are pointing to, let me try moving the jni.h out of the android specific section and rerun the code.

          Thanks.
          d0d0

          Comment


            #6
            Yes, Mac will be compiled by Xcode and Android by clang from the NDK toolchain.

            You need to protect any references to Android dependencies with #if PLATFORM_ANDROID / #endif or it will fail to compile for anything but Android.

            Comment


              #7
              All,

              I am able to get the code to work now. These are the changes I made:

              1) I had to do all my JNI or Android specific coding between #if PLATFORM_ANDROID and #endif. Some of my coding was out of this platform specific structure and it did not compile.
              2) In my build.cs file, I removed the dependencies on the module 'Launch'. For some reason, I get a linker error whenever I include 'Launch' module. Here is a link to this issue: https://answers.unrealengine.com/que...#answer-531520

              But for now, I am able to proceed with my coding.

              Thanks Again for all your help.
              d0d0

              Comment


                #8
                Originally posted by d0d0 View Post
                2) In my build.cs file, I removed the dependencies on the module 'Launch'. For some reason, I get a linker error whenever I include 'Launch' module. Here is a link to this issue: https://answers.unrealengine.com/que...#answer-531520
                That's because the Launch module is meant for Engine-only use I believe, not standalone projects or plugins. I made the same mistake as well.

                Comment


                  #9
                  Ok... Thanks. that makes sense

                  Comment


                    #10
                    Originally posted by _Auron_ View Post
                    That's because the Launch module is meant for Engine-only use I believe, not standalone projects or plugins. I made the same mistake as well.
                    Every plugin I've created for Android requires the Launch module. You may need to wrap the inclusion of the Launch module in a conditional statement in your build.cs file. So...

                    if (Target.Platform == UnrealTargetPlatform.Android)
                    {
                    PrivateDependencyModuleNames.AddRange(new string[] { "Launch" });
                    }

                    Co-Founder - Get Set Games | Twitter | LinkedIn

                    Comment

                    Working...
                    X