JNIHelper isn't aware of java class

I’m trying to call a java function from a C++ class. Here’s what I’ve done:

1.) Created a class in Android Studio called “AdHelper” in the app/src/com/myCompany folder. It has a public static function “test” which in theory should log the message “Please let me just have this one success today.”

package com.myCompany;

public class AdHelper {
    public static void test()
    {
        System.out.println("Please let me just have this one success today.");
    }
}

2.) In my C++ class I have the following code:

    JniMethodInfo t;
    if (JniHelper::getStaticMethodInfo(t,
                                       "AdHelper",
                                       "test",
                                       "()V")) {

        t.env->CallStaticVoidMethod(t.classID, t.methodID);
        t.env->DeleteLocalRef(t.classID);
    }

3.) In the progaurd-rules.pro file I added:

-keep public class com.myCompany.** (*;}

And I get the error:

E/JniHelper: Classloader failed to find class of AdHelper

TLDR; What do I need to do to allow JniHelper to find my java class?

There is nothing to do special for JniHelper. Your code seems correct.

Does it work without ProGuard activated? If so, your proguard-rules.pro isn’t correct. A better solution for custom JniHelper Java functions, is to annotate the functions with @Keep. So you can do fine grained ProGuard configuration. But this is only my opinion about ProGuard.

Maybe try in the second argument to put more than just AdHelper, ie: app/src/com/myCompany/AdHelper… or whatever the correct path is.

For example, one of my getStaticMethodInfo uses org/cocos2dx/cpp/AnalyticsHelper for a specific class/function.

1 Like

@mars3142 @tdebock How can I determine if proguard is or isn’t enabled? What build or runtime indicators are there? Second, it’s all a bit confusing. There seems to be several answers regarding disabling proguard which range from deleting the rules file itself, to adding certain rules in the rules file, to setting minifyEnabled to false in the build.gradle (of which there are 3 in a cocos2dx project).

Also, there is a proguard-project.txt file referenced in the project.properties file (which doesn’t seem to exist and I assume should’ve been removed from the cocos2dx project template).

I should add that I use CMake for my C++ dependencies. And I added the AdHelper class through the Android Studio UI. I can see the AdHelper class in the project view in Android Studio. Does this mean that it is included in my build?

This is my first Android app and it’s been a struggle learning how a cocos2dx project comes together. So sorry about the length!

Did you try my recommendation? JniHelper might have a hard time just ‘finding’ AdHelper without the specification of the path instead com/myCompany/AdHelper

@tdebock Yes, I did try that to no effect, but again nothing I have done has effected the outcome in any way. I guess I’ll try and comment out all of the rules that were automatically added by cocos2dx or SDKBOX.

@tdebock That fixed it! Thank you. I thought you meant change the path in the proguard. In my frustration I wasn’t reading clearly enough.

1 Like

awesome, yea my wording might not have been the greatest… sometimes it is hard to communicate with a string of characters… glad it worked out for you

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.