温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MAC下Android Studio 1.4编译构建Cocos2d-x 3.9之Android版本程序

发布时间:2020-07-06 18:16:42 来源:网络 阅读:2222 作者:googlingman 栏目:游戏开发


      本文记载的是一个HELLO WORLD级别的入门程序在如题所示环境下的编译构建过程。

      说明:如今安装Android Studio 1.4,它会自动安装Gradle 2.4(早期却不是这样),但遗憾的是没有下载对应的.zip文件。所以,后面在命令行编译时还需要再次下载这个文件。

另外,在MAC环境下下载安装Cocos2d-x 3.9Android Studio 1.4的步骤,在此省略描述。

环境变量配置

终端工具下,运行如下命令:

vim ~/.bash_profile

输入如下内容:

ANT_ROOT='/usr/local/Cellar/ant/1.9.6/bin'

exportANT_ROOT

COCOS_PATH='~/Downloads/cocos2d-x-3.9/tools/cocos2d-console/bin'

exportCOCOS_PATH

exportPATH=$PATH:$COCOS_PATH:$ANT_ROOT

exportPATH=$PATH:/Users/zxzpc/Library/Android/sdk/tools

exportPATH=$PATH:/Users/zxzpc/LIbrary/Android/sdk/build-tools

exportPATH=$PATH:/Users/zxzpc/Downloads/android-ndk-r10e

exportPATH=$PATH:/Users/zxzpc/usr/local/Cellar/ant/1.9.6/bin

 

exportANDROID_SDK_ROOT=/Users/zxzpc/Library/Android/sdk

exportANDROID_NDK_ROOT=/Users/zxzpc/Downloads/android-ndk-r10e

exportNDK_ROOT=/Users/zxzpc/Downloads/android-ndk-r10e

 

#Add gradle variable export

GRADLE_HOME='/Applications/Android\Studio.app/Contents/gradle/gradle-2.4/bin'

exportPATH=$PATH:$GRADLE_HOME

 

接下来运行下面命令:

source ~/.bash_profile

注意,这里的source命令用于重新执行刚刚修改的系统环境配置文件,使之内容立即生效,而不必注销并重新登录。这一点很重要;这样以后,从命令行下执行cocos命令,它就能够自动找到上述SDK所在位置。

Android Studio 1.4下打开工程与修改文件gradle-wrapper.properties

启动Android Studio 1.4打开示例工程MyHelloWorld(使用命令“File”“Open”打开文件夹proj.android-studio即可)。因为文件gradle-wrapper.properties在后期命令行编译时使用到,所以需要根据实际情况进行修改。初始内容如下:

#Fri Jul 31 20:40:49 CST 2015

distributionBase=GRADLE_USER_HOME

distributionPath=wrapper/dists

zipStoreBase=GRADLE_USER_HOME

zipStorePath=wrapper/dists

distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip

但是,我在第一次命令行编译时出现网络连接错误。大致的意思是,连接不到上面一行指定的网址,这种原因估计一般程序员都知道。于是,根据网络上的提示,我想也把文件gradle-2.4-all.zip下载到本地,并修改上面的distributionUrl参数。

文件gradle-2.4-all.zip下载倒不是难事,大家可以参考本文后面的下载网址。但是,修改文件倒是出现麻烦了。修改后内容如下:

#Fri Jul 31 20:40:49 CST 2015

distributionBase=GRADLE_USER_HOME

distributionPath=wrapper/dists

zipStoreBase=GRADLE_USER_HOME

zipStorePath=wrapper/dists

#distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip

distributionUrl==file\:/Users/zxzpc/Downloads/gradle-2.4-all.zip

但是,接下来,再次在命令行运行命令cocos compile -p android --android-studio时,还是出现错误,如下所示:

——————————————————————————————

编译模式:debug

使用 Android Studio 工程:/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio

未指定 Android 目标平台版本,自动查找一个可用版本...

正在执行:'/Users/zxzpc/Library/Android/sdk/tools/android update project -tandroid-10 -p /Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/app'

 

Updated project.properties

Updated local.properties

Updated file/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/app/proguard-project.txt

正在执行 ndk-build...

NDK 编译模式:debug

NDK_TOOLCHAIN_VERSION: 4.9

