本文主要是记录如何将
opengl es编程指南
中的示例代码搭建在Android Studio
中。
主要过程包括以下几点:
- 在
Android Studio
创建一个新的支持NDK项目 - 修改
AndroidManifest.xml
,注册NativeActivity
- 拷贝
opengl es编程指南
的示例代码到cpp
目录下 - 修改
CMakeLists.txt
,添加opengl es
的依赖, 以及NativeActivity
在NDK
中的依赖
下面依次介绍各个流程,在Andrid Studio
中创建一个新项目就不用多说了,着很简单
注册NativeActivity
打开AndroidManifest.xml
,注释掉创建项目自动生成的MainActivity
,然后添加一下代码
<activity android:name="android.app.NativeActivity"
android:label="HelloTriangle"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:launchMode="singleTask"
android:configChanges="orientation|keyboardHidden">
<!--这里比较重要,value指的是ndk生成的库-->
<meta-data android:name="android.app.lib_name"
android:value="Hello_Triangle" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
拷贝示例代码到cpp下
下面是书籍代码示例目录
拷贝common
和Chapter_2
目录下的c
文件和头文件到cpp目录下,主要包括以下文件:
可能在这几个拷贝的文件中有些头文件没有加, 按照提示自己添加
添加库的依赖
在Android Studio
中,NDK的编译和eclipse不太一样,在eclipse中是依靠Android.mk和Application.mk编译, 而Android Studio中是用CMake去编译
下面是几个需要添加的依赖:
- NativeActivity所需要的依赖,也就是静态库android_native_app_glue
-
opengl es
所需的几个库 -
NativeWindow
所需的android.so
下面是修改后的CMakeLists.txt
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
include_directories(
${ANDROID_NDK}/sources/android/native_app_glue)
add_library( # Sets the name of the library.
Hello_Triangle
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/esUtil_Android.c
src/main/cpp/esShader.c
src/main/cpp/esTransform.c
src/main/cpp/esUtil.c
src/main/cpp/Hello_Triangle.c)
add_library(
app-glue
STATIC
${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c )
find_library( OPENGLES3_LIBRARY GLESv3 "OpenGL ES v3.0 library")
find_library( EGL_LIBRARY EGL "EGL 1.4 library" )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
Hello_Triangle
# Links the target library to the log library
# included in the NDK.
${log-lib}
app-glue
${OPENGLES3_LIBRARY}
${EGL_LIBRARY}
android.so
)
下面就是运行之后的结果,只要NDK
和SDK
版本没有问题,基本上就没问题了