0

我有一个 gradle-experimental 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++ 文件,但无论我尝试什么,它每次都在第一次包含标题时失败。

我尝试使用以下方法添加源文件/标头包含,如下所述

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/

4

2 回答 2

3

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

然后我工作,我在我的 Mac OSX 上使用 cocos2Dx 3.9 完成了它。

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

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

西尔

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')
}
于 2016-02-14T18:04:09.667 回答
0

所以我终于通过添加

exportedHeaders {
                    srcDir "src"
                }

行并将其指向丢失标题所在的正确目录。

于 2015-12-10T17:15:01.283 回答