最新首发Eclipse+CDT+android-ndk写纯c++安卓应用(附openGL Es)

首先下载eclipse和cdt,我的版本号依次是:Version: Indigo Service Release 2和Version: 1.0.0.201202111925,再下载windows的ndk,我使用的是android-ndk-r9d

什么cygwin这等东西,太恶心了,下载慢,大的要命!复杂,今天给一个最爽的编译教程。

前面的cdt插件怎么这里pass,网上教程很多的。直接配置。。。

启动eclipse,然后点Windows-Prefrences-C/C++-Build-Envionment,添加以下路径

然后创建一个android工程,把代码全部删除,资源全部删除,AndroidManifest.xml内容如下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.native_activity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        tools:ignore="UsesMinSdkAttributes" />

    <application
        android:hasCode="false"
        android:label="纯CPP应用"
        tools:ignore="AllowBackup,MissingApplicationIcon" >
        <activity
            android:name="android.app.NativeActivity"
            android:configChanges="orientation|keyboardHidden" >

            <!-- Tell NativeActivity the name of or .so -->
            <meta-data
                android:name="android.app.lib_name"
                android:value="native-activity" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

然后创建jni目录,里面放三个文件,依次是Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := native-activity
LOCAL_SRC_FILES := main.cpp
LOCAL_LDLIBS    := -llog -landroid -lEGL -lGLESv1_CM
LOCAL_STATIC_LIBRARIES := android_native_app_glue

include $(BUILD_SHARED_LIBRARY)

$(call import-module,android/native_app_glue)

Application.mk

APP_PLATFORM := android-14

main.cpp

#include <jni.h>
#include <errno.h>
#include <EGL/egl.h>
#include <GLES/gl.h>
#include <string.h>
#include <android/sensor.h>
#include <android/log.h>
#include <android_native_app_glue.h>

#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__))
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native-activity", __VA_ARGS__))

/**
 * Our saved state data.
 */
struct saved_state {
	float angle;
	int32_t x;
	int32_t y;
};

/**
 * Shared state for our app.
 */
struct engine {
	struct android_app* app;

	ASensorManager* sensorManager;
	const ASensor* accelerometerSensor;
	ASensorEventQueue* sensorEventQueue;

	int animating;
	EGLDisplay display;
	EGLSurface surface;
	EGLContext context;
	int32_t width;
	int32_t height;
	struct saved_state state;
};

/**
 * Initialize an EGL context for the current display.
 */
static int engine_init_display(struct engine* engine) {
	// initialize OpenGL ES and EGL

	/*
	 * Here specify the attributes of the desired configuration.
	 * Below, we select an EGLConfig with at least 8 bits per color
	 * component compatible with on-screen windows
	 */
	const EGLint attribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BLUE_SIZE,
			8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_NONE };
	EGLint w, h, dummy, format;
	EGLint numConfigs;
	EGLConfig config;
	EGLSurface surface;
	EGLContext context;

	EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);

	eglInitialize(display, 0, 0);

	/* Here, the application chooses the configuration it desires. In this
	 * sample, we have a very simplified selection process, where we pick
	 * the first EGLConfig that matches our criteria */
	eglChooseConfig(display, attribs, &config, 1, &numConfigs);

	/* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
	 * guaranteed to be accepted by ANativeWindow_setBuffersGeometry().
	 * As soon as we picked a EGLConfig, we can safely reconfigure the
	 * ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */
	eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);

	ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format);

	surface = eglCreateWindowSurface(display, config, engine->app->window,
			NULL);
	context = eglCreateContext(display, config, NULL, NULL);

	if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
		LOGW("Unable to eglMakeCurrent");
		return -1;
	}

	eglQuerySurface(display, surface, EGL_WIDTH, &w);
	eglQuerySurface(display, surface, EGL_HEIGHT, &h);

	engine->display = display;
	engine->context = context;
	engine->surface = surface;
	engine->width = w;
	engine->height = h;
	engine->state.angle = 0;

	// Initialize GL state.
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
	glEnable(GL_CULL_FACE);
	glShadeModel(GL_SMOOTH);
	glDisable(GL_DEPTH_TEST);

	return 0;
}

/**
 * Just the current frame in the display.
 */
static void engine_draw_frame(struct engine* engine) {
	if (engine->display == NULL) {
		// No display.
		return;
	}

	// Just fill the screen with a color.
	glClearColor(((float) engine->state.x) / engine->width, engine->state.angle,
			((float) engine->state.y) / engine->height, 1);
	glClear(GL_COLOR_BUFFER_BIT);

	eglSwapBuffers(engine->display, engine->surface);
}

/**
 * Tear down the EGL context currently associated with the display.
 */
