【问题标题】:Android JNI: Java calling a C++ class object via JNIAndroid JNI:Java 通过 JNI 调用 C++ 类对象
【发布时间】:2018-01-29 03:50:24
【问题描述】:

Android Studio 项目: 我试图通过从java传递一个变量(整数,双精度...)来调用一个C++类对象,并使用JNI接口从C++类对象返回一个结果(双精度)。当我编译/构建项目时,没有错误或警告,它编译得很好。但是,当我运行应用程序时,它崩溃了“不幸的是,App_xxx 已停止”。我已经追踪到我调用对象的方式(在 java 和 NTI 文件中)的错误。

我是否从 MainActivity 和 JNI 接口中正确调用了该对象?谁能帮忙看看我的代码有什么问题?

这是 MainActivity java 的代码:

package com.abc.www.apps;

import ...

public class MainActivity extends AppCompatActivity {


   double ax, bx, cx;
   double totalA, totalB;

   // 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);

      //Call object in .cpp file (C++)
      ax = 2;
      bx = 3;
      cx = 4;
      totalA = getSum(ax, bx, cx);
      totalB = getDiff(ax, cx);

      //Display results
      TextView tv = (TextView) findViewById(R.id.id_dispTotA);
      tv.setText(Double.toString(totalA));

      TextView tv = (TextView) findViewById(R.id.id_dispTotB);
      tv.setText(Double.toString(totalB));
   }

   /**
    * A native method that is implemented by the 'native-lib' native library,
    * which is packaged with this application.
   */
   public native double getSum(double ax, double bx, double cx);
   public native double getDiff(double ax, double cx);
}

这是native-lib.cpp的代码:

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

extern "C"
JNIEXPORT jdouble JNICALL
Java_com_abc_www_apps_MainActivity_getSum(
    JNIEnv *env, jobject,
    jfloat a, jfloat b, jfloat c) {

    jdouble value;
    mathResult m;

    value = m.getSum(a, b, c);
    return value;
}

JNIEXPORT jdouble JNICALL
Java_com_abc_www_apps_MainActivity_getDiff(
    JNIEnv *env, jobject,
    jfloat a, jfloat c) {

    jdouble value;
    mathResult m;

    value = m.getDiff(a, c);
    return value;
}

这是 C++ 类对象 (.cpp) 的代码:

#include "mathResult.h"
#include "math.h"

//Sum
double mathResult::getSum(double a, double b, double c)
{
   Sum = a + b + c;
   return Sum;
}

//Subtraction
double mathResult::getDiff(double a, double c)
{
   Diff = c - a;
   return Diff;
}

任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: java android c++ java-native-interface


    【解决方案1】:

    MainActivity

     public class MainActivity extends AppCompatActivity {
    
            // Used to load the 'native-lib' library on application startup.
            static {
                System.loadLibrary("native-lib");
            }
    
    
            double ax, bx, cx;
            double totalA, totalB;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
    
    
                ax = 2;
                bx = 3;
                cx = 4;
                totalA = getSum(ax, bx, cx);
                totalB = getDiff(ax, cx);
    
    
                // Example of a call to a native method
                TextView tv = (TextView) findViewById(R.id.sample_text);
                tv.setText(Double.toString(totalA) + " === " + Double.toString(totalB));
            }
    
            /**
             * A native method that is implemented by the 'native-lib' native library,
             * which is packaged with this application.
             */
            public native String stringFromJNI();
    
            public native double getSum(double ax, double bx, double cx);
    
            public native double getDiff(double ax, double cx);
        }
    

    Nativelib.cpp

    #include <jni.h>
    #include <string>
    #include "math.h"
    
    
    extern "C"
    JNIEXPORT jstring
    
    JNICALL
    Java_com_abc_www_apps_MainActivity_stringFromJNI(
            JNIEnv *env,
            jobject /* this */) {
        std::string hello = "Hello from C++";
        return env->NewStringUTF(hello.c_str());
    }
    
    
    
    extern "C"
    JNIEXPORT jdouble JNICALL
    Java_com_abc_www_apps_MainActivity_getSum(JNIEnv *env, jobject instance, jdouble ax,
                                                    jdouble bx, jdouble cx) {
    
        return math().getSum(ax, bx, cx);
    
    }extern "C"
    JNIEXPORT jdouble JNICALL
    Java_com_abc_www_apps_MainActivity_getDiff(JNIEnv *env, jobject instance, jdouble ax,
                                                     jdouble cx) {
    
        return math().getDiff(ax, cx);
    
    }
    

    ma​​th.h

    #ifndef JNIDEMO_MATH_H
    #define JNIDEMO_MATH_H
    
    
    #include <jni.h>
    
    class math {
    
    public:
        jdouble  getSum(jdouble ax, jdouble bx, jdouble cx);
    
        jdouble  getDiff(jdouble ax,jdouble bx);
    };
    
    
    #endif //JNIDEMO_MATH_H
    

    数学.cpp

    #include <jni.h>
    #include "math.h"
    
    
    
    
    jdouble math::getSum(jdouble ax, jdouble bx, jdouble cx) {
        return ax+bx+cx;
    }
    
    jdouble math::getDiff(jdouble ax, jdouble bx) {
        return ax-bx;
    }
    

    注意: 例如,您必须在 CmakerLists.txt 中添加新添加的文件 ma​​th.cpp 更改cmakerLists.txt 代码,如下所示以添加文件

    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).
                 src/main/cpp/native-lib.cpp
                  src/main/cpp/math.cpp)
    

    这里我用 add_library 添加了 src/main/cpp/math.cpp

    【讨论】:

    • jignesh,非常感谢您的快速回复。它对我有用,再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    相关资源
    最近更新 更多