简体   繁体   English

在Android Studio中使用OpenCV Native Code

[英]Using OpenCV Native Code in Android Studio

I created an OpenCV project in C++ and would like to run it on an Android device. 我用C ++创建了一个OpenCV项目,并希望在Android设备上运行它。 I have tried to set up my android project to run native code with no success. 我试图设置我的android项目来运行本机代码但没有成功。 I am currently facing this error: 我目前正面临这个错误:

Error:(23,0) Gradle DSL method not found: 'compile()' 错误:(23,0)找不到Gradle DSL方法:'compile()'

I used the following github sample for native android development with opencv as a foundation: https://github.com/jlhonora/opencv-android-sample 我使用以下github示例进行本机android开发,以opencv为基础: https//github.com/jlhonora/opencv-android-sample

So far I have done the following to try and get gradle to synce properly: 到目前为止,我已经完成了以下操作以尝试正确地进行同步:

updated project properties to look like this: 更新的项目属性如下所示:

ndk.dir=C\:\\Android\\sdk\\ndk-bundle
sdk.dir=C\:\\Android\\sdk
opencv.dir=C\:\\Android\\OpenCV-android-sdk

Imported {OpenCV_DIR}\\sdk\\java as a module and added it as a module dependency in the project structure settings 导入{OpenCV_DIR} \\ sdk \\ java作为模块并将其作为模块依赖项添加到项目结构设置中

This is the opencv-sample-master\\build.gradle: 这是opencv-sample-master \\ build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle-experimental:0.7.0-beta1'

    }
}
allprojects {
    repositories {
        jcenter()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

dependencies {
    compile project(':OpenCVLib')
}

Here is the project's build.gradle: 这是项目的build.gradle:

apply plugin: 'com.android.model.application'

model {
android {
    compileSdkVersion = 23
    buildToolsVersion = "23.0.2"

    defaultConfig.with {
        applicationId = "org.honorato.opencvsample"
        minSdkVersion.apiLevel = 15
        targetSdkVersion.apiLevel = 23
        versionCode = 1
        versionName = "1.0"
    }

    ndk {
        moduleName = "native"
        cppFlags.add("-I${file(getOpenCVDir())}".toString())
        cppFlags.add("-frtti")
        cppFlags.add("-fexceptions")
        ldLibs.addAll(["log", "opencv_java3"])
        stl    = "gnustl_static"
    }
}

android.buildTypes {
    release {
        // minifyEnabled = false
        // proguardFiles = getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

android.productFlavors {
    // for detailed abiFilter descriptions, refer to "Supported ABIs" @
    // https://developer.android.com/ndk/guides/abis.html#sa
    create("arm") {
        ndk.abiFilters.add("armeabi")
        ndk.ldFlags.add("-L${file('src/main/jniLibs/armeabi')}".toString())
    }
    create("arm7") {
        ndk.abiFilters.add("armeabi-v7a")
        ndk.ldFlags.add("-L${file('src/main/jniLibs/armeabi-v7a')}".toString())
    }
    create("arm8") {
        ndk.abiFilters.add("arm64-v8a")
        ndk.ldFlags.add("-L${file('src/main/jniLibs/arm64-v8a')}".toString())
    }
  }
}

def getOpenCVDir() {
   Properties properties = new Properties()
   properties.load(new File(rootDir.absolutePath +     "/local.properties").newDataInputStream())
   def externalModuleDir = properties.getProperty('opencv.dir', null)
   if (externalModuleDir == null) {
       throw new GradleException(
               "OpenCV location not found. Define location with opencv.dir in the local.properties file!")
   }
   return externalModuleDir
 }

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

And finally, the opencv library's build.gradle: 最后,opencv库的build.gradle:

apply plugin: 'com.android.library'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':{DIR}:workspace:appcompat_v7')
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }    

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

If anyone can help me with this issue and how to get started with native android development with opencv, I would highly appreciate it! 如果有人可以帮我解决这个问题,以及如何开始使用opencv进行本机android开发,我将非常感谢!

Your problem might come from your depenciencies flag: 您的问题可能来自您的依赖关系标志:

 compile project(':openCVLibrary300')

As you defined your project with gradle.0.7.0 alpha and probably your opencv gradle files are compiled from gradle stable release 2.8 or 2.10. 当您使用gradle.0.7.0 alpha定义项目时,可能您的opencv gradle文件是从gradle stable release 2.8或2.10编译的。

gradle 0.7.0 gradle 0.7.0

apply plugin: 'com.android.model.application'

gradle 2.10 gradle 2.10

apply plugin: 'com.android.library'

So probably, your opencv gradle file is not correctly defined. 可能,您的opencv gradle文件未正确定义。

So you have two options. 所以你有两个选择。

If you are going to debug your app in native mode, change your opencv gradle files to 0.7.0 or change your code to gradle 2.10. 如果要以纯模式调试应用程序,请将opencv gradle文件更改为0.7.0或将代码更改为gradle 2.10。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM