JNIEnv problem

I'm following Adam's slides on how to work with JUCE and native Android widgets. It was all going very well until I tried to use the JNIHelpers. As soon as I add the following to my header:

#if defined(JUCE_ANDROID)

namespace juce
//#include "juce_android_JNIHelpers.h"
#include "../JuceLibraryCode/modules/juce_core/native/juce_android_JNIHelpers.h"


I get a plethora of errors such as the one posted below:

     [exec] In file included from jni/../../../Source/AndroidStandalone/AndroidStandaloneFilterApp.cpp:14:0:
     [exec] ../../JuceLibraryCode/../JuceLibraryCode/modules/juce_core/native/juce_android_JNIHelpers.h:37:8: error: 'JNIEnv' does not name a type
     [exec]  extern JNIEnv* getEnv() noexcept;

Any ideas? JUCE_ANDROID_ACTIVITY_CLASSNAME and JUCE_ANDROID_ACTIVITY_CLASSPATH were set when I created the project with the Introjucer. As far as I can tell they seem right. JUCE_ANDROID is also set. Is there anything else I should check for? 

[edit] It's clear it's not finding jni.h. I'm kind of wary of adding an explicit include path to jni.h as I've got so many of them on my machine. Should I use the java one, or one of those that comes with the various Android ndks? locate jni.h gives me a lot of jni.h's :|

[edit2] Ok, I was wrong. The build is finding jni.h, just as it should be. Back to the drawing board. 


I'm not sure you need to explicitly include the juce_android_JNIHelpers.h file - that gets including with the JuceHeader.h no?

I've not actually done any work with the JNIHelpers, as I've not had to have C++ call custom Java functions as yet. But there is some info in this thread (you may have seen it already): http://www.juce.com/forum/topic/communication-between-java-and-c




Thanks Adam. I was assuming you were as you've included the headers in your project. Let me see how far I get when I removed those includes. I've read over that thread you referenced quite a bit. It's definitely helping me understand what's expected. I'll let you know how I get on. Thanks again for posting your slides. I look forward to seeing the talk.

I'm only interested in called a C++ method from Java. I've no intention of going the other way around! I'm getting a new error now after removing the JNI helper headers.

     [exec] <command-line>:0:33: error: 'com_yourcompany_cabbageandroid_CabbageAndroid' has not been declared
     [exec] jni/../../../Source/AndroidStandalone/AndroidStandaloneFilterWindow.h:355:20: note: in expansion of macro 'JUCE_ANDROID_ACTIVITY_CLASSNAME'
     [exec]  JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, setMessage, void, (JNIEnv* env, jclass, jstring message))

 The following are defined:

-D "JUCE_ANDROID_ACTIVITY_CLASSNAME=com_yourcompany_cabbageandroid_CabbageAndroid"
-D JUCE_ANDROID_ACTIVITY_CLASSPATH=\"com/yourcompany/cabbageandroid/CabbageAndroid\"

The offending C++ code is the same that your example uses in its setMessage() function.

JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, setMessage, void, (JNIEnv* env, jclass, jstring message))


I'll keep digging. I can also try not using the JUCE_JNI_CALLBACK macro but I would prefer to use it if I can. 

Looks like it might be a project configuration issue, check that your generated AndroidManifest file has the correct acitivity set?

Everything looks in order there. The package is


And the activity is:


Coupled with the classname and classpath I posted earlier I think everything looks fine. I'm starting to think that I NEED the jni helpers header. If you have a spare moment, would you mind commented that include out from your project and seeing if everything still compiles Ok? I'm having problems running your project with my version of Android Studio, but I can try again if it you don't have the time. Thanks.


I tried exporting a brand new project and it seems to work in fine, so now I know for sure it's an issue with my existing project. Something has obviously gone astray along the way. A fix is immanent!


[edit] looks like the issue relates to earlier versions of JUCE. I just tried with a fresh project using 3.2.0 and I get the same errors as before.

Yes that project was done using JUCE 4 - its generally good to be on the latest tip ! 

I've tried everything I could to use the JUCE_JNI_CALLBACK macro but it's just not happening. For now I'm going to roll my own as Phil did in the thread you mentioned.

Have you tried loading my example project in  Introjucer (latest version)  and re-saving the Android Studio project? 

That's a good idea. But I think the problem is the complexity of my project. Actually, tardiness is a better word. I'm trying to port a fairly large desktop project straight to Android. I was able to get past the issues I posted by creating my own JNI code. It's all working nicely now. I have a Android-like home page that users can launch their instruments from, that's all I was looking to do. Thanks again for the help and good luck with your own work.