2010-10-25 7 views
12

Je suis en train de compiler les éléments suivants pour le NDK AndroidAndroid C++ NDK

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

extern "C" { 
    JNIEXPORT jstring JNICALL Java_com_knucklegames_helloCpp_testFunction(JNIEnv * env, jobject obj); 
}; 

JNIEXPORT jstring JNICALL Java_com_knucklegames_helloCpp_testFunction(JNIEnv *env, jobject obj) { 
return env->NewStringUTF(env, "Hello from native code!"); 
} 

mais il donne l'erreur suivante

Compile++ thumb: helloCpp <= /cygdrive/c/workspace/helloCpp/jni/main.cpp 
/cygdrive/c/workspace/helloCpp/jni/main.cpp: In function '_jstring* Java_com_knucklegames_hello 
Cpp_testFunction(JNIEnv*, _jobject*)': 
/cygdrive/c/workspace/helloCpp/jni/main.cpp:10: error: no matching function for call to '_JNIEn 
v::NewStringUTF(JNIEnv*&, const char [24])' 
/cygdrive/d/android/android-ndk-r4b/build/platforms/android-8/arch-arm/usr/include/jni.h:839: note: candidates 
are: _jstring* _JNIEnv::NewStringUTF(const char*) 
make: *** [/cygdrive/c/workspace/helloCpp/obj/local/armeabi/objs/helloCpp/main.o] Error 1 

Répondre

19

La fonction NewStringUTF ne prend un argument, un c-string :

env->NewStringUTF("Hello from native code!"); 

Il existe une version C qui va comme ceci:

NewStringUTF(env, "Hello from native code!"); 

Mais vous utilisez évidemment la version C++.

+0

Merci cela a résolu l'erreur, mais il est maintenant la fermeture de force lors de la tentative de chargement de la librairie. Il est dans le dossier lib sous le nom de libhelloCpp.so et static {System.loadLibrary ("helloCpp"); } est le problème – Will03uk

+0

Si vous avez une autre question, vous devriez faire un autre post. Plus de gens sont susceptibles de voir cela de cette façon. –

+0

Assurez-vous que .so est sous libs/armeabi; lorsque l'APK est installé sur l'appareil, vous devriez le voir sous /data/data/com.knucklegames.helloCpp/lib/libhelloCpp.so – I82Much