创建一个C ++类并在另一个C ++类中使用该类,该类包含ndk android中的JNI方法

Amjad Khan 07/27/2017. 1 answers, 51 views
android android c++ android-ndk android-ndk native

在Android中,我面临着在原生Android中连接两个cpp类的问题。 我已经测试过这个单独的类,它工作正常。

但是当我创建另一个文件,现在面临与当前cpp文件链接的问题。

MainClass.cpp

#include #include "native-handler.h"

extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_fragment_SampleFragment_setTitle(JNIEnv *env,jobject instance,jobject context) {

    ClassNativeHandler classNativeHandler;
    return classNativeHandler.getType(env,context);
} 

native-handler.cpp

#include #include "native-handler.h"

jstring jstringObject;

jstring ClassNativeHandler::getType(JNIEnv *env, jobject contextObject) {
    jstring jstringObject = env->NewStringUTF("Hello world");
    return jstringObject;
}

void ClassNativeHandler::setType(jstring string) {
    myType = string;
    jstringObject = string;
} 

native-handler.h

#ifndef SAMPLE_NATIVE_HANDLER_H
#define SAMPLE_NATIVE_HANDLER_H

#include #include class ClassNativeHandler
{
private:
    jstring myType;

public:
    void setType(jstring string);
    jstring getType(JNIEnv *env, jobject contextObject);
    jstring getHeaderName(JNIEnv *env);

};

#endif //SAMPLE_NATIVE_HANDLER_H 

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := Native
LOCAL_SRC_FILES := ../cpp/mainClass.cpp
LOCAL_C_INCLUDES := ../cpp/native-handler.h
LOCAL_LDLIBS := -lz -llog -ljnigraphics
include $(BUILD_SHARED_LIBRARY) 

错误:未定义引用`ClassNativeHandler :: getType(_JNIEnv *,_jobject *)'

在这一行返回classNativeHandler.getType(env,context);

所以我无法构建.so文件。 请指导我

1 Comments
Joe C 07/27/2017
这个问题与Java有什么关系?

1 Answers


taskinoor 07/27/2017.

你还没有编译native-handler.cpp 。 您需要在LOCAL_SRC_FILES指定它,以便NDK编译它。

LOCAL_SRC_FILES := ../cpp/mainClass.cpp ../cpp/native-handler.cpp

3 comments
Amjad Khan 07/27/2017
现在我可以编译该文件,但在运行应用程序时出现错误,如{JNI DETECTED ERROR IN APPLICATION:使用已删除的本地引用0x7f5bc55a3d}
taskinoor 07/27/2017
ClassNativeHandler::getType里面,你已经定义了本地jstring jstringObject有效地映射了可能导致无效本地引用错误的全局jstringObject
Amjad Khan 07/27/2017
它不存储在内存中时,再次指向获取null的位置,因此在ClassNativeHandler::getType上得到错误这是使用locai变量jstring jstringObject

HighResolutionMusic.com - Download Hi-Res Songs

1 (G)I-DLE

POP/STARS flac

(G)I-DLE. 2018. Writer: Riot Music Team;Harloe.
2 Ariana Grande

​Thank U, Next flac

Ariana Grande. 2018. Writer: Crazy Mike;Scootie;Victoria Monét;Tayla Parx;TBHits;Ariana Grande.
3 Clean Bandit

Baby flac

Clean Bandit. 2018. Writer: Jack Patterson;Kamille;Jason Evigan;Matthew Knott;Marina;Luis Fonsi.
4 Imagine Dragons

Bad Liar flac

Imagine Dragons. 2018. Writer: Jorgen Odegard;Daniel Platzman;Ben McKee;Wayne Sermon;Aja Volkman;Dan Reynolds.
5 Halsey

Without Me flac

Halsey. 2018. Writer: Halsey;Delacey;Louis Bell;Amy Allen;Justin Timberlake;Timbaland;Scott Storch.
6 BTS

Waste It On Me flac

BTS. 2018. Writer: Steve Aoki;Jeff Halavacs;Ryan Ogren;Michael Gazzo;Nate Cyphert;Sean Foreman;RM.
7 Nicki Minaj

No Candle No Light flac

Nicki Minaj. 2018. Writer: Denisia “Blu June” Andrews;Kathryn Ostenberg;Brittany "Chi" Coney;Brian Lee;TJ Routon;Tushar Apte;ZAYN;Nicki Minaj.
8 Backstreet Boys

Chances flac

Backstreet Boys. 2018.
9 BlackPink

Kiss And Make Up flac

BlackPink. 2018. Writer: Soke;Kny Factory;Billboard;Chelcee Grimes;Teddy Park;Marc Vincent;Dua Lipa.
10 Little Mix

The Cure flac

Little Mix. 2018. Writer: Pete Kelleher;Camille Purcell;Tom Barnes;Ben Kohn.
11 Fitz And The Tantrums

HandClap flac

Fitz And The Tantrums. 2017. Writer: Fitz And The Tantrums;Eric Frederic;Sam Hollander.
12 Diplo

Close To Me flac

Diplo. 2018. Writer: Ellie Goulding;Savan Kotecha;Peter Svensson;Ilya;Swae Lee;Diplo.
13 The Chainsmokers

Beach House flac

The Chainsmokers. 2018. Writer: Andrew Taggart.
14 Bradley Cooper

Always Remember Us This Way flac

Bradley Cooper. 2018. Writer: Lady Gaga;Dave Cobb.
15 Kelly Clarkson

Never Enough flac

Kelly Clarkson. 2018. Writer: Benj Pasek;Justin Paul.
16 Lady Gaga

I'll Never Love Again flac

Lady Gaga. 2018. Writer: Benjamin Rice;Lady Gaga.
17 Frida Sundemo

Apologize flac

Frida Sundemo. 2018.
18 Little Mix

Woman Like Me flac

Little Mix. 2018. Writer: Nicki Minaj;Steve Mac;Ed Sheeran;Jess Glynne.
19 Imagine Dragons

Machine flac

Imagine Dragons. 2018. Writer: Wayne Sermon;Daniel Platzman;Dan Reynolds;Ben McKee;Alex Da Kid.
20 Billie Eilish

When The Party's Over flac

Billie Eilish. 2018. Writer: Billie Eilish;FINNEAS.

Related questions

Hot questions

Language

Popular Tags