因为业务需要,公司原来使用eclipes作为Android工程输出的项目,需要转到Andiro Studio(后面简称AS)环境下。在折腾了2周失败后,在一个之前从没有用过AS的大神帮助下,帮我在AS下成功转移了环境,并出了可以测试的包……
先说一下项目环境:应该是Cocos2dx 3.4以后的版本,大神说我们的项目当时修改了Cosos的引擎,具体是哪个版本他记不清了……我拿到的工程目录大致是这样的。
只有eclipes用的proj.android目录和Viso Studio编辑的Win32工程目录,外加游戏项目的主要代码:Classes目录和资源Resources目录。
开干!
首先用3.13的cocos2dx新建一个Cocos2D工程,比如在D:work2目录下,按住Shift键,点击鼠标右键,选择在此处打开Powershe窗口(如果没有此选项的,可以选择:在此处打开命令窗口)
输入新建工程的命令,如果不能执行,请自行寻找如何配置Cocos环境的教程
D:\work2> cocos new mygame -p com.funland.magame -l cpp
新建的Cocos工程目录结构如下:
把原来工程的5个目录:
\Classes
\cocos2d
\proj.android
\proj.win32
\Resources
拷贝并覆盖到新建工程里。
接下来要检查和修改两个重要的配置文件Android.mk,这里提供了2个mk文件的下载。注意,从下面复制代码进入mk文件,有可能会导致编译失败!
先看D:\work2\mygame\proj.android\jni\Android.mk的内容如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/external)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/cocos)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/cocos/audio/include)
LOCAL_MODULE := MyGame_shared
LOCAL_MODULE_FILENAME := libMyGame
#traverse all the directory and subdirectory
define walk
$(wildcard $(1)) $(foreach e, $(wildcard $(1)/*), $(call walk, $(e)))
endef
#traverse Classes Directory
ALLFILES = $(call walk, $(LOCAL_PATH)/../../Classes)
FILE_LIST := hellocpp/main.cpp
FILE_LIST += $(filter %.cpp, $(ALLFILES))
FILE_LIST += $(filter %.c, $(ALLFILES))
FILE_INCLUDES := $(shell find $(LOCAL_PATH)/../../Classes -type d)
#source file will be compiled
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%) \
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes \
$(LOCAL_PATH)/../../cocos2d/cocos/storage/local-storage
# _COCOS_HEADER_ANDROID_BEGIN
# _COCOS_HEADER_ANDROID_END
LOCAL_STATIC_LIBRARIES := cocos2dx_static
LOCAL_STATIC_LIBRARIES += cocos_localstorage_static
LOCAL_STATIC_LIBRARIES += cocos2d_lua_static
# _COCOS_LIB_ANDROID_BEGIN
# _COCOS_LIB_ANDROID_END
include $(BUILD_SHARED_LIBRARY)
$(call import-module,.)
$(call import-module,storage/local-storage)
$(call import-module,scripting/lua-bindings/proj.android)
# _COCOS_LIB_IMPORT_ANDROID_BEGIN
# _COCOS_LIB_IMPORT_ANDROID_END
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/external)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/cocos)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/cocos/audio/include)
LOCAL_MODULE := MyGame_shared
LOCAL_MODULE_FILENAME := libMyGame
LOCAL_SRC_FILES := hellocpp/main.cpp \
../../../Classes/AppDelegate.cpp \
../../../Classes/HelloWorldScene.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../Classes
# _COCOS_HEADER_ANDROID_BEGIN
# _COCOS_HEADER_ANDROID_END
LOCAL_STATIC_LIBRARIES := cocos2dx_static
# _COCOS_LIB_ANDROID_BEGIN
# _COCOS_LIB_ANDROID_END
include $(BUILD_SHARED_LIBRARY)
$(call import-module,.)
# _COCOS_LIB_IMPORT_ANDROID_BEGIN
# _COCOS_LIB_IMPORT_ANDROID_END
修改后的D:\work2\mygame\proj.android-studio\app\jni\Android.mk的内容如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/external)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/cocos)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/cocos/audio/include)
LOCAL_MODULE := MyGame_shared
LOCAL_MODULE_FILENAME := libMyGame
#traverse all the directory and subdirectory
define walk
$(wildcard $(1)) $(foreach e, $(wildcard $(1)/*), $(call walk, $(e)))
endef
#traverse Classes Directory
ALLFILES = $(call walk, $(LOCAL_PATH)/../../../Classes)
FILE_LIST := hellocpp/main.cpp
FILE_LIST += $(filter %.cpp, $(ALLFILES))
FILE_LIST += $(filter %.c, $(ALLFILES))
FILE_INCLUDES := $(shell find $(LOCAL_PATH)/../../../Classes -type d)
#source file will be compiled
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%) \
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../Classes \
$(LOCAL_PATH)/../../../cocos2d/cocos/storage/local-storage
# _COCOS_HEADER_ANDROID_BEGIN
# _COCOS_HEADER_ANDROID_END
LOCAL_STATIC_LIBRARIES := cocos2dx_static
LOCAL_STATIC_LIBRARIES += cocos_localstorage_static
LOCAL_STATIC_LIBRARIES += cocos2d_lua_static
# _COCOS_LIB_ANDROID_BEGIN
# _COCOS_LIB_ANDROID_END
include $(BUILD_SHARED_LIBRARY)
$(call import-module,.)
$(call import-module,storage/local-storage)
$(call import-module,scripting/lua-bindings/proj.android)
# _COCOS_LIB_IMPORT_ANDROID_BEGIN
# _COCOS_LIB_IMPORT_ANDROID_END
PS D:\work2\mygame\proj.android> cocos compile -p android --android-studio --ap android-22
这里可能会有2个坑,大家需要注意一下。
C:\Users\work1\AppData\Local\Android\Sdk\platforms
另外一个坑就是你安装AS并升级SDK到了26或以上的版本,会在执行导出工程的命令时,出现下面的状况。
PS D:\work2\mygame> cocos compile -p android --android-studio --ap android-22
编译模式:debug
使用 Android Studio 工程:D:\work2\mygame\proj.android-studio
正在执行:'"C:\Users\work1\AppData\Local\Android\Sdk\tools\android" update project -t android-22 -p D:\work2\mygame\proj.android-studio\app'
**************************************************************************
The "android" command is deprecated.
For manual SDK, AVD, and project management, please use Android Studio.
For command-line tools, use tools\bin\sdkmanager.bat
and tools\bin\avdmanager.bat
**************************************************************************
Invalid or unsupported command "update project -t android-22 -p D:\work2\mygame\proj.android-studio\app"
Supported commands are:
android list target
android list avd
android list device
android create avd
android move avd
android delete avd
android list sdk
android update sdk
执行命令出错,返回值:1。
PS D:\work2\mygame>
PS D:\work2\mygame> cocos compile -p android --android-studio --ap android-22
编译模式:debug
使用 Android Studio 工程:D:\work2\mygame\proj.android-studio
正在执行:'"C:\Users\work1\AppData\Local\Android\Sdk\tools\android" update project -t android-22 -p D:\work2\mygame\proj.android-studio\app'
Updated project.properties
Updated local.properties
Updated file D:\work2\mygame\proj.android-studio\app\proguard-project.txt
正在执行 ndk-build...
NDK 编译模式:debug
正在执行:'"D:\environment\android-ndk-r10c\ndk-build" -C D:\work2\mygame\proj.android-studio\app -j4 NDK_MODULE_PATH=D:\work2\mygame\proj.android-studio\../cocos2d;D:\work2\mygame\proj.android-studio\../cocos2d/cocos;D:\work2\mygame\proj.android-studio\../cocos2d/external NDK_TOOLCHAIN_VERSION=4.9 NDK_DEBUG=1'
FIND: 参数格式不正确
make.exe: Entering directory `D:/work2/mygame/proj.android-studio/app'
[armeabi] StaticLibrary : liblocalstorage.a
[armeabi] StaticLibrary : libluacocos2d.a
[armeabi] StaticLibrary : libwxsqlite3.a
[armeabi] StaticLibrary : libcocos2d.a
[armeabi] StaticLibrary : libcocostudio.a
……
此处神略一万行
……
BUILD SUCCESSFUL
Total time: 9.798 secs
Stopped 0 compiler daemon(s).
正在移动 apk 文件 D:\work2\mygame\bin\debug\android
编译成功。
PS D:\work2\mygame>
接下来需要修改D:\work2\mygame\proj.android-studio\build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
指定的gradle版本太低,开启工程的时候会比较麻烦,所以这里直接修改为3.0.1。
classpath 'com.android.tools.build:gradle:1.3.0' 这里的1.3.0修改为3.0.1
下面进入AS,开始导入工程:
打开工程目录,可以看到\mygame\proj.android-studio 目录出现了gradlew的标志。
进入后可能会提示:
按提示的,点击链接进行修复或升级
成功,没有提示错误了。
下面构建编译环境,在这里选择:Edit Configurations。
Add New Configuration 下选择:Run External tool
在弹出External Tools目录下,点+号,在Edit Tool里,建立一个CDT。
Tool Settings里的Program:项,是你当前Cocos版本的cocos.bat路径,我的是:
D:\environment\cocos2d-x-3.13.1\tools\cocos2d-console\bin\cocos.bat
Parameters:就是刚才在命令行里使用的导出安卓工程的命令:compile -p android --android-studio --ap android-22
Working directory:就是工程所在目录。
没问题后OK。
在Run/Debug Configurations目录下,将刚才建立的CDT提到最前面。
OK后,就可以在当前环境下编译了。
以下内容仅供同事参考:==========
AppActivity需要添加代码修复棋子变黑的Bug。
package org.cocos2dx.cpp;
import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
public class AppActivity extends Cocos2dxActivity {
@Override
public Cocos2dxGLSurfaceView onCreateView() {
Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
return glSurfaceView;
}
}
android:screenOrientation="portrait"
最后在出包前需要加密素材。
复制:DecryptResource目录和DecryptResource.bat 到工程目录下,先建立Resources的副本,然后将Resources拖拽到DecryptResource.bat,完成素材加密,最后出包。
补充:当前环境出的包在Android 4的手机下,可能会报错:
E/Auth: [GoogleAccountDataServiceImpl] getToken() -> BAD_AUTHENTICATION. Account: , App: com.google.android.gms, Service: oauth2:https://www.googleapis.com/auth/contextcontroller
edb: Long live credential not available.
E/ctxmgr: [BaseServerTask]Server task (WriteInterestRecordTask) got error statusCode=-1.
com.android.volley.VolleyError: Unable to obtain auth token - is the device online?
11-29 16:12:40.748 766-766/com.funland.animal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.funland.animal, PID: 766
java.lang.UnsatisfiedLinkError: Couldn't load MyGame from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.funland.animal-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.funland.animal-1, /vendor/lib, /system/lib]]]: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:365)
at java.lang.System.loadLibrary(System.java:606)
at org.cocos2dx.lib.Cocos2dxActivity.onLoadNativeLibraries(Cocos2dxActivity.java:248)
at org.cocos2dx.lib.Cocos2dxActivity.onCreate(Cocos2dxActivity.java:264)
at com.funland.animal.AppActivity.onCreate(AppActivity.java:80)
at android.app.Activity.performCreate(Activity.java:5354)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2320)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2407)
at android.app.ActivityThread.access$800(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5318)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:838)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:654)
at dalvik.system.NativeStart.main(Native Method)
11-29 16:12:40.814 766-766/? I/Process: Sending signal. PID: 766 SIG: 9
修改CDT编译命令:compile -p android --android-studio --ap android-9 --ndk-toolchain arm-linux-androideabi-4.8
编译出包后,解决了4.0系列闪退的问题。