zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

使用Android Studio进行cocos2d-x开发

AndroidStudio开发 进行 cocos2d 使用
2023-09-27 14:28:00 时间

cocos2d-x进行android时官方使用的是eclipse+ADT+NDK的模式,但是随着Android Studio正式版的推出,越来越多的开发者正开始转变他们的开发工具为Android Studio。

Android Studio相对于eclipse+ADT的优势 Android Studio是基于Intellij的IDEA社区版进行开发而成的,而IDEA则被誉为最智能的JAVA开发工具,Android Studio在智能提醒、预览xml布局界面等功能方面相较于eclipse方面是比较优势的。 ADT正在被谷歌官方边缘化,谷歌方面已经宣布不再对ADT进行支持开发了,这将意味着很多bug得不到及时的修复、新功能难以添加上去。 Android Studio具有更加优秀的用户界面。 使用Android Studio进行cocos2d-x的开发 Android Studio的ndk配置

Android Studio使用ndk十分方便。


1
2
3
4
5
6
7
8
9
10
11
## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file should *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=C\:/Android/android-sdk-windows
ndk.dir=C\:/Android/android-ndk-r10e

1
2
3
4
5
sourceSets.main {
jni.srcDirs = [] // 这里代表的是c++源码的目录,默认不写为src/main/jni下,因为在windows下直接使用gradle进行编译会出错,因为这里将它制空,然后后面用其他脚本进行编译
jniLibs.srcDir src/main/libs // 生成的.so的目录
assets.srcDir src/main/assets // assets目录
} /span br /pre /td /tr /tbody /table /figure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
buildscript {
repositories {
jcenter()
}
dependencies {
classpath com.android.tools.build:gradle:1.1.1
}
}
apply plugin: com.android.application

repositories {
jcenter()
// flatDir{
// dirs libs
// }
}

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
applicationId "com.vincent.twoplusone.app"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_6
targetCompatibility JavaVersion.VERSION_1_6
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(proguard-android.txt), proguard-rules.pro
}
}

sourceSets.main {
jni.srcDirs = []
jniLibs.srcDir src/main/libs
assets.srcDir src/main/assets
}
}

dependencies {
compile fileTree(dir: libs, include: [*.jar])
// compile(name:loginModule-release, ext:aar)
compile project(:loginModule)
compile com.android.support:appcompat-v7:22.2.0
}

至此ndk配置完成。

使用Android Stduio开发cocos2d-x

首先用Android Studio和cocos的脚本分别创建一个新的项目,然后就是将cocos项目中对应的文件拷贝到相应的目录下面。


最后因为Android Studio在window下直接使用ndk进行编译会因为一些全局变量的缺失导致失败,需要使用在src\main 目录下使用以下脚本进行编译(其中一些全局变量需要自行配制):

1
ndk-build NDK_ROOT=%NDK_ROOT% NDK_MODULE_PATH=%NDK_MODULE_PATH% ANDROID_SDK_ROOT=%ANDROID_SDK_ROOT% NDK_TOOLCHAIN_VERSION=4.9