Cocos2d-x 2.2.6 to gradle?

@nite @slackmoehrle

Hi,

Is there way to easy make cocos2d-x 2.2.6 compile thru Android studio/gradle - by copy pasting cocos2d-x 3.x gradle configuration files?

Is cocos2d-x 3.15 console is capable of compiling cocos2d-x 2.2.6 with gradle setup?

We want to make update for older project made on cocos2d-x 2.x - but this is really pain as no Android SDK or Eclipse not supporting anymore ant builds ;( and it will be hard to manage new integrations - so looking for gradle setup :confused:

Is it something working solution?

http://blog.kasajei.com/entry/2015/04/05/AndroidStuido%26coco2d-x_ver2_2_6でgradleビルドする方法

I think you can ref to bellow folders:

~/test/cpp3151$ find . -name "build.gradle"
./cocos2d/cocos/platform/android/libcocos2dx/build.gradle
./proj.android-studio/app/build.gradle
./proj.android-studio/build.gradle
1 Like

@zhangxm do you know any team in china has compiled 2.2.6 with android studio?

No, i haven’t heard that.
@energyy you can still use ant, just copy tools folder from older Android SDK.

no, we now hate ant :smiley: after using gradle.

@nite @zhangxm
Good news:) I managed to make it working with Android studio/gradle - so I can compile project from Android studio and from console using cocos2d-x 3.15.1 console :slight_smile:

Actually it is very easy to do if u know what to do:D even thinking that u can add android studio support to 2.2.6 an release it :slight_smile:

I managed to make it working with project setup from cocos 3.14 and 3.15 :wink:

To make 2.2.6 compile in gradle as cocos 3.14 version:

  1. Using NDK 10, Cocos2d-x 3.15.1 console which supports latest adnroid sdk.

  2. Copying from 3.14 project folder file .cocos-project.json to 2.2.6 project folder (as cocos console checking for this file)

  3. Copying from 3.14 project folder proj.android-studio to 2.2.6 project

  4. Replacing in proj.android-studio main.h file in JNI folder to original one from proj.android

  5. Most tricky part was to change build-cfg.json inside proj.android-studio:

    {
    “ndk_module_path” :[
    “…/…/…/cocos2dx”,
    “…/…/…/external”,
    “…/…/…/cocos2dx/platform/third_party/android/prebuilt”,
    “…/…/…/extensions”,
    “…/…/…/CocosDenshion/android”

     ],
     "copy_resources": [
         {
             "from": "../Resources",
             "to": ""
         }
     ]
    

    }

  6. Copying folder from cocos 3.14 cocos2d/cocos/platform/android/libcocos2dx to cocos2d-x-2.2.6/cocos2dx/platform/android/libcocos2dx - it contains build.gradle from cocos - so after that u just need to adjust in settings.gradle path to point cocos2d-x-2.2.6/cocos2dx/platform/android/libcocos2dx

After that I managed to compile project, also made it working with build.gradle from cocos 3.15 to make it able compile from Android Studio directly, also imported SDKBOX and Fabric and it also worked :slight_smile:

Also good news that I can compile cocos2d-x 2.2.6 IOS project with latest Xcode 8.3.3 - so it works together still :slight_smile:

Example of build.gradle from app folder if u want to use fabric, Android-studio direct compile in cocos 2.2.6:

import org.apache.tools.ant.taskdefs.condition.Os




buildscript {
    repositories {
        jcenter()
        maven { url 'https://maven.fabric.io/public'
        }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        // The Fabric Gradle plugin uses an open ended version to
        // react quickly to Android tooling updates
        classpath 'io.fabric.tools:gradle:1.+'
    }
}


apply plugin: 'com.android.application'
apply plugin: 'io.fabric'



repositories {
    jcenter()
    maven { url 'https://maven.fabric.io/public' }

    flatDir{
        dirs 'heyzap'
    }
}



android {
    compileSdkVersion 22
    buildToolsVersion "25.0.0"

    defaultConfig {
        applicationId "com.test.game2048"
        minSdkVersion 16
        targetSdkVersion 22
        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-16'

                    def module_paths = [project.file("../../../../cocos2dx").absolutePath,
                                        project.file("../../../../external").absolutePath,
                                        project.file("../../../../extensions").absolutePath,
                                        project.file("../../../../cocos2dx/platform/third_party/android/prebuilt").absolutePath,
                                        project.file("../../../../CocosDenshion/android").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})
                }
            }
        }
    }


    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }


    sourceSets.main {
        java.srcDir "src"
        res.srcDir "res"
        manifest.srcFile "AndroidManifest.xml"
        assets.srcDir "../../Resources"
        jniLibs.srcDirs = ['libs']

    }

    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"
            }
        }
    }

    dexOptions {
        javaMaxHeapSize "4g"
    }

    splits {
        abi {
            enable true
            reset()
            include 'armeabi'
            universalApk false
        }
    }

    signingConfigs {

        release {
            if (project.hasProperty("RELEASE_STORE_FILE")) {
                storeFile file(RELEASE_STORE_FILE)
                storePassword RELEASE_STORE_PASSWORD
                keyAlias RELEASE_KEY_ALIAS
                keyPassword RELEASE_KEY_PASSWORD
            }
        }
    }

    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'
                }
            }
        }

        debug {
            externalNativeBuild {
                ndkBuild {
                    arguments 'NDK_DEBUG=1'
                }
            }
        }
    }
}



crashlytics {
    enableNdk true
    androidNdkOut 'build/intermediates/ndkBuild/release/obj/local'
    androidNdkLibsOut 'build/intermediates/ndkBuild/release/obj/local'
}



dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile fileTree(include: ['*.jar'], dir: 'heyzap')
    compile project(':libcocos2dx')





    // Crashlytics Kit
    compile('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') {
        transitive = true
    }
    // NDK Kit
    compile('com.crashlytics.sdk.android:crashlytics-ndk:1.1.6@aar') {
        transitive = true
    }
}
3 Likes

Could you help me on this ? Maybe tell me more about the 2-5 steps, thanks!

For 3.14 project I mean hello world project generated using cocos2d-x 3.14 version

@energyy thanks for the steps, but why are you referring to both cocos 3.14 and 3.15? Is it possible to do all steps with latest cocos2dx version (3.17.1) instead? As I will work on latest android studio (3.3.1)

Not sure if you noticed, but v3.17.1 didn’t exist in November 2017 :wink:

I think 3.16 + little changed compile script and gradle files - I would recommend first to make it working as I described and then update gradle files logic to 3.17 - most probably even there is no difference only some cosmetic code changes

1 Like

by HelloWorld project you mean cocos2d-x 3.14/tests/cpp-tests?

There is only main.cpp under jni folder within project.android-studio

No I mean create new project based on cocos2d-x and use files from it… like
cocos new -l cpp - p…

yes , there is only main.cpp file

I did a new cpp project using the 3.15 console. however the output project.android-studio didn’t contain any build-cfg.json what should I do?

Just create it manually :wink: adjust

1 Like