项目作者: bytedeco

项目描述 :
Gradle plugins to automate the build process of JavaCPP and JavaCV
高级语言: Java
项目地址: git://github.com/bytedeco/gradle-javacpp.git
创建时间: 2020-05-20T07:40:27Z
项目社区:https://github.com/bytedeco/gradle-javacpp

开源协议:Other

下载


Gradle JavaCPP

Gitter Maven Central Sonatype Nexus (Snapshots) Build Status Commercial support: xscode

Introduction

Gradle JavaCPP offers plugins that make it easy to use JavaCPP and JavaCV as part of the Gradle build system.

Please feel free to ask questions on the mailing list if you encounter any problems with the software! I am sure it is far from perfect…

Required Software

To use Gradle JavaCPP, you will need to download and install the following software:

Getting Started

Gradle JavaCPP comes with 2 plugins:

  • The build plugin to create new packages containing native libraries using JavaCPP, and
  • The platform plugin to select from existing artifacts the ones corresponding to user-specified platforms.

Fully functional sample projects are also provided in the samples subdirectory and can be used as templates.

The Build Plugin

To understand how JavaCPP is meant to be used, one should first take a look at the Mapping Recipes for C/C++ Libraries, but a high-level overview of the Basic Architecture is also available to understand the bigger picture.

Once comfortable enough with the command line interface, the build plugin for Gradle can be used to integrate easily that workflow as part of build.gradle as shown below. By default, for Java libraries and applications, it creates a javacppJar task that archives the native libraries into a separate JAR file and sets its classifier to $javacppPlatform$javacppPlatformExtension, while excluding those files from the default jar task. To customize the behavior, there are properties that can be modified and whose documentation is available as part of the source code in these files:

  1. plugins {
  2. id 'java-library'
  3. id 'org.bytedeco.gradle-javacpp-build' version "$javacppVersion"
  4. }
  5. // We can set this on the command line too this way: -PjavacppPlatform=android-arm64
  6. ext {
  7. javacppPlatform = 'android-arm64' // or any other platform, defaults to Loader.getPlatform()
  8. }
  9. dependencies {
  10. api "org.bytedeco:javacpp:$javacppVersion"
  11. }
  12. tasks.withType(org.bytedeco.gradle.javacpp.BuildTask) {
  13. // set here default values for all build tasks below, typically just includePath and linkPath,
  14. // but also properties to set the path to the NDK and its compiler in the case of Android
  15. }
  16. javacppBuildCommand {
  17. // typically set here the buildCommand to the script that fills up includePath and linkPath
  18. }
  19. javacppBuildParser {
  20. // typically set here the classOrPackageNames to class names implementing InfoMap
  21. }
  22. javacppBuildCompiler {
  23. // typically set here boolean flags like copyLibs
  24. }

Integration with Android Studio

It is also possible to integrate the BuildTask with Android Studio for projects with C/C++ support by:

  1. Following the instructions at https://developer.android.com/studio/projects/add-native-code ,
  2. Adding something like below to the app/build.gradle file, and

    1. android.applicationVariants.all { variant ->
    2. def variantName = variant.name.capitalize() // either "Debug" or "Release"
    3. def javaCompile = project.tasks.getByName("compile${variantName}JavaWithJavac")
    4. def configureCMake = project.tasks.findAll {
    5. it.name.startsWith("configureCMake$variantName")
    6. }
    7. // Compiles NativeLibraryConfig.java
    8. task "javacppCompileJava$variantName"(type: JavaCompile) {
    9. include 'com/example/myapplication/NativeLibraryConfig.java'
    10. source = javaCompile.source
    11. classpath = javaCompile.classpath
    12. destinationDir = javaCompile.destinationDir
    13. }
    14. // Parses NativeLibrary.h and outputs NativeLibrary.java
    15. task "javacppBuildParser$variantName"(type: org.bytedeco.gradle.javacpp.BuildTask) {
    16. dependsOn "javacppCompileJava$variantName"
    17. classPath = [javaCompile.destinationDir]
    18. includePath = ["$projectDir/src/main/cpp/"]
    19. classOrPackageNames = ['com.example.myapplication.NativeLibraryConfig']
    20. outputDirectory = file("$projectDir/src/main/java/")
    21. }
    22. // Compiles NativeLibrary.java and everything else
    23. javaCompile.dependsOn "javacppBuildParser$variantName"
    24. // Generates jnijavacpp.cpp and jniNativeLibrary.cpp
    25. task "javacppBuildCompiler$variantName"(type: org.bytedeco.gradle.javacpp.BuildTask) {
    26. dependsOn javaCompile
    27. classPath = [javaCompile.destinationDir]
    28. classOrPackageNames = ['com.example.myapplication.NativeLibrary']
    29. compile = false
    30. deleteJniFiles = false
    31. outputDirectory = file("$projectDir/src/main/cpp/")
    32. }
    33. // Picks up the C++ files listed in CMakeLists.txt
    34. configureCMake.forEach {
    35. it.dependsOn "javacppBuildCompiler$variantName"
    36. }
    37. }
  3. Updating the CMakeLists.txt file to include the generated .cpp files.

The Platform Plugin

With Maven, we are able to modify dependencies transitively using profiles, and although Gradle doesn’t provide such functionality out of the box, it can be emulated via plugins. After adding a single line to the build.gradle script as shown below, the platform plugin will filter the dependencies of artifacts whose names contain “-platform” using the comma-separated values given in $javacppPlatform. To understand better how this works, it may be worth taking a look at the source code of the plugin:

  1. plugins {
  2. id 'java-library'
  3. id 'org.bytedeco.gradle-javacpp-platform' version "$javacppVersion"
  4. }
  5. // We can set this on the command line too this way: -PjavacppPlatform=linux-x86_64,macosx-x86_64,windows-x86_64,etc
  6. ext {
  7. javacppPlatform = 'linux-x86_64,macosx-x86_64,windows-x86_64,etc' // defaults to Loader.getPlatform()
  8. }
  9. dependencies {
  10. api "org.bytedeco:javacv-platform:$javacvVersion" // or any other "-platform" artifacts
  11. }

Moreover, in the case of Android, its plugin is not able to use native libraries found in JAR files when building Android App Bundles (AAB files). However, to work around this limitation we can easily use Gradle to extract the files automatically, for example, in the following manner with an additional javacppExtract task inside app/build.gradle:

  1. configurations {
  2. javacpp
  3. }
  4. task javacppExtract(type: Copy) {
  5. dependsOn configurations.javacpp
  6. from { configurations.javacpp.collect { zipTree(it) } }
  7. include "lib/**"
  8. into "$buildDir/javacpp/"
  9. android.sourceSets.main.jniLibs.srcDirs += ["$buildDir/javacpp/lib/"]
  10. tasks.getByName('preBuild').dependsOn javacppExtract
  11. }
  12. dependencies {
  13. implementation group: 'org.bytedeco', name: 'javacv', version: "$javacvVersion"
  14. javacpp group: 'org.bytedeco', name: 'openblas-platform', version: "$openblasVersion-$javacppVersion"
  15. javacpp group: 'org.bytedeco', name: 'opencv-platform', version: "$opencvVersion-$javacppVersion"
  16. javacpp group: 'org.bytedeco', name: 'ffmpeg-platform', version: "$ffmpegVersion-$javacppVersion"
  17. ...
  18. }

Project lead: Samuel Audet samuel.audet at gmail.com
Developer site: https://github.com/bytedeco/gradle-javacpp
Discussion group: http://groups.google.com/group/javacpp-project