在android工程下面jni/hellocpp/main.cpp里面是cocos2d c++的入口。
一路跟踪Cocos2dxActivity.java找到Cocos2dxRenderer.java
public void onSurfaceCreated(final GL10 pGL10,final EGLConfig pEGLConfig) { Cocos2dxRenderer.nativeInit(this.mScreenWidth,this.mScreenHeight); this.mLastTickInNanoSeconds = System.nanoTime(); mNativeInitCompleted = true; }
调用native方法nativeInit创建glView。
其中nativeInit方法在cocos/platform/android/javaactivity-android.cpp可以看到
java层是怎么能够找的到javaactivity-android.cpp这个文件呢?
在obj目录下可以看到javaactivity-android.o文件
但是这个文件在什么时候在哪里编译的呢?
右键工程可以看到会执行目录下的build_native.py
在build_native.py里面可以看到一段函数
def do_build(cocos_root,ndk_root,app_android_root,ndk_build_param,sdk_root,android_platform,build_mode): ndk_path = os.path.join(ndk_root,"ndk-build") num_of_cpu = get_num_of_cpu() if ndk_build_param == None: command = '%s -j%d -C %s NDK_DEBUG=%d' % (ndk_path,num_of_cpu,build_mode=='debug') else: command = '%s -j%d -C %s NDK_DEBUG=%d %s' % (ndk_path,build_mode=='debug',' '.join(str(e) for e in ndk_build_param)) if os.system(command) != 0: raise Exception("Build dynamic library for project [ " + app_android_root + " ] fails!") elif android_platform is not None: sdk_tool_path = os.path.join(sdk_root,"tools/android") cocoslib_path = os.path.join(cocos_root,"cocos/platform/android/java") command = '%s update lib-project -t %s -p %s' % (sdk_tool_path,cocoslib_path) if os.system(command) != 0: raise Exception("update cocos lib-project [ " + cocoslib_path + " ] fails!") command = '%s update project -t %s -p %s -s' % (sdk_tool_path,app_android_root) if os.system(command) != 0: raise Exception("update project [ " + app_android_root + " ] fails!") buildfile_path = os.path.join(app_android_root,"build.xml") command = 'ant clean %s -f %s -Dsdk.dir=%s' % (build_mode,buildfile_path,sdk_root) os.system(command)
打印执行的命令可以看到
xx\android-ndk-r10c\ndk-build -j4 -C xx\Projects\cake\proj.android NDK_DEBUG=0ndk-build 其实就是对GNU Make的封装,它的目的是调用正确的NDK编译脚本,它等价于 make -f $NDK/build/core/build-local.mk [参数]
打开 build-local.mk
可以看到关于jni目录下的application.mk和android.mk信息。
可见这就是编译C++文件的入口。
再看android.mk
其中$(call import-module,.)会引入上面add-path下的外部android.mk文件
可以看到cocos目录下有个android.mk文件
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := cocos2dx_internal_static LOCAL_MODULE_FILENAME := libcocos2dxinternal LOCAL_SRC_FILES := \ cocos2d.cpp \ 2d/CCAction.cpp \ 2d/CCActionCamera.cpp \ 2d/CCActionCatmullRom.cpp \ 2d/CCActionEase.cpp \ 2d/CCActionGrid.cpp \ 2d/CCActionGrid3D.cpp \ 2d/CCActionInstant.cpp \ 2d/CCActionInterval.cpp \ 2d/CCActionManager.cpp \ 2d/CCActionPageTurn3D.cpp \ 2d/CCActionProgressTimer.cpp \ 2d/CCActionTiledGrid.cpp \ 2d/CCActionTween.cpp \ 2d/CCAnimation.cpp \ 2d/CCAnimationCache.cpp \ 2d/CCAtlasNode.cpp \ 2d/CCCamera.cpp \ 2d/CCClippingNode.cpp \ 2d/CCClippingRectangleNode.cpp \ 2d/CCComponent.cpp \ 2d/CCComponentContainer.cpp \ 2d/CCDrawNode.cpp \ 2d/CCDrawingPrimitives.cpp \ 2d/CCFont.cpp \ 2d/CCFontAtlas.cpp \ 2d/CCFontAtlasCache.cpp \ 2d/CCFontCharMap.cpp \ 2d/CCFontFNT.cpp \ 2d/CCFontFreeType.cpp \ 2d/CCGLBufferedNode.cpp \ 2d/CCGrabber.cpp \ 2d/CCGrid.cpp \ 2d/CCLabel.cpp \ 2d/CCLabelAtlas.cpp \ 2d/CCLabelBMFont.cpp \ 2d/CCLabelTTF.cpp \ 2d/CCLabelTextFormatter.cpp \ 2d/CCLayer.cpp \ 2d/CCLight.cpp \ 2d/CCMenu.cpp \ 2d/CCMenuItem.cpp \ 2d/CCMotionStreak.cpp \ 2d/CCNode.cpp \ 2d/CCNodeGrid.cpp \ 2d/CCParallaxNode.cpp \ 2d/CCParticleBatchNode.cpp \ 2d/CCParticleExamples.cpp \ 2d/CCParticleSystem.cpp \ 2d/CCParticleSystemQuad.cpp \ 2d/CCProgressTimer.cpp \ 2d/CCProtectedNode.cpp \ 2d/CCRenderTexture.cpp \ 2d/CCScene.cpp \ 2d/CCSprite.cpp \ 2d/CCSpriteBatchNode.cpp \ 2d/CCSpriteFrame.cpp \ 2d/CCSpriteFrameCache.cpp \ 2d/CCTMXLayer.cpp \ 2d/CCFastTMXLayer.cpp \ 2d/CCTMXObjectGroup.cpp \ 2d/CCTMXTiledMap.cpp \ 2d/CCFastTMXTiledMap.cpp \ 2d/CCTMXXMLParser.cpp \ 2d/CCTextFieldTTF.cpp \ 2d/CCTileMapAtlas.cpp \ 2d/CCTransition.cpp \ 2d/CCTransitionPageTurn.cpp \ 2d/CCTransitionProgress.cpp \ 2d/CCTweenFunction.cpp \ platform/CCGLView.cpp \ platform/CCFileUtils.cpp \ platform/CCSAXParser.cpp \ platform/CCThread.cpp \ platform/CCImage.cpp \ math/CCAffineTransform.cpp \ math/CCGeometry.cpp \ math/CCVertex.cpp \ math/Mat4.cpp \ math/MathUtil.cpp \ math/Quaternion.cpp \ math/TransformUtils.cpp \ math/Vec2.cpp \ math/Vec3.cpp \ math/Vec4.cpp \ base/CCAutoreleasePool.cpp \ base/CCConfiguration.cpp \ base/CCConsole.cpp \ base/CCData.cpp \ base/CCDataVisitor.cpp \ base/CCDirector.cpp \ base/CCEvent.cpp \ base/CCEventAcceleration.cpp \ base/CCEventCustom.cpp \ base/CCEventDispatcher.cpp \ base/CCEventFocus.cpp \ base/CCEventKeyboard.cpp \ base/CCEventController.cpp \ base/CCEventListener.cpp \ base/CCEventListenerController.cpp \ base/CCEventListenerAcceleration.cpp \ base/CCEventListenerCustom.cpp \ base/CCEventListenerFocus.cpp \ base/CCEventListenerKeyboard.cpp \ base/CCEventListenerMouse.cpp \ base/CCEventListenerTouch.cpp \ base/CCEventMouse.cpp \ base/CCEventTouch.cpp \ base/CCIMEDispatcher.cpp \ base/CCNS.cpp \ base/CCProfiling.cpp \ base/ccRandom.cpp \ base/CCRef.cpp \ base/CCScheduler.cpp \ base/CCScriptSupport.cpp \ base/CCTouch.cpp \ base/CCUserDefault.cpp \ base/CCUserDefault-android.cpp \ base/CCValue.cpp \ base/TGAlib.cpp \ base/ZipUtils.cpp \ base/atitc.cpp \ base/base64.cpp \ base/ccCArray.cpp \ base/ccFPSImages.c \ base/ccTypes.cpp \ base/ccUTF8.cpp \ base/ccUtils.cpp \ base/etc1.cpp \ base/pvr.cpp \ base/s3tc.cpp \ base/CCController.cpp \ base/CCController-android.cpp \ base/ObjectFactory.cpp \ renderer/CCBatchCommand.cpp \ renderer/CCCustomCommand.cpp \ renderer/CCGLProgram.cpp \ renderer/CCGLProgramCache.cpp \ renderer/CCGLProgramState.cpp \ renderer/CCGLProgramStateCache.cpp \ renderer/CCGroupCommand.cpp \ renderer/CCQuadCommand.cpp \ renderer/CCMeshCommand.cpp \ renderer/CCRenderCommand.cpp \ renderer/CCRenderer.cpp \ renderer/CCTexture2D.cpp \ renderer/CCTextureAtlas.cpp \ renderer/CCTextureCache.cpp \ renderer/ccGLStateCache.cpp \ renderer/ccShaders.cpp \ renderer/CCVertexIndexBuffer.cpp \ renderer/CCVertexIndexData.cpp \ renderer/CCPrimitive.cpp \ renderer/CCPrimitiveCommand.cpp \ renderer/CCTrianglesCommand.cpp \ deprecated/CCArray.cpp \ deprecated/CCSet.cpp \ deprecated/CCString.cpp \ deprecated/CCDictionary.cpp \ deprecated/CCDeprecated.cpp \ deprecated/CCNotificationCenter.cpp \ physics/CCPhysicsBody.cpp \ physics/CCPhysicsContact.cpp \ physics/CCPhysicsJoint.cpp \ physics/CCPhysicsShape.cpp \ physics/CCPhysicsWorld.cpp \ physics/chipmunk/CCPhysicsBodyInfo_chipmunk.cpp \ physics/chipmunk/CCPhysicsContactInfo_chipmunk.cpp \ physics/chipmunk/CCPhysicsJointInfo_chipmunk.cpp \ physics/chipmunk/CCPhysicsShapeInfo_chipmunk.cpp \ physics/chipmunk/CCPhysicsWorldInfo_chipmunk.cpp \ ../external/ConvertUTF/ConvertUTFWrapper.cpp \ ../external/ConvertUTF/ConvertUTF.c \ ../external/tinyxml2/tinyxml2.cpp \ ../external/unzip/ioapi_mem.cpp \ ../external/unzip/ioapi.cpp \ ../external/unzip/unzip.cpp \ ../external/edtaa3func/edtaa3func.cpp \ ../external/xxhash/xxhash.c LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH) \ $(LOCAL_PATH)/. \ $(LOCAL_PATH)/.. \ $(LOCAL_PATH)/platform \ $(LOCAL_PATH)/base \ $(LOCAL_PATH)/../external \ $(LOCAL_PATH)/../external/tinyxml2 \ $(LOCAL_PATH)/../external/unzip \ $(LOCAL_PATH)/../external/chipmunk/include/chipmunk \ $(LOCAL_PATH)/../external/xxhash \ $(LOCAL_PATH)/../external/nslog LOCAL_C_INCLUDES := $(LOCAL_PATH) \ $(LOCAL_PATH)/. \ $(LOCAL_PATH)/platform \ $(LOCAL_PATH)/../external \ $(LOCAL_PATH)/../external/tinyxml2 \ $(LOCAL_PATH)/../external/unzip \ $(LOCAL_PATH)/../external/chipmunk/include/chipmunk \ $(LOCAL_PATH)/../external/edtaa3func \ $(LOCAL_PATH)/../external/xxhash \ $(LOCAL_PATH)/../external/ConvertUTF \ $(LOCAL_PATH)/../external/nslog LOCAL_EXPORT_LDLIBS := -lGLESv2 \ -llog \ -lz \ -landroid LOCAL_STATIC_LIBRARIES := cocos_freetype2_static LOCAL_STATIC_LIBRARIES += cocos_png_static LOCAL_STATIC_LIBRARIES += cocos_jpeg_static LOCAL_STATIC_LIBRARIES += cocos_tiff_static LOCAL_STATIC_LIBRARIES += cocos_webp_static LOCAL_STATIC_LIBRARIES += cocos_chipmunk_static LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dxandroid_static # define the macro to compile through support/zip_support/ioapi.c LOCAL_CFLAGS := -DUSE_FILE32API LOCAL_CPPFLAGS := -Wno-deprecated-declarations -Wno-extern-c-compat LOCAL_EXPORT_CFLAGS := -DUSE_FILE32API LOCAL_EXPORT_CPPFLAGS := -Wno-deprecated-declarations -Wno-extern-c-compat ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) LOCAL_ARM_NEON := true endif include $(BUILD_STATIC_LIBRARY) #============================================================== include $(CLEAR_VARS) LOCAL_MODULE := cocos2dx_static LOCAL_MODULE_FILENAME := libcocos2d LOCAL_STATIC_LIBRARIES := cocostudio_static LOCAL_STATIC_LIBRARIES += cocosbuilder_static LOCAL_STATIC_LIBRARIES += cocos3d_static LOCAL_STATIC_LIBRARIES += spine_static LOCAL_STATIC_LIBRARIES += cocos_network_static LOCAL_STATIC_LIBRARIES += audioengine_static include $(BUILD_STATIC_LIBRARY) #============================================================== $(call import-module,freetype2/prebuilt/android) $(call import-module,platform/android) $(call import-module,png/prebuilt/android) $(call import-module,jpeg/prebuilt/android) $(call import-module,tiff/prebuilt/android) $(call import-module,webp/prebuilt/android) $(call import-module,chipmunk/prebuilt/android) $(call import-module,3d) $(call import-module,audio/android) $(call import-module,editor-support/cocosbuilder) $(call import-module,editor-support/cocostudio) $(call import-module,editor-support/spine) $(call import-module,network) $(call import-module,ui) $(call import-module,extensions) $(call import-module,Box2D) $(call import-module,curl/prebuilt/android) $(call import-module,websockets/prebuilt/android) $(call import-module,protobuf-lite)
$(call import-module,audio/android)这个下面也有android.mk文件javaactivity-android.o就是在这里编译的。
到这里一切就豁然开朗了。
参考链接: http://blog.sina.com.cn/s/blog_4057ab62010197z8.html http://blog.csdn.net/smfwuxiao/article/details/8523087