Cocos2dx v3 gradle build

Cocos2dx v3 gradle build
0.0 0

#1

Hi,

I am pretty new here as a “signed user” but I have been using cocos for over 4 month now.

I am currently trying to build cocos using the new gradle-experimental plugin, and it has been quite a chore. I am not yet done but still, things are building up. (note that I am using the gradle-experimental-0.6.0-alpha3 version atm)

I just wanted to share my progress (and get a bit of help if you feel like so).

Here are some basic references for building:

Here are the build.gradle file I have at the moment (many errors in linkage, but no syntax/ compilation error):

apply plugin: 'com.android.model.library'

model {

repositories {
    libs(PrebuiltLibraries) {
        chipmunk {
            headers.srcDir "../../../../external/chipmunk/include"
            binaries.withType(SharedLibraryBinary) {
                //
                // NOTE: this block should be "StaticLibraryBinary"/staticLibraryFile - but SharedLibraryBinary works and StaticLibraryBinary doesn't as of 0.6.0-alpha2
                // bug reported here: https://code.google.com/p/android/issues/detail?id=196065
                //
                sharedLibraryFile = file("../../../../external/chipmunk/prebuilt/android/${targetPlatform.getName()}/libchipmunk.a")
            }
        }
        freetype2 {
            headers.srcDir "../../../../external/freetype2/include/android/freetype2"
            binaries.withType(SharedLibraryBinary) {
                sharedLibraryFile = file("../../../../external/freetype2/prebuilt/android/${targetPlatform.getName()}/libfreetype.a")
            }
        }
        curl {
            headers.srcDir "../../../../external/curl/include/android/curl"
            binaries.withType(SharedLibraryBinary) {
                sharedLibraryFile = file("../../../../external/curl/prebuilt/android/${targetPlatform.getName()}/libcurl.a")
            }
        }
    }
}

android {
    compileSdkVersion = 21
    buildToolsVersion = "23.0.2"

    defaultConfig.with {
        minSdkVersion.apiLevel = 9
        targetSdkVersion.apiLevel = 9
    }
}
android.buildTypes {
    release {
        minifyEnabled = false
    }
}

android.ndk {
    moduleName = "cocos2dx"
    stl = "c++_static"
    cppFlags.add("-std=c++11")
    cppFlags.add("-pthread")
    cppFlags.add("-fexceptions")
    cppFlags.add("-frtti")

    CFlags.add("-DUSE_FILE32API")
    CFlags.add("-fexceptions")

    ldLibs.addAll(["atomic", "log", "android", "EGL", "GLESv2", "z"])
    abiFilters.addAll(["armeabi", "armeabi-v7a", "x86"])
}

android.sources {
    main {
        manifest.source {
            srcDir "."
            include "AndroidManifest.xml"
        }

        jni {
            source {
                // INTERNAL CPP FILES
                //srcDir "../../../../cocos/"
                // include only file at this level
                srcDir "../../../../cocos/2d"
                srcDir "../../../../cocos/math"
                srcDir "../../../../cocos/base"
                //srcDir "../../../../cocos/platform"
                // include only file at this level
                srcDir "../../../../cocos/platform/android"
                srcDir "../../../../cocos/renderer"
                srcDir "../../../../cocos/deprecated"
                srcDir "../../../../cocos/physics"
                srcDir "../../../../cocos/physics3d"
                srcDir "../../../../cocos/navmesh"

                // EXTERNAL CPP FILES
                srcDir "../../../../external/ConvertUTF"
                srcDir "../../../../external/tinyxml2"
                srcDir "../../../../external/unzip"
                srcDir "../../../../external/edtaa3func"
                srcDir "../../../../external/poly2tri"
                srcDir "../../../../external/clipper"
                srcDir "../../../../external/xxhash"
            }

            exportedHeaders {
                // INTERNAL HEADERS
                srcDir "../../../../cocos"
                srcDir "../../../../cocos/2d"
                srcDir "../../../../cocos/math"
                srcDir "../../../../cocos/base"
                srcDir "../../../../cocos/platform"
                srcDir "../../../../cocos/platform/android"
                srcDir "../../../../cocos/renderer"
                srcDir "../../../../cocos/deprecated"
                srcDir "../../../../cocos/physics"
                srcDir "../../../../cocos/physics3d"
                srcDir "../../../../cocos/navmesh"

                // EXTERNAL HEADERS
                srcDir "../../../../external"
                srcDir "../../../../external/ConvertUTF"
                srcDir "../../../../external/tinyxml2"
                srcDir "../../../../external/unzip"
                srcDir "../../../../external/edtaa3func"
                srcDir "../../../../external/poly2tri"
                srcDir "../../../../external/poly2tri/common"
                srcDir "../../../../external/poly2tre/sweep"
                srcDir "../../../../external/clipper"
                srcDir "../../../../external/xxhash"


                // STATIC LIBRARIES
                srcDir "../../../../external/chipmunk/include/chipmunk"
                srcDir "../../../../external/freetype2/include/android/freetype2"
                srcDir "../../../../external/curl/include/android"
            }

            dependencies
            {
                library "chipmunk"
                library "freetype2"
                library "curl"
            }
        }
    }
}
}
dependencies {
    compile files('../java/libs/android-async-http-1.4.8.jar')
}

