I decided to write a very easy step-for-step tutorial on how to build OSG for Android. I'm going to share it here. Corrections are of course welcome.

This Tutorial is to show how to compile OpenSceneGraph for Android and how to build the Example Application osgViewer on Ubuntu 11.10. It is meant as additional help and does not exempt the developer from reading
http://www.openscenegraph.org/projects/osg/wiki/Support/PlatformSpecifics/Android <这个比较重要,建议看看>
and
http://developer.android.com/sdk/ndk/index.html
Screenshots are available here: http://imgur.com/a/VEyrg
Initial requirements are an already working Android Development Environment. I am using Eclipse Indigo, Android SDK r16, Android NDK r7b. Further required are cmake and g++.
In this tutorial $HOME always stands for /home/USER where USER denotes the username.

Install Synaptic using the Software Center (SCREENSHOT1).

Install the package openjdk-6-jdk using Synaptic (SCREENSHOT2).
Install the following packages using Synaptic or apt-get.
cmake ( sudo apt-get install cmake )
g++


Download Eclipse and extract it. I'm using
$HOME/Android/eclipse/
My version is Eclipse Indigo.

Download the Android SDK (my version: r16) and extract it into
$HOME/Android/android-sdk-linux/

Install the ADT-Plugin into eclipse. This works like this: start eclipse and on the top panel go to Help → Install New Software . In the Field labeled „Work with:“ fill in:
https://dl-ssl.google.com/android/eclipse/
and press Enter (SCREENSHOT3). After it has loaded the sources („Developer Tools“ is visible) (screenshot), check the „Developer Tools“ and click Next until everything is installed.

After restarting eclipse go to Window → Android SDK Manager . Install AT LEAST Android 2.2 (API 8). This is the minimum required version for OSG on Android. (SCREENSHOT4). I installed it to
$HOME/Android/android-sdks/
to have it all in one place.

Extract the Android NDK to
$HOME/Android/android-ndk-r7b/


Developers with an already working environment should be able to start here.
Installing OpenSceneGraph 3.0.1 for Android with OpenGL ES 1.x

Download OpenSceneGraph-3.0.1 and extract it to
$HOME/Android/OpenSceneGraph-3.0.1/

Download the 3rd Party Dependencies and extract it to
$HOME/Android/OpenSceneGraph-3.0.1/3rdparty/

Set the environment variable ANDROID_NDK so that the cmake building script can find it. Go to
/home/USER/
and open the file .bashrc . You might need to make it visible. Do this with CTRL+H.
Edit the file (add the following lines to the end):
export ANDROID_NDK=$HOME/Android/android-ndk-r7b
export ANDROID_SDK=$HOME/Android/android-sdks

(alternatively you can use /home/USER/ instead of $HOME/ ) (SCREENSHOT5). I added a variable for the SDK too, so that we don't always have to type the full path when accessing the SDK folder from the command line. Save it.

Open a Terminal. Type the following commands (replace USER with your username):
mkdir $HOME/Android/OpenSceneGraph-3.0.1/build
mkdir $HOME/Android/OpenSceneGraph-3.0.1/osginstall
cd $HOME/Android/OpenSceneGraph-3.0.1/build


This created a directory build in the OpenSceneGraph-directory. This is where we will place our build in. The osginstall directory is where the make install command will later install our libraries and header files in. The next command will create the necessary makefiles. Change the last parameter according to the number of cpu-cores you have. I set it to 4, because I have a quadcore.
cmake .. -DOSG_BUILD_PLATFORM_ANDROID=ON -DDYNAMIC_OPENTHREADS=OFF -DDYNAMIC_OPENSCENEGRAPH=OFF -DOSG_GL_DISPLAYLISTS_AVAILABLE=OFF -DOSG_GL_MATRICES_AVAILABLE=ON -DOSG_GL_VERTEX_FUNCS_AVAILABLE=ON -DOSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE=ON -DOSG_GL_FIXED_FUNCTION_AVAILABLE=ON -DOSG_CPP_EXCEPTIONS_AVAILABLE=OFF -DOSG_GL1_AVAILABLE=OFF -DOSG_GL2_AVAILABLE=OFF -DOSG_GL3_AVAILABLE=OFF -DOSG_GLES1_AVAILABLE=ON -DOSG_GLES2_AVAILABLE=OFF -DJ=4 -DCMAKE_INSTALL_PREFIX=$HOME/Android/OpenSceneGraph-3.0.1/osginstall

