Giter Site home page Giter Site logo

msoftware / androidopencvgradleplugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ahasbini/androidopencvgradleplugin

0.0 1.0 0.0 118 KB

Gradle Plugin that will automate retrieving the Android OpenCV SDK and linking it to the project, making it easy to include OpenCV into Android applications.

License: MIT License

Groovy 5.60% Java 93.57% CMake 0.83%

androidopencvgradleplugin's Introduction

AndroidOpenCVGradlePlugin

Gradle Plugin that will automate retrieving the Android OpenCV SDK and linking it to the project, making it easy to include OpenCV into Android applications.

Usage

Currently the plugin is not yet published on public repositories. To use the plugin, it requires to be compiled and published locally on the machine for projects to be able to resolve and use it:

git clone https://github.com/ahasbini/AndroidOpenCVGradlePlugin.git
cd AndroidOpenCVGradlePlugin

# Either (on Windows):
gradlew.bat :plugin:publishToMavenLocal
# or (on *nix):
./gradlew :plugin:publishToMavenLocal

Once commands above complete to succession, the plugin is now located in the local Maven repository of the machine (.m2) under com\ahasbini\tools\android-opencv-gradle-plugin.

For usage in an Android Project, the below changes are needed:

  1. In the project build.gradle (at the root directory of the project folder), modify the repositories and dependencies block as below:
buildscript {
    repositories {
        mavenLocal() // At the beginning of the block
        // ... google() or jcenter() others
        maven { // At the end of the block and after google()
           url 'https://repo.gradle.org/gradle/libs-releases'
        }
    }
    dependencies {
        // ... the Android plugin and other classpath definitions
        classpath 'com.ahasbini.tools:android-opencv-gradle-plugin:0.0.+'
    }
}
  1. In the app module (or application/library module that you're developing) build.gradle file, add the android-opencv-gradle-plugin plugin and the androidOpenCV as below:
// apply plugin: 'com.android.application' or other Android plugin
apply plugin: 'com.ahasbini.android-opencv-gradle-plugin' // After the Android plugin

// ...

android {
    // ...
}

androidOpenCV { // After the android block

    // Required: Version of OpenCV to be used in the project
    version '3.3.0'

    // Optional: Custom url for downloading the
    // opencv-xxx-android-sdk.zip file located at
    // https://sourceforge.net/projects/opencvlibrary/files/opencv-android
    url 'https://sourceforge.net/projects/opencvlibrary/files/opencv-android/3.3.0/opencv-3.3.0-android-sdk.zip/download'
}

// ...
  1. Optional: If the project did not contain any C++ code (usually located in jni or cpp folders under {project_app_module}/src/main/), perform the below changes:

    • Add the below in app module (or application/library module that you're developing) build.gradle file:
    android {
        // ...
        externalNativeBuild {
            cmake {
                path "CMakeLists.txt"
            }
        }
    }
    • Create the CMakeLists.txt in app module (or application/library module that you're developing) directory and check the Android Guides for NDK or the sample for more info.
  2. Do a Gradle Sync , refresh linked C++ projects (Build > Refresh Linked C++ Projects) and compile to make sure the integration was successful.

Underlying Logic & Implementation

TL;DR The plugin downloads the opencv-xxx-android-sdk.zip, extracts the files, compiles the Java sources into AARs, and links them along with JNI binaries into the project using dependencies and externalNativeBuild configurations.

In detail, below are the steps it carries our (primarily in this order) which can be found mostly in AndroidOpenCVGradlePlugin.java:

  • Set the OpenCV JNI directory and arguments of the externalNativeBuild in the android block.
  • Extract the requested version of OpenCV from the androidOpenCV block.
  • Check if existing or download the opencv-xxx-android-sdk.zip into the directory {user_home}/.androidopencv/{version} using the url template "https://sourceforge.net/projects/opencvlibrary/files/" + version + "/opencv-" + version + "-android-sdk.zip" or the value of url in androidOpenCV block.
  • Check if existing or extract the downloaded zip file.
  • Check if existing or copy the JNI directories (path used in the first step) into {project_module_directory}/build/androidopencv.
  • Compile AAR binaries from Java source and place outputs (debug and release builds) in {user_home}/.androidopencv/{version}/build-cache using the Gradle Tooling API.
  • Add flatDir repository with {user_home}/.androidopencv/{version}/build-cache/outputs path and add dependencies debugImplementation and releaseImplementation with the AARs to project dependencies.

Contributing & Future Plans

As this is still under development, testing and not yet published, feel free to share your contributions to the project with finding issues, code improvements and/or feature additions and requests. Below is a brief list of things (TODOs) that are in plan for the project:

  • Add Android NDK version checking and configuration for proper linking with compiled binaries of OpenCV
  • Add plugin clean task
  • Add more tests and assertions in test cases
  • CI/CD integration

androidopencvgradleplugin's People

Contributors

ahasbini avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.