android-studio中基于cocos2dx的项目使用gradle-experimental插件

时间:2015-12-10 16:38:11

标签: c++ android-ndk android-gradle cocos2d-x gradle-experimental

我有一个符合gradle实验的0.6.0-alpha1兼容的build.gradle文件:

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

model {
    android {
        compileSdkVersion = 21
        buildToolsVersion = "23.0.2"

        defaultConfig.with {
            applicationId = "com.company.application"
            minSdkVersion.apiLevel = 21
            targetSdkVersion.apiLevel = 21
        }
    }
    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file('proguard-rules.pro'))
        }
    }

    android.ndk {
        moduleName = "moduleName"
    }
}

dependencies {
    compile project(':libcocos2dx')
}

我正在使用cocos2dx作为我的引擎。我有一个文件夹层次结构,如下所示:

  • 类(包含我项目的所有C ++代码)
  • proj.android/src/main/projectName/jni(其中包含要挂钩的cocos2d的main.cpp文件)

我还有许多其他cpp类,它们位于proj.android之外的不同文件夹中(例如cocos2d cpp文件位于proj.android/../../../dependencies/XXXX/cocos2d- x和引擎文件位于proj.android/../../../dependencies/XXXX/engine,我所有的C ++代码都在proj.android/../Classes,就像在每个cocos2dx项目中一样)

我正在尝试使用最新的gradle实验插件(gradle-experimental-0.6.0-alpha1)构建我的应用程序。

我首先尝试按原样构建它,项目编译并运行但是为空(没有生成libMyApp.so),因为我没有在我的gradle构建中包含任何C ++源文件。

然后我创建了一些指向源文件文件夹的符号链接,以便gradle可以选择它们:

ln -s proj.android/../../../dependencies/XXXX/cocos2d-x proj.android/src/main/projectName/jni

现在当我构建(./gradlew assembleDebug)时,我得到它编译符号链接的C ++文件,但无论我尝试什么,它都会在每次第一次包含头文件时失败。

我尝试使用以下内容添加源文件/标头包含here

android.sources {
        main {
            jni {
                source {
                    srcDir "src"
                }
                exportedHeaders {
                    srcDir "src"
                }
            }
        }
    }

但我无法找到丢失的“.h”标题。如果有人试图使用cocos2Dx,gradle-experimental和android-studio构建一个后续的C ++项目,请随时分享任何经验。我将尝试编译某种指南,当我能够编译它时(有一天:))

供参考:最新的gradle doc:

https://docs.gradle.org/2.9/userguide/nativeBinaries.html

有用的链接:

http://ph0b.com/new-android-studio-ndk-support/

2 个答案:

答案 0 :(得分:3)

嗨感谢您的文件,我对您的文件进行了一些修改。

我的工作原理是,我在Mac OSX上用cocos2Dx 3.9做到了。

你需要:com.android.tools.build:gradle-experimental:0.6.0-alpha5'

您可以同时在Java和C ++中放置断点。