正在执行:'/Users/zxzpc/Downloads/android-ndk-r10e/ndk-build -C/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/app -j4NDK_MODULE_PATH=/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/../cocos2d:/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/../cocos2d/cocos:/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/../cocos2d/externalNDK_TOOLCHAIN_VERSION=4.9 NDK_DEBUG=1'

 

make: Entering directory`/Users/zxzpc/Documents/MyHelloWorld/proj.android-

 

。。。。。。。。。。(省略)

 

[armeabi] Install        : libcocos2dcpp.so =>libs/armeabi/libcocos2dcpp.so

make: Leaving directory`/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/app'

正在生成 apk 文件...

正在执行:'"/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/gradlew"--parallel --info assembleDebug'

 

Exception in thread "main"java.lang.RuntimeException: Could not load wrapper properties from'/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/gradle/wrapper/gradle-wrapper.properties'.

       atorg.gradle.wrapper.WrapperExecutor.<init>(WrapperExecutor.java:65)

       atorg.gradle.wrapper.WrapperExecutor.forWrapperPropertiesFile(WrapperExecutor.java:49)

       atorg.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:47)

Caused by: java.net.URISyntaxException: Illegalcharacter in scheme name at index 0:=file:/Users/zxzpc/Downloads/gradle-2.4/gradle-2.4-all.zip

       atjava.net.URI$Parser.fail(URI.java:2829)

       atjava.net.URI$Parser.checkChars(URI.java:3002)

       atjava.net.URI$Parser.checkChar(URI.java:3012)

       atjava.net.URI$Parser.parse(URI.java:3028)

       atjava.net.URI.<init>(URI.java:595)

       atorg.gradle.wrapper.WrapperExecutor.readDistroUrl(WrapperExecutor.java:82)

       atorg.gradle.wrapper.WrapperExecutor.prepareDistributionUri(WrapperExecutor.java:71)

       atorg.gradle.wrapper.WrapperExecutor.<init>(WrapperExecutor.java:59)

       ...2 more

执行命令出错,返回值:1

——————————————————————————————

问题显然出在『Illegal character in schemename at index 0』这一句。

就是这个问题上,我纠结了不少时间,尽管这种错误提示看起来并不复杂。因为实在没有多少这方面的经验,所以问题停留在:在.zip文件内容出现问题,还是上面引用本机文件格式出现问题?

再次修改文件与命令行下成功编译

经过不断尝试与借鉴网络查询结果,最终我还是把上面文件内容还原到最初状态,如下所示:

#Fri Jul 31 20:40:49 CST 2015

distributionBase=GRADLE_USER_HOME

distributionPath=wrapper/dists

zipStoreBase=GRADLE_USER_HOME

zipStorePath=wrapper/dists

distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip

 

 

再次启动MAC终端,CD到示例工程HELLOWORLD目录下,然后执行如下命令:

cocos compile -p android --android-studio

显示及输出内容如下:

————————————————————————————————-

编译模式:debug

使用 Android Studio 工程:/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio

未指定 Android 目标平台版本,自动查找一个可用版本...

正在执行:'/Users/zxzpc/Library/Android/sdk/tools/android update project -tandroid-10 -p /Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/app'

 

Updated project.properties

Updated local.properties

Updated file/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/app/proguard-project.txt

正在执行 ndk-build...

NDK 编译模式:debug

NDK_TOOLCHAIN_VERSION: 4.9

正在执行:'/Users/zxzpc/Downloads/android-ndk-r10e/ndk-build -C/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/app -j4NDK_MODULE_PATH=/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/../cocos2d:/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/../cocos2d/cocos:/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/../cocos2d/externalNDK_TOOLCHAIN_VERSION=4.9 NDK_DEBUG=1'

 

make: Entering directory`/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/app'

/Users/zxzpc/Downloads/android-ndk-r10e/build/core/build-binary.mk:466:warning: overriding commands for target`obj/local/armeabi/objs-debug/bullet_static/BulletMultiThreaded/SpuLibspe2Support.o'

