Hi,
I got a problem here about NDK with Android and C++.
Here's the specification I run :
- Android API 6 (2.1)
- NDK r5 or NDK r7
- cygwin
- Command Line Tool for Android SDK (I'm not using eclipse or anything else)
All I need is just simple :
- Declaring variable in Java (int x, int y).
- Summary those variable in C++ and return it to Java
- Print the value in Android emulator.
Here's my current files :
Android side (myActivity and myView)
package com.game.demo;
import android.app.Activity;
import android.os.Bundle;
public class MyActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
static {
System.loadLibrary("ndk_demo");
}
}
package com.game.demo;
import android.widget.TextView;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
public class MyView extends TextView
{ public int x=10;
public int y=15;
public static Paint m_sp = null;
public MyView(Context context)
{
super(context);
}
protected void onDraw (Canvas canvas)
{
canvas.drawColor(0xFFFDCBFE);
m_sp = new Paint();
try
{
if (m_sp != null)
{
canvas.drawColor(0xFFFDCBFE);
canvas.drawText("HELLO JNI di JAVA View", 100, 100, m_sp);
canvas.drawText("Ini variabel x :"+x, 100, 110, m_sp);
canvas.drawText("Ini variabel y :"+y, 100, 120, m_sp);
}
PrintString();
System.out.println("This line executed after PrintString()");
}
catch(Exception e)
{
System.out.println("..... " + e.getMessage());
}
}
public static native void PrintString();
}
Files in cpp :
#include <stdio.h>
#ifndef ANDROID
#include <windows.h>
#include <tchar.h>
#endif
#ifdef ANDROID
#include <jni.h>
#include <android/log.h>
extern "C" {
JNIEXPORT void JNICALL Java_com_game_demo_MyView_PrintString (JNIEnv *env, jobject obj);
};
#endif
void PrintStringC()
{
#ifdef ANDROID
__android_log_print(ANDROID_LOG_INFO, "PRINT_STRING" ,"");
#endif
printf ("Hello JNI p\n");
}
#ifndef ANDROID
int _tmain(int argc, _TCHAR* argv[])
{
PrintStringC();
getchar();
return 0;
}
#endif
#ifdef ANDROID
void
Java_com_game_demo_MyView_PrintString (JNIEnv *env, jobject obj)
{
PrintString();
}
#endif
\
How could I do that?
Thanks in advance
PS : Consider that I'm really really new at NDK and cygwin things ^^.