Cyrl

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

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")
                }
            }
            png {
                headers.srcDir "../../../../external/png/include/android"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file("../../../../external/png/prebuilt/android/${targetPlatform.getName()}/libpng.a")
                }
            }
            jpeg {
                headers.srcDir "../../../../external/jpeg/include/android"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file("../../../../external/jpeg/prebuilt/android/${targetPlatform.getName()}/libjpeg.a")
                }
            }
            tiff {
                headers.srcDir "../../../../external/tiff/include/android"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file("../../../../external/tiff/prebuilt/android/${targetPlatform.getName()}/libtiff.a")
                }
            }
            webp {
                headers.srcDir "../../../../external/webp/include/android"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file("../../../../external/webp/prebuilt/android/${targetPlatform.getName()}/libwebp.a")
                }
            }
            bullet {
                headers.srcDir "../../../../external/bullet/"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file("../../../../external/bullet/obj/local/${targetPlatform.getName()}/libbullet.a")
                }
            }
            recast {
                headers.srcDir "../../../../external/recast/"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file("../../../../external/recast/obj/local/${targetPlatform.getName()}/librecast.a")
                }
            }
            websockets {
                headers.srcDir "../../../../external/websockets/include/android"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file("../../../../external/websockets/prebuilt/android/${targetPlatform.getName()}/libwebsockets.a")
                }
            }
            ssl {
                headers.srcDir "../../../../external/curl/include/android/curl"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file("../../../../external/curl/prebuilt/android/${targetPlatform.getName()}/libssl.a")
                }
            }
            crypto {
                headers.srcDir "../../../../external/curl/include/android/curl"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file("../../../../external/curl/prebuilt/android/${targetPlatform.getName()}/libcrypto.a")
                }
            }
        }
    }

    android {
        compileSdkVersion = 21
        buildToolsVersion = "23.0.2"

        defaultConfig.with {
            minSdkVersion.apiLevel = 9
            targetSdkVersion.apiLevel = 9
        }
    }

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

    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/sourceFile"
                    srcDir "../../../../cocos/3d"
                    srcDir "../../../../cocos/math"
                    srcDir "../../../../cocos/base"
                    srcDir "../../../../cocos/platform/sourceFiles"
                    srcDir "../../../../cocos/cpufeatures"
                    // 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"
                    srcDir "../../../../cocos/network"
                    srcDir "../../../../tests/cpp-empty-test/Classes"
                    srcDir "../../../../tests/cpp-empty-test/Main"

                    // 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"
                    srcDir "../../../../tests/cpp-empty-test/Classes"


                    // 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"
                    srcDir "../../../../external/png/include/android"
                    srcDir "../../../../external/tiff/include/android"
                    srcDir "../../../../external/jpeg/include/android"
                    srcDir "../../../../external/webp/include/android"
                    srcDir "../../../../external/websockets/include/android"
                }

                dependencies {
                    library "chipmunk"
                    library "freetype2"
                    library "curl"
                    library "png"
                    library "jpeg"
                    library "tiff"
                    library "webp"
                    library "bullet"
                    library "recast"
                    library "websockets"
                    library "ssl"
                    library "crypto"
                }
            }
        }
    }

    android.buildTypes {
        release {

        }
        debug {
            ndk.with {
                debuggable = true
            }
        }
    }

    android.productFlavors {
        create ("arm") {
            ndk.with {
                abiFilters.add("armeabi-v7a")
                ldFlags.addAll([
                        "-L${file("./obj/local/armeabi-v7a")}".toString()
                ])
            }
        }
        create("x86") {
            ndk.with {
                abiFilters.add("x86")
                ldFlags.addAll([
                        "-L${file("./obj/local/x86")}".toString()
                ])
            }
        }
    }
}

task buildMkRecast(type: Exec) {
    // Retrieve ndk dir
    Properties properties = new Properties()
    properties.load(project.rootProject.file('local.properties').newDataInputStream())
    def ndkDir = properties.getProperty('ndk.dir', null)

    // Call ndk build
    def ndkBuildExt = Os.isFamily(Os.FAMILY_WINDOWS) ? ".cmd" : ""
    commandLine "$ndkDir/ndk-build${ndkBuildExt}",
            '-C', file('.').absolutePath,
            'NDK_APPLICATION_MK=Application.mk',
            'APP_BUILD_SCRIPT=../../../../external/recast/Android.mk',
            'NDK_PROJECT_PATH=../../../../external/recast/'
}

task buildMkBullet(dependsOn: "buildMkRecast",type: Exec) {
    // Retrieve ndk dir
    Properties properties = new Properties()
    properties.load(project.rootProject.file('local.properties').newDataInputStream())
    def ndkDir = properties.getProperty('ndk.dir', null)

    // Call ndk build
    def ndkBuildExt = Os.isFamily(Os.FAMILY_WINDOWS) ? ".cmd" : ""
    commandLine "$ndkDir/ndk-build${ndkBuildExt}",
            '-C', file('.').absolutePath,
            'NDK_APPLICATION_MK=Application.mk',
            'APP_BUILD_SCRIPT=../../../../external/bullet/Android.mk',
            'NDK_PROJECT_PATH=../../../../external/bullet/'
}

tasks.whenTaskAdded { task ->
    def taskName = task.name

    if (taskName.startsWith("compile")) {
        task.dependsOn "buildMkBullet"
    }
}

dependencies {
    compile files('../java/libs/android-async-http-1.4.8.jar')
}

答案 1 :(得分:0)

所以我最终通过添加

使其工作
exportedHeaders {
                    srcDir "src"
                }

行并将其指向缺少标题的右侧目录。