Setup in android-studio is pretty straightforward and will not be covered here except for 3 points:

1°) be sure to use:

distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-all.zip

in your gradle-wrapper.properties

2°) be sure to use gradle-experimental in your general top-level gradle file:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle-experimental:0.6.0-alpha3'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

3°) The build.gradle file I have posted should be a replacement of the one found in

cocos/platform/android/libcocos2dx

I will keep this post updated as I progress. For now I am stuck on the network folder building (it uses an Android.mk file), and I have not yet decided if I should make it a sub-project with its own build.gradle file.

As I said, it doesn’t quite compile yet (link error due to MANY things)but I am slowly building the file up. Any help would be appreciated.

I may submit a pull request when it’s finished, so everyone can enjoy the new ndk support in android-studio :slight_smile:

Thanks!


How to add C++ folder to android stodio
How can I use Android Studio together with cocos2d-x? Any hints?
#2

What benefits it have over standard android-studio compile?


#3

There are many in facts:

  • faster build (see https://docs.gradle.org/current/release-notes)
  • allows to debug in android-studio using breakpoints and variable inspection, the time of gdb command line debugger will soon be over :)!!
  • code-completion and inspection in android-studio for native C++ code instead of just java

Those are the main things, but there are others.

This topic has been discussed many times in this forum (like here, it was “asked” for 3.4):

http://discuss.cocos2d-x.org/t/adding-gradle-support/17289

but the cocos team has never been clear on whether they want to migrate to gradle, I can see two reason for this:

  • they want to promote their own IDE
  • the gradle plugin is still in very early stage as of 15/12/2015, and many things are going to change over time

#4

xmmm is it something different , from what now included in cocos2d-x? It’s already possible to build project using gradle. But not sure about debugging and breakpoints from c++ code on Android.

After checking your code, it looks in gradle 2.9 no need for Android.mk and so on? Everything can be done in gradle?


#5

Avoid 65k method limit using multidex :smile:
Google Play Services library is so huge… Lately I’ve updated it and again had to remove few jars from a project, because 65k limit -.-.
Also debugging, yes!


#6

regarding 65k limit, don’t use full se of features for gps, there is option to split it to parts you only need.


#7

How? As for as I know in gradle/android studio you can import for example admob alone, but in Eclipse you can only import whole GP services project.


#8

Yes it is, and it is the main reason to switch. What is done in cocos actually is just building the java part using gradle, and running the traditional Android.mk file for native C++ code, with no benefice in android studio (no debugging, build optimisation etc).


#9

Yeah I was meaning this for gradle build (android-studio), for eclipse probably it is possible, but not that easy:)


#10

Wow, nice work. When I started using Android Studio, there was no C++ support at all.

I would gladly migrate to Gradle-only, but currently it is quite a stretch, since, as you stated, support is still experimental, and the path to migrate is hard (way harder than just switching from Eclipse build to Gradle build, judging from your post).

Do you have a repo with the changes you outlined? It would be useful for anyone who would like to help (I would love to have a look, but I’m not sure I would have the time to help anytime soon).


#11

If i remember correctly, they stated that current gradle version doesn’t support static libraries. I’m not sure if that is still valid though.


#13

It is no more valid:

cf http://tools.android.com/tech-docs/new-build-system/gradle-experimental

change log 0.4.0

Added support for dependencies on NDK plugin and shared object/static library files. Actually you can see some static linkage in my build (chipmunk, curl, freetype2)

I will put a repo up so that anyone interested may join


#14

so how far we are with this?


#15

I am kind of waiting for an answer from the dev team on this subject, whether they will support it or not, as I don’t want/ have time to do it all by myself.


#16

Jan 11th the next version will be out. Hopefully a working gradle+AndroidStudio version will be part of it.


#17

Any progress with Android Studio & gradle-experimental?


#18

You can check this post on SO. Someone helped me with the build file. I still can’t compile it at the moment as I am using the network extensions, and it was moved to core, and so there is no network.a to include.

I am currently trying to build this network extension.


#19

Would you like create a simplest cocos2dx demo repository on github which uses gradle-experimental?Currently, my complany plans to use the new build system,gradle-experimental.But errors come in.I am stuck into it.Thank you very much.


#20

I maybe late to this thread but I am also looking for Android Studio build solution. I found this link in docs:

http://cocos2d-x.org/docs/installation/Android-Studio/index.html

can anyone please verify if this works?


#21

I doesn’t! I was following the tutorial without success… I opened this issue: