您好,登錄后才能下訂單哦!
本文記載的是一個HELLO WORLD級別的入門程序在如題所示環(huán)境下的編譯構(gòu)建過程。
說明:如今安裝Android Studio 1.4,它會自動安裝Gradle 2.4(早期卻不是這樣),但遺憾的是沒有下載對應(yīng)的.zip文件。所以,后面在命令行編譯時還需要再次下載這個文件。
另外,在MAC環(huán)境下下載安裝Cocos2d-x 3.9和Android Studio 1.4的步驟,在此省略描述。
終端工具下,運行如下命令:
vim ~/.bash_profile
輸入如下內(nèi)容:
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命令用于重新執(zhí)行剛剛修改的系統(tǒng)環(huán)境配置文件,使之內(nèi)容立即生效,而不必注銷并重新登錄。這一點很重要;這樣以后,從命令行下執(zhí)行cocos命令,它就能夠自動找到上述SDK所在位置。
啟動Android Studio 1.4打開示例工程MyHelloWorld(使用命令“File”-“Open”打開文件夾proj.android-studio即可)。因為文件gradle-wrapper.properties在后期命令行編譯時使用到,所以需要根據(jù)實際情況進行修改。初始內(nèi)容如下:
#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
但是,我在第一次命令行編譯時出現(xiàn)網(wǎng)絡(luò)連接錯誤。大致的意思是,連接不到上面一行指定的網(wǎng)址,這種原因估計一般程序員都知道。于是,根據(jù)網(wǎng)絡(luò)上的提示,我想也把文件gradle-2.4-all.zip下載到本地,并修改上面的distributionUrl參數(shù)。
文件gradle-2.4-all.zip下載倒不是難事,大家可以參考本文后面的下載網(wǎng)址。但是,修改文件倒是出現(xiàn)麻煩了。修改后內(nèi)容如下:
#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時,還是出現(xiàn)錯誤,如下所示:
——————————————————————————————
編譯模式:debug
使用 Android Studio 工程:/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio
未指定 Android 目標平臺版本,自動查找一個可用版本...
正在執(zhí)行:'/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
正在執(zhí)行 ndk-build...
NDK 編譯模式:debug
NDK_TOOLCHAIN_VERSION: 4.9
正在執(zhí)行:'/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 文件...
正在執(zhí)行:'"/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
執(zhí)行命令出錯,返回值:1。
——————————————————————————————
問題顯然出在『Illegal character in schemename at index 0』這一句。
就是這個問題上,我糾結(jié)了不少時間,盡管這種錯誤提示看起來并不復(fù)雜。因為實在沒有多少這方面的經(jīng)驗,所以問題停留在:在.zip文件內(nèi)容出現(xiàn)問題,還是上面引用本機文件格式出現(xiàn)問題?
經(jīng)過不斷嘗試與借鑒網(wǎng)絡(luò)查詢結(jié)果,最終我還是把上面文件內(nèi)容還原到最初狀態(tài),如下所示:
#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目錄下,然后執(zhí)行如下命令:
cocos compile -p android --android-studio
顯示及輸出內(nèi)容如下:
————————————————————————————————-
編譯模式:debug
使用 Android Studio 工程:/Users/zxzpc/Documents/MyHelloWorld/proj.android-studio
未指定 Android 目標平臺版本,自動查找一個可用版本...
正在執(zhí)行:'/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
正在執(zhí)行 ndk-build...
NDK 編譯模式:debug
NDK_TOOLCHAIN_VERSION: 4.9
正在執(zhí)行:'/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 文件...
正在執(zhí)行:'"/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和相應(yīng)解壓花費了不少時間,但總算有了一個初步的驚喜。而且,如讀者所想,使用本地的文件是節(jié)約時間的最佳辦法。無奈,拙人我現(xiàn)在還沒有找到。以后找到,一定記錄下來。
剩下的好辦了,把生成的APK文件設(shè)法復(fù)制到我的三星I9100機器上(還是Android 2.3.4版本)。安裝運行即可,截圖如下:
注意:圖中中文顯示不正常,是因為我沒有作相應(yīng)處理而已。
Android Studio 1.4編譯與構(gòu)建速度比Eclipse快幾十倍,模擬器啟動速度也快得多。盡管我的編譯工程不是在IDE環(huán)境下實現(xiàn)的。
命令行編譯還是更迅速,值得推薦。
本次記錄的是MAC環(huán)境命令行下的編譯生成APK文件的過程。走了不少曲折的路,記錄下來供自己備忘,也供同志們參考。
Gradle下載頁面:http://services.gradle.org/distributions。非常全面,值得推薦。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。