/Users/zxzpc/Downloads/android-ndk-r10e/build/core/build-binary.mk:466:warning: ignoring old commands for target`obj/local/armeabi/objs-debug/bullet_static/BulletMultiThreaded/SpuLibspe2Support.o'

[armeabi] StaticLibrary  : libcocos2d.a

[armeabi] StaticLibrary  : libcocostudio.a

[armeabi] StaticLibrary  : libcocosbuilder.a

[armeabi] StaticLibrary  : libcocos3d.a

[armeabi] StaticLibrary  : libspine.a

[armeabi] StaticLibrary  : libui.a

[armeabi] StaticLibrary  : libcocosdenshion.a

[armeabi] StaticLibrary  : flatbuffers.a

[armeabi] StaticLibrary  : libextension.a

[armeabi] StaticLibrary  : libaudioengine.a

[armeabi] StaticLibrary  : libbox2d.a

[armeabi] StaticLibrary  : libnetwork.a

[armeabi] StaticLibrary  : libcocos2dxinternal.a

[armeabi] StaticLibrary  : librecast.a

[armeabi] StaticLibrary  : libbullet.a

[armeabi] StaticLibrary  : libcocos2dandroid.a

[armeabi] StaticLibrary  : libcpufeatures.a

[armeabi] Gdbserver      : [arm-linux-androideabi-4.9]libs/armeabi/gdbserver

[armeabi] Gdbsetup       : libs/armeabi/gdb.setup

[armeabi] SharedLibrary  : libcocos2dcpp.so

[armeabi] Install        : libcocos2dcpp.so =>libs/armeabi/libcocos2dcpp.so

make: Leaving directory`/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/app'

正在生成 apk 文件...

正在执行:'"/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/gradlew"--parallel --info assembleDebug'

 

Downloadinghttps://services.gradle.org/distributions/gradle-2.4-all.zip

.......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

Unzipping/Users/zxzpc/.gradle/wrapper/dists/gradle-2.4-all/3i2gobhdl0fm2tosnn15g540i0/gradle-2.4-all.zipto /Users/zxzpc/.gradle/wrapper/dists/gradle-2.4-all/3i2gobhdl0fm2tosnn15g540i0

Set executable permissions for:/Users/zxzpc/.gradle/wrapper/dists/gradle-2.4-all/3i2gobhdl0fm2tosnn15g540i0/gradle-2.4/bin/gradle

Starting Build

Settings evaluated using settings file'/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/settings.gradle'.

Projects loaded. Root project using buildfile '/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/build.gradle'.

Included projects: [root project'proj.android-studio', project ':MyHelloWorld', project ':libcocos2dx']

Parallel execution is an incubatingfeature.

Evaluating root project'proj.android-studio' using build file'/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/build.gradle'.

Downloadhttps://jcenter.bintray.com/com/android/tools/build/gradle/1.3.0/gradle-1.3.0.pom

Downloadhttps://jcenter.bintray.com/com/android/tools/build/gradle-core/1.3.0/gradle-core-1.3.0.pom

Downloadhttps://jcenter.bintray.com/com/android/tools/build/builder/1.3.0/builder-1.3.0.pom

Downloadhttps://jcenter.bintray.com/com/android/tools/lint/lint/24.3.0/lint-24.3.0.pom

//……………………………………..(a lot more, omitted)

Downloadhttps://jcenter.bintray.com/org/apache/httpcomponents/httpcore/4.1/httpcore-4.1.jar

Downloadhttps://jcenter.bintray.com/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar

Downloadhttps://jcenter.bintray.com/commons-codec/commons-codec/1.4/commons-codec-1.4.jar

Downloadhttps://jcenter.bintray.com/com/android/tools/external/lombok/lombok-ast/0.2.3/lombok-ast-0.2.3.jar

Evaluating project ':MyHelloWorld' usingbuild file'/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/app/build.gradle'.

Creating configuration compile.

Creating configuration apk.

Creating configuration provided.

Creating configuration wearApp.

Creating configuration androidTestCompile.

Creating configuration androidTestApk.

Creating configuration androidTestProvided.

Creating configuration androidTestWearApp.

Creating configuration testCompile.

Creating configuration testApk.

Creating configuration testProvided.

Creating configuration testWearApp.

Creating configuration debugCompile.

Creating configuration debugApk.

Creating configuration debugProvided.

Creating configuration debugWearApp.

