cocos2dx 的 Android Studio 项目配置


cocos2dx 的 Android Studio 项目配置

  • local.properties
1
2
3
4
5
6
7
8
9
## This file must *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.
#Fri Nov 22 19:24:54 CST 2019
ndk.dir=C\:\\android-ndk-r16b
sdk.dir=C\:\\Users\\Administrator\\AppData\\Local\\Android\\Sdk

ndk目录下修改build/core/default-build-commands.mk

1
TARGET_FORMAT_STRING_CFLAGS := -Wformat -Werror=format-security

1
TARGET_FORMAT_STRING_CFLAGS := -Wformat   #-Werror=format-security
  • settings.gradle
1
2
3
4
include ':libcocos2dx'
project(':libcocos2dx').projectDir = new File(settingsDir, '../cocos2d/cocos/platform/android/libcocos2dx')
include ':xxxGame'
project(':xxxGame').projectDir = new File(settingsDir, 'app')

注意 libcocos2dx 的相对路径,并且 libcocos2dx 版本不能太低,否则会报错:No implementation found for void org.cocos2dx.lib.Cocos2dxHelper.nativeSetAudioDeviceInfo

xxxGame 为你的游戏项目名称

  • gradle.properties
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
PROP_TARGET_SDK_VERSION=14
PROP_APP_ABI=armeabi-v7a
  • gradle-wrapper.properties
1
2
3
4
5
6
#Mon Nov 19 16:06:44 GMT+08:00 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
  • build.gradle (proj.android-studio)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
jcenter()
google()
}
}
  • build.gradle (libcocos2dx)
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
apply plugin: 'com.android.library'

android {
compileSdkVersion 24
buildToolsVersion "26.0.2"

defaultConfig {
minSdkVersion 10
targetSdkVersion 24
versionCode 1
versionName "1.0"
}

sourceSets.main {
aidl.srcDir "../java/src"
java.srcDir "../java/src"
manifest.srcFile "AndroidManifest.xml"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
  • build.gradle (xxxGame)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "26.0.2"

android{compileOptions.encoding="UTF-8"}

defaultConfig {
applicationId "com.xxxx.xxxGame"
minSdkVersion 10
targetSdkVersion 23
versionCode 1
versionName "1.0"

externalNativeBuild {
ndkBuild {
if (!project.hasProperty("PROP_NDK_MODE") || PROP_NDK_MODE.compareTo('none') != 0) {
// skip the NDK Build step if PROP_NDK_MODE is none
targets 'cocos2dcpp'
arguments 'NDK_TOOLCHAIN_VERSION=4.9'
arguments 'APP_PLATFORM=android-'+PROP_TARGET_SDK_VERSION

def module_paths = [project.file("../../cocos2d").absolutePath,
project.file("../../cocos2d/cocos").absolutePath,
project.file("../../cocos2d/external").absolutePath]
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
// should use '/'
module_paths = module_paths.collect {it.replaceAll('\\\\', '/')}
arguments 'NDK_MODULE_PATH=' + module_paths.join(";")
}
else {
arguments 'NDK_MODULE_PATH=' + module_paths.join(':')
}

arguments '-j' + Runtime.runtime.availableProcessors()
abiFilters.addAll(PROP_APP_ABI.split(':').collect{it as String})
}
}
}
}

sourceSets.main {
java.srcDir "src"
res.srcDir "res"
jniLibs.srcDir "libs"
manifest.srcFile "AndroidManifest.xml"
assets.srcDir "../../Resources"
}

externalNativeBuild {
ndkBuild {
if (!project.hasProperty("PROP_NDK_MODE") || PROP_NDK_MODE.compareTo('none') != 0) {
// skip the NDK Build step if PROP_NDK_MODE is none
path "jni/Android.mk"
}
}
}

signingConfigs {
release {
keyAlias 'xxx'
keyPassword 'xxxx'
storeFile file('E:/xxxx.keystore')
storePassword 'xxx'
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
if (project.hasProperty("RELEASE_STORE_FILE")) {
signingConfig signingConfigs.release
}
externalNativeBuild {
ndkBuild {
arguments 'NDK_DEBUG=0'
}
}
signingConfig signingConfigs.release
}
debug {
externalNativeBuild {
ndkBuild {
arguments 'NDK_DEBUG=1'
}
}
}
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile project(':libcocos2dx')
}
  • Android.mk
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
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 := cocos2dcpp_shared

LOCAL_MODULE_FILENAME := libcocos2dcpp


define walk
$(wildcard $(1)) $(foreach e, $(wildcard $(1)/*), $(call walk, $(e)))
endef

define uniq
$(eval seen :=) $(foreach _,$(1),$(if $(filter ${_},${seen}),,$(eval seen += ${_}))) ${seen}
endef


ALLFILES = $(call walk, $(LOCAL_PATH)/../../../Classes)

FILE_LIST := $(filter %.cpp, $(ALLFILES))
#FILE_LIST += $(filter %.c, $(ALLFILES))
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)\
LOCAL_SRC_FILES += ../../../Classes/Libs/SQLite/sqlite3secure.c
#LOCAL_SRC_FILES += $(LOCAL_PATH)/hellocpp/main.cpp
$(info "UI path2:"$(LOCAL_SRC_FILES))

FILES_PATH := $(LOCAL_PATH)/../../../Classes
FILE_INCLUDES := $(dir $(foreach src_path,$(FILES_PATH), $(call walk,$(src_path),*/) ) )
FILE_INCLUDES := $(call uniq,$(FILE_INCLUDES))

