2016-09-21 69 views
3

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

我已经看过与./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 
    } 
} 

注意,路径是正确的,因为我可以强制副本做通过task 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<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') 
} 

其他信息:

  • cmake的版本:3.6.3155560
  • 如果共享的对象是在jniLibs文件夹中的文件AAR正确创建的,所以构建实际上除了工作为复制步骤。

回答

2

我想出了如何做到底。要包含来自某个地方的构建共享对象,我们需要在我们的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' 
     } 
    } 
}