static void engine_term_display(struct engine* engine) {
	if (engine->display != EGL_NO_DISPLAY) {
		eglMakeCurrent(engine->display, EGL_NO_SURFACE, EGL_NO_SURFACE,
				EGL_NO_CONTEXT);
		if (engine->context != EGL_NO_CONTEXT) {
			eglDestroyContext(engine->display, engine->context);
		}
		if (engine->surface != EGL_NO_SURFACE) {
			eglDestroySurface(engine->display, engine->surface);
		}
		eglTerminate(engine->display);
	}
	engine->animating = 0;
	engine->display = EGL_NO_DISPLAY;
	engine->context = EGL_NO_CONTEXT;
	engine->surface = EGL_NO_SURFACE;
}

/**
 * Process the next input event.
 */
static int32_t engine_handle_input(struct android_app* app,
		AInputEvent* event) {
	struct engine* engine = (struct engine*) app->userData;
	if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
		engine->animating = 1;
		engine->state.x = AMotionEvent_getX(event, 0);
		engine->state.y = AMotionEvent_getY(event, 0);
		return 1;
	}
	return 0;
}

/**
 * Process the next main command.
 */
static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
	struct engine* engine = (struct engine*) app->userData;
	switch (cmd) {
	case APP_CMD_SAVE_STATE:
		// The system has asked us to save our current state.  Do so.
		engine->app->savedState = malloc((size_t)sizeof(struct saved_state));
		*((struct saved_state*) engine->app->savedState) = engine->state;
		engine->app->savedStateSize = sizeof(struct saved_state);
		break;
	case APP_CMD_INIT_WINDOW:
		// The window is being shown, get it ready.
		if (engine->app->window != NULL) {
			engine_init_display(engine);
			engine_draw_frame(engine);
		}
		break;
	case APP_CMD_TERM_WINDOW:
		// The window is being hidden or closed, clean it up.
		engine_term_display(engine);
		break;
	case APP_CMD_GAINED_FOCUS:
		// When our app gains focus, we start monitoring the accelerometer.
		if (engine->accelerometerSensor != NULL) {
			ASensorEventQueue_enableSensor(engine->sensorEventQueue,
					engine->accelerometerSensor);
			// We'd like to get 60 events per second (in us).
			ASensorEventQueue_setEventRate(engine->sensorEventQueue,
					engine->accelerometerSensor, (1000L / 60) * 1000);
		}
		break;
	case APP_CMD_LOST_FOCUS:
		// When our app loses focus, we stop monitoring the accelerometer.
		// This is to avoid consuming battery while not being used.
		if (engine->accelerometerSensor != NULL) {
			ASensorEventQueue_disableSensor(engine->sensorEventQueue,
					engine->accelerometerSensor);
		}
		// Also stop animating.
		engine->animating = 0;
		engine_draw_frame(engine);
		break;
	}
}

/**
 * This is the main entry point of a native application that is using
 * android_native_app_glue.  It runs in its own thread, with its own
 * event loop for receiving input events and doing other things.
 */