LOCAL_C_INCLUDES := $(FILE_INCLUDES)

# _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
  • Application.mk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
APP_STL := gnustl_static

APP_CPPFLAGS := -frtti -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 -std=c++11 -fsigned-char
APP_LDFLAGS := -latomic

APP_SHORT_COMMANDS := true
APP_CPPFLAGS += -Wno-error=format-security

ifeq ($(NDK_DEBUG),1)
APP_CPPFLAGS += -DCOCOS2D_DEBUG=1
APP_OPTIM := debug
else
APP_CPPFLAGS += -DNDEBUG
APP_OPTIM := release
endif
  • AndroidManifest.xml(xxxGame)
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
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxxx.xxxGame"
android:installLocation="auto">

<uses-feature android:glEsVersion="0x00020000" />

<application
android:label="@string/app_name"
android:icon="@drawable/icon">
<meta-data android:name="android.app.lib_name"
android:value="cocos2dcpp" />
<activity android:name="com.xxxx.xxxGame.AppActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="sensorPortrait"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

<supports-screens android:anyDensity="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"/>

<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
  • AndroidManifest.xml(libcocos2dx)
1
2
3
4
5
6
7
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.cocos2dx.lib">

<application android:allowBackup="true">

</application>

</manifest>
  • AppActivity
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.gamedo.clonehero.pass;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import org.cocos2dx.lib.Cocos2dxActivity;

public class AppActivity extends Cocos2dxActivity {

protected static Handler mUIHandler;

private Context mContext;
private static ImageView mWelcome = null;


// 创建一个ImageView,welcome是闪屏图片
protected ImageView createLaunchImage() {
mWelcome = new ImageView(mContext);
mWelcome.setImageResource(R.drawable.splash);
mWelcome.setScaleType(ImageView.ScaleType.CENTER_CROP);
return mWelcome;
}


// 当资源加载好了之后 删除之前创建的imageView 否则一直会在界面上显示
public static void removeLaunchImage() {
mUIHandler.post(new Runnable() {
@Override
public void run() {
if (mWelcome != null) {
mWelcome.setVisibility(View.GONE);
}
}
});
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mContext = this;
// DO OTHER INITIALIZATION BELOW
mUIHandler = new Handler();
// 显示launch image 遮住「黑屏」
addContentView(createLaunchImage(),
new WindowManager.LayoutParams(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN));
JniService.initWithActivity(this);
new Thread() {
@Override
public void run() {
super.run();
try{
Thread.sleep(5000);//休眠3秒
}catch (Exception e){

}

/**
* 要执行的操作
*/
removeLaunchImage();
}
}.start();
}

static {
System.loadLibrary("cocos2dcpp");
}
}