Click here to Skip to main content
15,920,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)

Java
//------------THIS IS MYACTIVITY------------------

package com.game.demo;

import android.app.Activity;
import android.os.Bundle;

public class MyActivity extends Activity
{
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
		setContentView(new MyView(this));
		
    }
	
	static {
        System.loadLibrary("ndk_demo");
    }	
}

Java
//-------------- THIS IS MYVIEW ------------------------
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 :
C++
//---------- THIS IS JNIDemo.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 ^^.
Posted

1 solution

I've solved it hust a minutes ago, and it works somehow :

1. Create Android project
2. Create java class contain teh method to "add" two variable.
3. Compile it to .class forms using your compiler (such as eclipse or command line, i use command line so this the command should be like this javah -d jni <class_name>).
4. From the class that we create, make a header file ".h". I use cygwin and NDK r5.
5. Make a folder called "jni" on root project. Copy the ".h" file we generate.
6. In the same folder, create Android.mk and ".c" file to summary the variabel.
7. On the root project, create Application.mk
8. Maintain the UI (if you use it).
9. On your main activity class of android project, do some coding to show teh result on emulator.
10.Build and run it.

Enjoy the NDK!.

Note : at 3rd step : javah -d jni ,-d jni means that I wanted to place the result on the folder jni that I've created before.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900