After that we can build it:
make
...This will take a while, so go and grab a snack.

The following command will then install the libraries and headers into $HOME/Android/OpenSceneGraph-3.0.1/osginstall into the folders obj , lib , and include :
make install

After this is finished, we are ready to start the eclipse project. Before starting eclipse, copy the folder osgAndroidExampleGLES1 from
$HOME/Android/OpenSceneGraph-3.0.1/examples
to
$HOME/workspace/osgAndroidExampleGLES1
(or wherever else your eclipse workspace is).

Copy the three folders obj , lib , and include from inside the $HOME/Android/OpenSceneGraph-3.0.1/osginstall folder into $HOME/workspace/osgAndroidExampleGLES1 (replace if necessary).

Start eclipse and go to New → Project and choose “Android Project“. Next, then choose „Create project from existing source“ and set the location to
/home/USER/workspace/osgAndroidExampleGLES1
The Project Name should be osgAndroidExampleGLES1. (SCREENSHOT6)
Next, the build target should be Android 2.2 or higher.

Expand the project and open the folder jni . Open the file Android.mk
Change line 7 to:
OSG_ANDROID_DIR := /home/USER/workspace/osgAndroidExampleGLES1
Change line 21 to:
LOCAL_DLIBS := -llog -lGLESv1_CM -ldl -lz -lgnustl_static
and save it. (SCREENSHOT7)

In a Terminal window, go to the project directory
cd $HOME/workspace/osgAndroidExampleGLES1
We need to update the build.xml. Type
$ANDROID_SDK/tools/android list targets
and look for „android-8“ (Name: Android 2.2) in the output. Remember the id. My id is 1. The next command to execute in the terminal is:
$ANDROID_SDK/tools/android update project -t 1 -p . -s
where you have to exchange the „1“ with your id number. After that, build the project by executing
$ANDROID_NDK/ndk-build

Now that we built the native part of the Android OSG Viewer, go back to eclipse and run it. That's it.


Using OpenGLES2
When compiling for OpenGLES2, the only difference is the „cmake“ command (and of course the osgAndroidExampleGLES2 example).
The corresponding cmake command for OpenGLES2 is as follows:
cmake .. -DOSG_BUILD_PLATFORM_ANDROID=ON -DDYNAMIC_OPENTHREADS=OFF -DDYNAMIC_OPENSCENEGRAPH=OFF -DOSG_GL_DISPLAYLISTS_AVAILABLE=OFF -DOSG_GL_MATRICES_AVAILABLE=OFF -DOSG_GL_VERTEX_FUNCS_AVAILABLE=OFF -DOSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE=OFF -DOSG_GL_FIXED_FUNCTION_AVAILABLE=OFF -DOSG_CPP_EXCEPTIONS_AVAILABLE=OFF -DOSG_GL1_AVAILABLE=OFF -DOSG_GL2_AVAILABLE=OFF -DOSG_GL3_AVAILABLE=OFF -DOSG_GLES1_AVAILABLE=OFF -DOSG_GLES2_AVAILABLE=ON -DJ=4 -DCMAKE_INSTALL_PREFIX=$HOME/Android/OpenSceneGraph-3.0.1/osginstall



As additional information to get the code completion to work for the c++ part of the project i followed this tutorial: http://permadi.com/blog/2011/09/creating-your-first-android-jnindk-project-in-eclipse-with-sequoyah/

Maybe if I have time I will add on to this tutorial how to debug it.

更多相关文章

  1. Android(安卓)一起来看看面试必问的消息机制
  2. Android代码内存优化建议-Android资源篇
  3. Android:Fragment+ViewPager+FragmentPagerAdapter
  4. android 建议在onPause和onStop处理的事情
  5. 给学android的同学们的一些建议【III】
  6. Android最佳实践之Notification、下拉刷新、内存及性能建议等
  7. android 操作sqllite 数据库
  8. 如何成为一个更好的Android开发者:30+个专业建议
  9. 关于自学小编提出的几点建议

随机推荐

  1. Android的ADB工具使用
  2. 认识Android中的MVP设计模式
  3. AIDL --- Android中的远程接口
  4. android四层框架
  5. Android(安卓)显示意图和隐式意图的区别
  6. android AsyncTask介绍
  7. Android(安卓)样式系统 | 主题背景和样式
  8. Android自动打包、签名、优化、上传ANT脚
  9. 微信在Github开源了Hardcoder,对Android开
  10. android 内存优化