环境
- 硬件:macbook pro 四核Intel Core i7
- 系统:macOS Big Sur 11.4.2、 xcode Version 13.1 、cmake 3.20.5
- 软件:iterm2 Build 3.4.8、zsh 5.8、Android Studio Dolphin | 2021.3.1
- cocos2d-x v4 :
官方下载压缩包 http://cocos2d-x.org/filedown/cocos2d-x-v4.0
前提
1、正确安装并配置好了cocos工具集
2、用cocos命令创建一个GameDemo
用如下步骤创建了Demo
# luogw @ ericluodeMacBook-Pro in ~/study/cocos/localCreateDemo [18:29:01]
$ pwd
/Users/luogw/study/cocos/localCreateDemo
$ cocos new AndGame -p com.luo.game -l cpp -d .
$ cd AndGame
$ mkdir build
$ cd build
$ cocos run --proj-dir .. -p android
在最后一步会遇到(要实际解决这个问题可能要去研究cocos命令的源码了吧)
然后我们去创建的工程目录的proj.android看一下
如上所示,看起来是一个很完整的Android工程,用AS打开去编译看一下
遇到问题
用AS直接打开尝试构建会遇到如下三个问题
1、cvc-complex-type.2.4.a: 发现了以元素 ‘base-extension‘ 开头的无效内容。应以 ‘{layoutlib}‘ 之一开头
2、ERROR: Minimum supported Gradle version is 5.4.1. Current version is 5.1.1.
3、No toolchains found in the NDK toolchains folder for ABI with prefix: arm-linux-androideabi
以过往的经验该要升级gradle版本、ndk版本了
解决方案
1、升级gradle版本
2、配置ndkversion
diff文件的内容如下(把该如下内容copy到一个文件文件,然后用patch命令给Demo打上aptch)
diff --git a/cocos2d/cocos/platform/android/libcocos2dx-with-controller/build.gradle b/cocos2d/cocos/platform/android/libcocos2dx-with-controller/build.gradle
index 94f3dd4..399a3fb 100644
--- a/cocos2d/cocos/platform/android/libcocos2dx-with-controller/build.gradle
+++ b/cocos2d/cocos/platform/android/libcocos2dx-with-controller/build.gradle
@@ -2,6 +2,7 @@ apply plugin: 'com.android.library'
android {
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
+ ndkVersion "21.3.6528147"
defaultConfig {
minSdkVersion PROP_MIN_SDK_VERSION
diff --git a/cocos2d/cocos/platform/android/libcocos2dx/build.gradle b/cocos2d/cocos/platform/android/libcocos2dx/build.gradle
index 44f9a32..50f198b 100644
--- a/cocos2d/cocos/platform/android/libcocos2dx/build.gradle
+++ b/cocos2d/cocos/platform/android/libcocos2dx/build.gradle
@@ -3,6 +3,8 @@ apply plugin: 'com.android.library'
android {
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
+ ndkVersion "21.3.6528147"
+
defaultConfig {
minSdkVersion PROP_MIN_SDK_VERSION
targetSdkVersion PROP_TARGET_SDK_VERSION
diff --git a/proj.android/app/build.gradle b/proj.android/app/build.gradle
index e91e946..db95423 100644
--- a/proj.android/app/build.gradle
+++ b/proj.android/app/build.gradle
@@ -4,7 +4,7 @@ apply plugin: 'com.android.application'
android {
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
-
+ ndkVersion "21.3.6528147"
defaultConfig {
applicationId "com.luo.game"
minSdkVersion PROP_MIN_SDK_VERSION
diff --git a/proj.android/build.gradle b/proj.android/build.gradle
index 5dff94d..fbce19f 100644
--- a/proj.android/build.gradle
+++ b/proj.android/build.gradle
@@ -7,7 +7,7 @@ buildscript {
}
dependencies {
- classpath 'com.android.tools.build:gradle:3.1.0'
+ classpath 'com.android.tools.build:gradle:3.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
diff --git a/proj.android/gradle/wrapper/gradle-wrapper.properties b/proj.android/gradle/wrapper/gradle-wrapper.properties
index 8e341b7..02471c8 100644
--- a/proj.android/gradle/wrapper/gradle-wrapper.properties
+++ b/proj.android/gradle/wrapper/gradle-wrapper.properties
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-bin.zip
最终构建
同步工程OK,直接run
后续就一路畅通,enjoy…
手机上的Demo运行效果