1

I want to call inner java class in my cpp class.

here is my simple java class(Activity)

 public class MainActivity extends AppCompatActivity {

    static {

        System.loadLibrary("myLibrary");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toast.makeText(this, "" + stringFromJNI(), Toast.LENGTH_SHORT).show();
        checkAES();
        checkRSA();
        invokeclass();
        Log.d("Gajanand", "onCreate: "+invokeclass());
    }
   public native String stringFromJNI();

    public native int checkRSA();

    public native void checkAES();

    public native int invokeclass();

    public static class bar {
        public static int timesTen(int input){
            return input * 10;
        }
    }

}

and my cpp class is like below

#include<jni.h>
#include <string>

#include <android/log.h>
#include <openssl/rsa.h>
#include <openssl/aes.h>


extern "C"
JNIEXPORT jstring  JNICALL
Java_com_manvish_bwssb_Activities_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}
#define nr_bits 2048

extern "C"
JNIEXPORT int JNICALL
  Java_com_manvish_bwssb_Activities_MainActivity_checkRSA(JNIEnv *env, jobject /* this */) {
    RSA *rsa = RSA_generate_key(nr_bits, 65537, NULL, NULL);
    return 0;
}

static const unsigned char key[] = {
        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
};

extern "C"
void Java_com_manvish_bwssb_Activities_MainActivity_checkAES(JNIEnv *env, jobject /* this */) {

    unsigned char text[] = "Kishan of Nanda";
    unsigned char out[80];
    unsigned char decout[80];

    AES_KEY wctx;

    AES_set_encrypt_key(key, 128, &wctx);
    AES_encrypt(text, out, &wctx);
    printf("encryp data = %s\n", out);
    __android_log_print(ANDROID_LOG_DEBUG, "Gajanand", "Encrypted data: %s\n", out);

    AES_set_decrypt_key(key, 128, &wctx);
    AES_decrypt(out, decout, &wctx);

    printf(" Decrypted o/p: %s \n", decout);
    __android_log_print(ANDROID_LOG_DEBUG, "Gajanand", "Decrypted data: %s\n", decout);
}
extern "C"
JNIEXPORT int JNICALL
 Java_com_manvish_bwssb_Activities_MainActivity_invokeclass(JNIEnv *env)
{
    jclass myClass = env->FindClass("bar");
    jmethodID mid = env->GetStaticMethodID(myClass, "timesTen", "(I)I");
    jint hundred = env->CallStaticIntMethod(myClass, mid, (jint)10);
    return hundred;
}

what happening in my class is checkRSA() and checkAES() is calling repeatedly. i am not getting why only these two methods calling repeatedly.but invokeclass() method is not calling at all.is this correct way to call java class or is there any other method.? and another problem in my app is building app taking more than 3 minutes.help me to solve these both issue.

Gaju Kollur
  • 2,046
  • 5
  • 23
  • 47

1 Answers1

0

You didn't specify path to your bar class, just put your package path, replace . with / and since it is inner class - you should access it using YourClass$YourInnerClass .

So in your case it is something like that:

jclass myClass = env->FindClass("com/mavish/bwssb/Activities/MainActivity$bar");

Also please note that env->FindClass and similar calls DO cause exceptions, so read carefully through Exception Handling in JNI

Another common reason for that problem is minifiers, so check your Proguard rules. Make sure they don't delete any native calls or your static Bar class (If it isn't invoked anywhere in java - it would likely be deleted on minify stage)

How to keep native methods

How to keep inner classes

Regarding your very long build time - I would say question is too broad, because there are plenty of possible issues which make build time long. I would recommend though modifying gradle.properties in your root project folder, adding those parameters:

org.gradle.parallel=true 
org.gradle.daemon=true 
org.gradle.jvmargs=-Xmx8192m -XX:MaxPermSize=4096 -XX:+HeapDumpOnOutOfMemoryError 
Dmitrii Z.
  • 2,287
  • 3
  • 19
  • 29