How to call java class and its method from JNI android?

54 views
Skip to first unread message

hariprasad

unread,
Jan 30, 2018, 10:31:13 AM1/30/18
to android-platform
Hi,

I want to call inner java class in my app 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.

Thank You
Hari

Вячеслав Смирнов

unread,
Feb 5, 2018, 10:34:45 AM2/5/18
to android-platform


вторник, 30 января 2018 г., 17:31:13 UTC+2 пользователь hariprasad написал:
Hi,

I want to call inner java class in my app 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;
        }
    }

}
<p style="margin-bottom: 1em; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: inher
Reply all
Reply to author
Forward
0 new messages