void android_main(struct android_app* state) {
	struct engine engine = {0};
	// Make sure glue isn't stripped.
	app_dummy();
	state->userData = &engine;
	state->onAppCmd = engine_handle_cmd;
	state->onInputEvent = engine_handle_input;
	engine.app = state;

	// Prepare to monitor accelerometer
	engine.sensorManager = ASensorManager_getInstance();
	engine.accelerometerSensor = ASensorManager_getDefaultSensor(
			engine.sensorManager, ASENSOR_TYPE_ACCELEROMETER);
	engine.sensorEventQueue = ASensorManager_createEventQueue(
			engine.sensorManager, state->looper, LOOPER_ID_USER, NULL, NULL);

	if (state->savedState != NULL) {
		// We are starting with a previous saved state; restore from it.
		engine.state = *(struct saved_state*) state->savedState;
	}

	// loop waiting for stuff to do.

	while (true) {
		// Read all pending events.
		int ident;
		int events;
		struct android_poll_source* source;

		// If not animating, we will block forever waiting for events.
		// If animating, we loop until all events are read, then continue
		// to draw the next frame of animation.
		while ((ident = ALooper_pollAll(engine.animating ? 0 : -1, NULL,
				&events, (void**) &source)) >= 0) {

			// Process this event.
			if (source != NULL) {
				source->process(state, source);
			}

			// If a sensor has data, process it now.
			if (ident == LOOPER_ID_USER) {
				if (engine.accelerometerSensor != NULL) {
					ASensorEvent event;
					while (ASensorEventQueue_getEvents(engine.sensorEventQueue,
							&event, 1) > 0) {
						LOGI("accelerometer: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z);
					}
				}
			}

			// Check if we are exiting.
			if (state->destroyRequested != 0) {
				engine_term_display(&engine);
				return;
			}
		}

		if (engine.animating) {
			// Done with events; draw next animation frame.
			engine.state.angle += .01f;
			if (engine.state.angle > 1) {
				engine.state.angle = 0;
			}
			engine_draw_frame(&engine);
		}
	}
}

创建完成收工,然后创建另外一个工程。路径必须是刚才创建工程的jni目录,名字随便,重点看图

好了点完成,然后打开main.cpp发现N多错误,直接下设置一下环境变量,右键工程,属性(是刚创建的C++工程)

接下来看图,把所有的库加进去。

最后加一个Symbol,其实就是定义一个宏,告诉编译器我现在的平台是Android,add

最后点OK,所有的函数都能正常识别,提示功能也可以用了。开发效率高多了。编译直接点锤子就行了。

然后在原来的工程运行安装就行了!

最新首发Eclipse+CDT+android-ndk写纯c++安卓应用(附openGL Es)

时间: 2025-01-03 21:56:58

最新首发Eclipse+CDT+android-ndk写纯c++安卓应用(附openGL Es)的相关文章

使用eclipse 进行Android ndk开发(javah进行头文件生成)

android 程序基于java写之后,正式发布之后很容易被反编译,因此需要一种方式去对代码或者敏感数据进行保护(例如通讯密钥等),ndk是一种很好的解决方案. ndk可以生成较难进行反编译的二进制库(.so)文件. 首先需要进行ndk的环境搭建,eclipse的搭建很简单,首先eclipse中增加android的插件,其需要下载一个ndk的包,然后在eclipse的设置中,Android项中的ndk的路劲进行设置: 如图: 随后就可以开始进行NDK的开发了,随后通过一个例子来进行ndk的开发.

安装eclipse或者Android STDIO,运行一个安卓程序,并截图。发博客插入图片。

安装Android STDI 运行一个安卓程序,并截图 原文地址:https://www.cnblogs.com/zz2333/p/11448910.html

eclipse android ndk 提示Type &#39;JNIEnv&#39; could not be resolved 等信息解决办法

新配置完eclipse c++ android ndk 环境后,导入项目提示以下信息 是由于没有将jni.h导入的缘故,而这个文件在ndk的目录下面.所以,参照以下步骤:Project Properties -> C/C++ General -> Path and Symbols选择include标签,Add -> $Android_NDK_HOME/platforms/android-14/arch-arm/usr/include且选中All languages.最后Apply -&g

使用eclipse调试android的framework源码

http://www.eoeandroid.com/forum.php?mod=viewthread&tid=325492 这篇文章中的第3和第4章分别介绍了java和c/cpp代码的调试,调试可以大大减少log查看花费的工作量. (1)调试java代码已经验证过了,主要是在eclipse中建立一个相同package名的安卓应用工程, 工程目录中导入源码,并在java窗口打开:在DDMS中去指定要调试的thread,在Debug窗口打开源码设置断点: 如果目录结构能同包名,则可以直接调试 如果目

基于 Android NDK 的学习之旅-----序言

http://www.cnblogs.com/luxiaofeng54/tag/android/default.html?page=1 基于 Android NDK 的学习之旅-----目录 duicky 2011-09-15 16:34 阅读:3035 评论:2 基于 Android NDK 的学习之旅-----资源释放 duicky 2011-08-21 14:29 阅读:6173 评论:0 基于 Android NDK 的学习之旅-----数据传输二(引用数据类型)(附源码) duicky

Android OpenGL ES(三)----编程框架

首先当然是创建Android项目,你可以选择最新的Android Studio也可以选择eclipse都是一样的.我们重点讲解开发OpenGL ES的流程 1.定义顶点着色器和片段着色器 第一节我们讲解的已经很细致了,为了便于理解在这里在详细的说明一下.并且换一种方式定义着色器. 我们知道第一篇定义的顶点的坐标和颜色是分开的,这样可以但如果把它们放在一起会方便许多. 假设我们要绘制一个长方形和两条直线,二个定点,我们就需要这样来定义这个数组. float[] tableVerticesWithT

Android OpenGL ES向导学习笔记(扫盲专用)

Android 目前支持下面几个版本的OpenGL ES API : OpenGL ES 1.0 和 1.1 :Android 1.0和更高的版本支持这个API规范. OpenGL ES 2.0 : Android 2.2(API 8)和更高的版本支持这个API规范. OpenGL ES 3.0 : Android 4.3(API 18)和更高的版本支持这个API规范. OpenGL ES 3.1 :  Android 5.0(API 21)和更高的版本支持这个API规范. 支持OpenGL E

OPENGL es版本和OGL 及android关系

OpenGL ES 1.0 and 1.1 - This API specification is supported by Android 1.0 and higher. 固定管线. OpenGL ES 2.0 - This API specification is supported by Android 2.2 (API level 8) and higher. 2.0 1.10 对应GLSL 1.10全面支持Shader编程. OpenGL 2.0 (2004) Addition Pro

Android开发搭环境步骤, ADT, JDK, SDK, NDK, Eclipse, CDT

以下信息,只为遗忘做准备. 下面是我在配置安装Android的环境的过程记录. 1. 首先需要安装JDK, 里面有java运行时环境 2. 然后安装Eclipse, 这是用来编写代码的工具,类似于VS, 或者XCode, 3. 安装完Eclipse后, 在网上去下载ADT, 这是为了方便Eclipse编写android程序所准备的一个控件, 下载下来后,解包,然后放置在Eclipse的安装生成的文件夹下. (一般会提示是否替换文件, 选是即可).还有一种方式也可以配置ADT,在Eclipse--