在Android上使用cmake时将共享对象复制到jniLib

Krø*_*lle 3 c++ android cmake gradle

我最近开始将Android构建从Ant移到Gradle,然后我想对我的C ++代码使用cmake。该构建当前运行良好,但是没有共享对象被复制到jniLibs文件夹中,然后在创建aar文件之前将它们放置在该文件夹中(这是一个库项目,当前在Windows 10上构建)。

我已经研究了使用进行构建时正在运行的任务./gradlew assembleDebug。他们是:

:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preDebugAndroidTestBuild UP-TO-DATE
:app:preDebugUnitTestBuild UP-TO-DATE
:app:preReleaseBuild UP-TO-DATE
:app:preReleaseUnitTestBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72221Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42221Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileLint UP-TO-DATE
:app:copyDebugLint UP-TO-DATE
:app:copyLibs
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:incrementalDebugJavaCompilationSafeguard UP-TO-DATE
:app:compileDebugJavaWithJavac UP-TO-DATE
:app:extractDebugAnnotations UP-TO-DATE
:app:mergeDebugShaders UP-TO-DATE
:app:compileDebugShaders UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:mergeDebugProguardFiles UP-TO-DATE
:app:packageDebugRenderscript UP-TO-DATE
:app:packageDebugResources UP-TO-DATE
:app:processDebugJavaRes UP-TO-DATE
:app:transformResourcesWithMergeJavaResForDebug UP-TO-DATE
:app:transformClassesAndResourcesWithSyncLibJarsForDebug UP-TO-DATE
:app:generateJsonModelDebug UP-TO-DATE
:app:externalNativeBuildDebug
  building E:\path\to\app\Android\app\.externalNativeBuild\cmake\debug\libs\x86\libApp.so
  building 
:app:mergeDebugJniLibFolders
:app:transformNative_libsWithMergeJniLibsForDebug
:app:transformNative_libsWithSyncJniLibsForDebug
:app:bundleDebug
:app:compileDebugSources
:app:assembleDebug
Run Code Online (Sandbox Code Playgroud)

一切都很好,但是文件夹app/src/main/jniLibs为空,因此没有共享对象被复制到aar文件。因此,存在一个问题,即如何添加gradle构建步骤来实际复制这些文件。事实证明这很难做到。

方法1:

尝试创建要在:app:bundleDebug任务之前运行的复制任务。我做了一些尝试来做到这一点:

第一种可能性:

task copyLibs(type: Copy, dependsOn: 'bundleDebug') {
    from ('.externalNativeBuild/cmake/debug/libs') {
        include '**/libApp.so'
    }
    into 'src/main/jniLibs'
}
Run Code Online (Sandbox Code Playgroud)

第二种可能性:

task copyLibs(type: Copy) {
    from ('.externalNativeBuild/cmake/debug/libs') {
        include '**/libApp.so'
    }
    into 'src/main/jniLibs'
}

tasks.whenTaskAdded { task ->
    if (task.name ==  'bundleDebug') {
        task.dependsOn copyLibs
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,路径是正确的,因为我可以通过在task copyLibs <<内部使用一个复制步骤来强制完成复制。由于共享对象在第一次构建中不存在,因此它将在构建的第二次运行中被复制。(因为该任务在开始时运行。)

方法二:

在cmake中进行复制。这是可行的,但并不理想。因此,我没有走这条路。

这是我的build.gradle:

评论和更多信息如下。

apply plugin: 'com.android.library'

// This task is run before everything else, so it does the copy on the *second* build,
// thereby copying the shared objects from the *previous* build.
//task copyLibs << {
//    copy {
//        from ('.externalNativeBuild/cmake/debug/libs') {
//            include '**/libApp.so'
//        }
//        from ('.externalNativeBuild/cmake/release/libs') {
//            include '**/libApp.so'
//        }
//        into 'src/main/jniLibs'
//        includeEmptyDirs = false
//    }
//}

// This is the task signature if tasks.whenTaskAdded below is *not* used. If both are
// used we get a circular dependency.
//task copyLibs(type: Copy, dependsOn: 'bundleDebug') {

task copyLibs(type: Copy) {
    from ('.externalNativeBuild/cmake/debug/libs') {
        include '**/libApp.so'
    }
    into 'src/main/jniLibs'
}

tasks.whenTaskAdded { task ->
    if (task.name ==  'bundleDebug') {}
        // This dependecy *is* set. This can be seen by using task copyLibs(type: Copy, dependsOn: 'bundleDebug')
        // together with this and get a circular dependency.
        task.dependsOn copyLibs
    }
}

// Prints:
// [task ':app:assemble', task ':app:assembleAndroidTest', task ':app:assembleDefault', task ':app:buildDependents',
// task ':app:buildNeeded', task ':app:check', task ':app:compileLint', task ':app:connectedCheck', task ':app:copyLibs',
// task ':app:deviceCheck', task ':app:extractProguardFiles', task ':app:lint', task ':app:preBuild', task ':app:sourceSets',
// task ':app:uninstallAll']
println(tasks)

android {
    compileSdkVersion 22
//    buildToolsVersion "22.0.1"
    buildToolsVersion "24.0.2" // Tried using latest for good measures

    defaultConfig {
        minSdkVersion 12
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"

        // Some defines to build certain architectures, e.g. with './gradlew -Ponly-x86 assembleDebug'. This is working.
        if (project.hasProperty('only-x86')) {
            ndkConfig.abiFilters = ["x86"] as Set<String>
        }
        else if (project.hasProperty('only-armeabi-v7a')) {
            ndkConfig.abiFilters = ["armeabi-v7a"] as Set<String>
        }
        else {
            ndkConfig.abiFilters = ["x86", "armeabi-v7a"] as Set<String>
        }

        externalNativeBuild {
            cmake {
                cppFlags    "-fexceptions", "-frtti", "-Wno-error"
                arguments   "-DANDROID_STL=gnustl_static"
            }
        }
    }

    buildTypes {
        release {
        }
        debug {
        }
    }

    externalNativeBuild {
        cmake {
            path '../../../CMakeLists.txt'
        }
        beforeEvaluate( println("I ma printed at the top.") )
        afterEvaluate { println("I am printed before compiling.") }
    }
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.1'
}

task clean(type: Delete) {
    delete "${rootProject.buildDir}"
    delete "${project.buildDir}"
    delete "${project.projectDir}/.externalNativeBuild"
    delete fileTree(dir: "${project.projectDir}/src/main/jniLibs", include: '**/*.so')
}
Run Code Online (Sandbox Code Playgroud)

其他资讯:

  • cmake版本:3.6.3155560
  • 如果共享对象位于jniLibs文件夹中,则将正确创建aar文件,因此除复制步骤外,该构建实际上是可以运行的。

Krø*_*lle 5

我终于弄清楚了怎么做。要从某个地方包含构建的共享库,我们需要sourceSets.main.jniLibs.srcDirsandroidbuild.gradle的内部使用。下面的示例直接从Android cmake输出目录中获取共享库(用于调试)。

例如:

android {
    ...
    defaultConfig {
        ...
    }

    buildTypes {
        release {
        }
        debug {
        }
    }

    sourceSets {
        main {
            // Bundle so files with the final apk.
            // NOTE: Currently bundles all shared objects in that directory.
            // It was not straightforward to exclude in Android sourceSets at the time of writing,
            // see https://code.google.com/p/android/issues/detail?id=64957
            jniLibs.srcDirs = ['.externalNativeBuild/cmake/debug/libs']
        }
    }

    externalNativeBuild {
        cmake {
            path '../../../CMakeLists.txt'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)