使用C ++的NDK JNI Android [英] NDK JNI Android with C++

查看:112
本文介绍了使用C ++的NDK JNI Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在这里遇到有关Android和C ++的NDK的问题.
这是我运行的规范:
-Android API 6(2.1)
-NDK r5或NDK r7
-cygwin
-适用于Android SDK的命令行工具(我不使用Eclipse或其他工具)

我只需要简单:
-在Java中声明变量(int x,int y).
-在C ++中汇总这些变量,然后将其返回给Java
-在Android模拟器中打印值.

这是我当前的文件:
Android端(myActivity和myView)

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)

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


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



cpp中的文件:



Files in cpp :

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

\

我该怎么办?

在此先感谢


PS:考虑一下我真的是NDK和cygwin的新手,^^.

\

How could I do that?

Thanks in advance


PS : Consider that I''m really really new at NDK and cygwin things ^^.

推荐答案

我已经在几分钟前解决了它,并且可以正常工作不知何故:

1.创建Android项目
2.创建包含该方法的java类以添加"两个变量.
3.使用编译器将其编译为.class格式(例如eclipse或命令行,我使用命令行,因此此命令应类似于 javah -d jni< class_name> ). /> 4.从我们创建的类中,创建头文件".h".我使用cygwin和NDK r5.
5.在根项目上创建一个名为"jni"的文件夹.复制我们生成的".h"文件.
6.在同一文件夹中,创建Android.mk和".c"文件以汇总可变参数.
7.在根项目上,创建Application.mk
8.维护UI(如果使用).
9.在android项目的主要活动类上,执行一些编码以在模拟器上显示结果.
10.构建并运行它.

享受NDK !.

注意:在第3步: javah -d jni -d jni 表示我想将结果放置在我之前创建的文件夹jni中.
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.


这篇关于使用C ++的NDK JNI Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