android拷贝对象,使用cmake for Android时,将共享对象复制到jniLibs

我最近开始将我的Android构建从Ant移动到Gradle,然后我想使用cmake作为我的C代码.构建当前运行正常,但在创建aar文件之前,没有共享对象被复制到jniLibs文件夹(这是一个库项目,目前在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

这一切都很好,但文件夹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'

}

第二种可能性

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

}

}

请注意,路径是正确的,因为我可以通过让任务copyLibs<

方法2:

在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

}

else if (project.hasProperty('only-armeabi-v7a')) {

ndkConfig.abiFilters = ["armeabi-v7a"] as Set

}

else {

ndkConfig.abiFilters = ["x86", "armeabi-v7a"] as Set

}

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

}

其他信息:

> cmake版本:3.6.3155560

>如果共享对象位于jniLibs文件夹中,则会正确创建aar文件,因此除了复制步骤之外,构建实际上正在工作.

解决方法:

我想到了最后怎么做.要从某个地方包含构建的共享对象,我们需要在build.gradle的android部分内使用sourceSets.main.jniLibs.srcDirs.以下示例直接从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'

}

}

}

标签:android,c,gradle,cmake

来源: https://codeday.me/bug/20190828/1756050.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值