Создание экземпляра c ++ в JNI Layer, передача его в класс Java как длинный объект, но методы возвращают значение мусора - PullRequest
0 голосов
/ 29 мая 2020

Я новичок в концепции JNI и концепции взаимодействия между c ++ и Java. Я создал класс Numbers и хотел добавить оболочку java к абстрактному собственному слою. Я создал java класс Maths. java и добавил функции, которые мне нужно вызывать, и вызывал собственные методы для каждого метода в Java. Прежде всего, я создал экземпляр класса Maths в MainActivity. java, и когда мы создаем объект класса Maths, он вызывает собственный метод для создания экземпляра Numbers в c ++ и устанавливает длинное значение для указателей Numbers в Java . Я вызываю различные встроенные функции, используя это длинное значение в native-lib. cpp (на уровне JNI), приводя его к классу C ++. Но когда я вызываю эти функции, я получаю мусор.

Я прикрепляю код. Может кто-нибудь, пожалуйста, скажите мне точную проблему и почему она не работает.

Основная деятельность. java

package com.example.maths2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    public String TAG = "MainActivity.java";
    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

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


        //testing Maths class

        Log.d(TAG, "Creating Maths Object: ");

        Maths math = new Maths(3, 5);

        Log.d(TAG, "Calling addition: ");

        int addResult = math.add();

        Log.d(TAG, "Calling subtraction: ");

        int subResult = math.sub();

        Log.d(TAG, "Calling Multiplication: ");

        int mulResult = math.mul();

        //deleting c++ object

        math.destroy();

        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text);

        //Testing the values
        tv.setText(stringFromJNI() + "Add: " + addResult + "Sub: " + subResult + "Mul : " + mulResult);
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}

Математика. java

package com.example.maths2;

public class Maths {

    private int a, b;

    private long numberptr = 0L; //Stores c++ pointer to Numbers object

    static {
        System.loadLibrary("native-lib");
    }

    public Maths(int a, int b) {
        a = a;
        b = b;
        createNumberInstance(a, b);
    }

    public int add() {
        return nativeAdd();
    }

    public int sub() {
        return nativeSub();
    }

    public int mul() {
        return nativeMul();
    }

    public void destroy() {
        numberptr = 0L;
        nativeDestroy();

    }

    //Native Methods
    public native void createNumberInstance(int a, int b);

    public native int nativeSub();

    public native int nativeAdd();

    public native int nativeMul();

    public native void nativeDestroy();
}

CmakeLists.txt

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             native-lib.cpp

        Numbers.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

native-lib . cpp

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

// Get pointer field
jfieldID getPtrFieldId(JNIEnv * env, jobject obj)
{
    static jfieldID ptrFieldId = 0;

    if (!ptrFieldId)
    {
        jclass c = env->GetObjectClass(obj);
        ptrFieldId = env->GetFieldID(c, "numberptr", "J");
        env->DeleteLocalRef(c);
    }

    return ptrFieldId;
}

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


extern "C"
JNIEXPORT void JNICALL
Java_com_example_maths2_Maths_createNumberInstance(JNIEnv *env, jobject thiz, jint a, jint b) {
    // TODO: implement createNumberInstance()

    env->SetLongField(thiz, getPtrFieldId(env, thiz), (jlong) new Numbers(a, b));

}

extern "C"
JNIEXPORT jint JNICALL
Java_com_example_maths2_Maths_nativeSub(JNIEnv *env, jobject thiz) {
    // TODO: implement nativeSub()

    Numbers* numb = (Numbers*) env->GetLongField(thiz, getPtrFieldId(env, thiz));

    return numb->add();

}

extern "C"
JNIEXPORT jint JNICALL
Java_com_example_maths2_Maths_nativeAdd(JNIEnv *env, jobject thiz) {
    // TODO: implement nativeAdd()

    Numbers* numb = (Numbers*) env->GetLongField(thiz, getPtrFieldId(env, thiz));

    return numb->sub();

}

extern "C"
JNIEXPORT jint JNICALL
Java_com_example_maths2_Maths_nativeMul(JNIEnv *env, jobject thiz) {
    // TODO: implement nativeMul()

    Numbers* numb = (Numbers*) env->GetLongField(thiz, getPtrFieldId(env, thiz));

    return numb->mul();
}

extern "C"
JNIEXPORT void JNICALL
Java_com_example_maths2_Maths_nativeDestroy(JNIEnv *env, jobject thiz) {
    // TODO: implement nativedestroy()

    Numbers* numb = (Numbers*) env->GetLongField(thiz, getPtrFieldId(env, thiz));

    delete numb;
}

Числа. cpp

#include "Numbers.h"

Numbers::Numbers(int a, int b) {
    a = a;
    b = b;
}

int Numbers::add() {
    return a+b;
}

int Numbers::mul() {
    return a*b;
}

int Numbers::sub() {
    return a-b;
}

Numbers.h

//
//
//

#ifndef MATHS_NUMBERS_H
#define MATHS_NUMBERS_H


class Numbers {
    int a, b;

public:
    Numbers(int,int);
    int add();
    int mul();
    int sub();
};


#endif //MATHS_NUMBERS_H

Пожалуйста, скажите мне, что нужно сделать или можно исправить вещи, которые идут не так.

...