Creating configuration testDebugCompile.

Creating configuration testDebugApk.

Creating configuration testDebugProvided.

Creating configuration testDebugWearApp.

Creating configuration releaseCompile.

Creating configuration releaseApk.

Creating configuration releaseProvided.

Creating configuration releaseWearApp.

Creating configuration testReleaseCompile.

Creating configuration testReleaseApk.

Creating configuration testReleaseProvided.

Creating configuration testReleaseWearApp.

Evaluating project ':libcocos2dx' usingbuild file '/Users/zxzpc/Documents/MyHelloWorld/cocos2d/cocos/platform/android/libcocos2dx/build.gradle'.

Creating configuration compile.

Creating configuration publish.

//……………………………………..(a lot more, omitted)

Creating configuration testReleaseWearApp.

All projects evaluated.

Selected primary task 'assembleDebug' fromproject :

Tasks to be executed: [task':MyHelloWorld:preBuild', task ':MyHelloWorld:preDebugBuild', task':MyHelloWorld:checkDebugManifest', task ':MyHelloWorld:preReleaseBuild', task':libcocos2dx:compileLint', task ':libcocos2dx:copyReleaseLint', task':libcocos2dx:mergeReleaseProguardFiles', task ':libcocos2dx:preBuild', task':libcocos2dx:preReleaseBuild', task ':libcocos2dx:checkReleaseManifest', task':libcocos2dx:prepareReleaseDependencies', task':libcocos2dx:compileReleaseAidl', //……………………………………..(a lot more, omitted)

:MyHelloWorld:zipalignDebug

Executing task':MyHelloWorld:zipalignDebug' (up-to-date check took 0.063 secs) due to:

 Input file/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/app/build/outputs/apk/MyHelloWorld-debug-unaligned.apkhas changed.

Starting process 'command'/Users/zxzpc/Library/Android/sdk/build-tools/21.1.2/zipalign''. Working directory:/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/app Command:/Users/zxzpc/Library/Android/sdk/build-tools/21.1.2/zipalign -f 4/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/app/build/outputs/apk/MyHelloWorld-debug-unaligned.apk/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio/app/build/outputs/apk/MyHelloWorld-debug.apk

Successfully started process 'command'/Users/zxzpc/Library/Android/sdk/build-tools/21.1.2/zipalign''

:MyHelloWorld:zipalignDebug (Thread[Taskworker,5,main]) completed. Took 0.16 secs.

:MyHelloWorld:assembleDebug (Thread[Taskworker,5,main]) started.

:MyHelloWorld:assembleDebug

Skipping task ':MyHelloWorld:assembleDebug'as it has no actions.

:MyHelloWorld:assembleDebug (Thread[Taskworker,5,main]) completed. Took 0.0 secs.

 

BUILD SUCCESSFUL

 

Total time: 19 mins 12.95 secs

Stopped 0 compiler daemon(s).

 

This build could be faster, please considerusing the Gradle Daemon:http://gradle.org/docs/2.4/userguide/gradle_daemon.html

正在移动 apk 文件 /Users/zxzpc/Documents/MyHelloWorld/bin/debug/android

编译成功。

————————————————————————————————


显然,下载gradle-2.4-all.zip和相应解压花费了不少时间,但总算有了一个初步的惊喜。而且,如读者所想,使用本地的文件是节约时间的最佳办法。无奈,拙人我现在还没有找到。以后找到,一定记录下来。


剩下的好办了,把生成的APK文件设法复制到我的三星I9100机器上(还是Android 2.3.4版本)。安装运行即可,截图如下:

 

MAC下Android Studio 1.4编译构建Cocos2d-x 3.9之Android版本程序

注意:图中中文显示不正常,是因为我没有作相应处理而已。

小结

Android Studio 1.4编译与构建速度比Eclipse快几十倍,模拟器启动速度也快得多。尽管我的编译工程不是在IDE环境下实现的。

命令行编译还是更迅速,值得推荐。

本次记录的是MAC环境命令行下的编译生成APK文件的过程。走了不少曲折的路,记录下来供自己备忘,也供同志们参考。

引用

 

  • Gradle下载页面:http://services.gradle.org/distributions。非常全面,值得推荐。

 

 

 


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI