Suresh S
Suresh S

Reputation: 49

Gradle DSL method not found: 'compile()' while using tesseract library

I have tried to import tesseract ocr project in android studio. While building gradle it showin error as following

Error:(8, 0) Gradle DSL method not found: 'compile()' Possible causes:

  • The project 'textfairy' may be using a version of Gradle that does not contain the method. Open Gradle wrapper file
  • The build file may be missing a Gradle plugin. Apply Gradle plugin
  • My app/build.gradle file

        import java.util.regex.Pattern
    
        apply plugin: 'com.android.application'
        apply plugin: 'io.fabric'
    
    
    
        import org.apache.tools.ant.taskdefs.condition.Os
    
    
        task ndkBuild(type: Exec,description: 'run ndk-build') {
        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            workingDir 'src/main/jni'
            commandLine 'ndk-build.cmd', '-j',     Runtime.runtime.availableProcessors()
    
        } else {
            workingDir 'src/main/jni'
            commandLine "$ndkDir/ndk-build", '-j',  Runtime.runtime.availableProcessors()
        }
    }
    
    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn(ndkBuild)
    }
    
    
        def getVersionCodeFromManifest(String prefix) {
        def manifestFile = file("src/main/AndroidManifest.xml")
        def pattern = Pattern.compile("versionCode=\"(\\d+)\"")
        def manifestText = manifestFile.getText()
        def matcher = pattern.matcher(manifestText)
        matcher.find()
        def version = prefix + matcher.group(1)
        println sprintf("Returning version %s", version)
        return Integer.parseInt("$version")
    }
    
    
    
    
        android {
        compileSdkVersion 21
        buildToolsVersion "21.1.2"
    
        defaultConfig {
            testInstrumentationRunner "android.test.InstrumentationTestRunner"
            //testHandleProfiling true
            //testFunctionalTest true
        }
        signingConfigs {
            release
        }
        buildTypes {
            debug {
                debuggable true
            }
            release {
                //runProguard true
                proguardFile file('android.pro')
                proguardFile getDefaultProguardFile('proguard-android.txt')
                signingConfig signingConfigs.release
            }
        }
    
        productFlavors {
            x86 {
                versionCode getVersionCodeFromManifest("6")
                ndk {
                    abiFilter "x86"
                }
    
            }
            aV7 {
                versionCode getVersionCodeFromManifest("2")
                ndk {
                    abiFilter "armeabi-v7a"
                }
            }
            aV5 {
                versionCode getVersionCodeFromManifest("1")
                ndk {
                    abiFilter "armeabi"
                }
            }
        }
        sourceSets {
            main {
                //jniLibs.srcDirs = ['native-libs']
                jniLibs.srcDir 'src/main/libs'
                jni.srcDirs = [] //disable automatic ndk-build
            }
        }
    }
    
        def Properties props = new Properties()
        def propFile = new File('signing.properties')
        if (propFile.canRead()) {
        props.load(new FileInputStream(propFile))
    
        if (props != null && props.containsKey('STORE_FILE') &&    props.containsKey('STORE_PASSWORD') &&
                props.containsKey('KEY_ALIAS') && props.containsKey('KEY_PASSWORD')) {
            android.signingConfigs.release.storeFile = file(props['STORE_FILE'])
            android.signingConfigs.release.storePassword = props['STORE_PASSWORD']
            android.signingConfigs.release.keyAlias = props['KEY_ALIAS']
            android.signingConfigs.release.keyPassword = props['KEY_PASSWORD']
        } else {
            println 'signing.properties found but some entries are missing'
            android.buildTypes.release.signingConfig = null
        }
    } else {
        println 'signing.properties not found'
        android.buildTypes.release.signingConfig = null
    }
    
        dependencies {
        debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
        releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
        compile fileTree(dir: 'src/main/libs', include: '*.jar')
        compile 'com.viewpagerindicator:library:2.4.1@aar'
        compile 'com.github.chrisbanes.photoview:library:1.2.2'
        compile 'com.google.code.findbugs:jsr305:2.0.2'
        compile "com.google.guava:guava:18.0"
        compile 'de.greenrobot:eventbus:2.4.0'
        //compile 'com.android.support:design:22.2.0'
        compile 'com.nineoldandroids:library:2.4.0'
        compile 'com.android.support:appcompat-v7:19.0.+'
        compile 'org.apache.commons:commons-compress:1.5'
        compile project(":tess-two:tess-two")
        compile 'com.android.support:cardview-v7:21.0.+'
    
        androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.0'
        androidTestCompile 'com.google.dexmaker:dexmaker:1.0'
        androidTestCompile 'org.mockito:mockito-core:1.10.17'
        androidTestCompile 'junit:junit:4.12'
    
    
        testCompile 'junit:junit:4.12'
        testCompile "org.mockito:mockito-all:1.10.19"
        testCompile("org.robolectric:robolectric:3.0-rc2") {
            exclude group: 'commons-logging', module: 'commons-logging'
        }
        compile('com.crashlytics.sdk.android:crashlytics:2.4.0@aar') {
            transitive = true;
        }
    
    
    }
    

    My project root build.gradle file

        // Top-level build file where you can add configuration options common to all sub-projects/modules.
    buildscript {
        repositories {
            maven { url 'https://maven.fabric.io/public' }
            jcenter()
        }
        dependencies {
            compile fileTree(dir: 'libs', include: ['*.jar'])
        }
    }
    allprojects {
        repositories {
            maven { url 'https://maven.fabric.io/public' }
            maven { url "http://dl.bintray.com/populov/maven" }
            jcenter()
            maven {
                url "http://oss.sonatype.org/content/repositories/snapshots"
            }
    
        }
    
    }
    
    dependencies {
        apply plugin: 'osgi'
        compile project(':libraries:tess-two')
    }
    

    My settings.gradle file

        include ':libraries:tess-two'
    include ':app'
    

    My gradle-wrapper.properties

    distributionBase=GRADLE_USER_HOME
    distributionPath=wrapper/dists
    zipStoreBase=GRADLE_USER_HOME
    zipStorePath=wrapper/dists
    distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
    

    I have added dependencies also. Please tell me what is the problem. Thank you

    Upvotes: 1

    Views: 1504

    Answers (1)

    Angela
    Angela

    Reputation: 316

    You have to change your top-level build.gradle, removing the compile line, and adding the gradle and fabric plugin.

    buildscript {
        repositories {
            maven { url 'https://maven.fabric.io/public' }
            jcenter()
        }
       dependencies {
            classpath 'com.android.tools.build:gradle:1.3.1'
            classpath 'io.fabric.tools:gradle:1.+'
        }
    }
    
    
    allprojects {
        repositories {
            maven { url 'https://maven.fabric.io/public' }
            maven { url "http://dl.bintray.com/populov/maven" }
            jcenter()
            maven {
                url "http://oss.sonatype.org/content/repositories/snapshots"
            }
    
        }
    
    }
    

    Als if you want to use the libraries:tess-two in your project, you have to add this line inside the dependecies block of your app/build.gradle file

    compile project(':libraries:tess-two')
    

    Finally move the apply plugin: 'osgi' at the beginning of the app/build.gradle file

    To resolve the ndk issue you have to:

    1. add gradle.properties file to root folder of your project
    2. add 'android.useDeprecatedNdk=true' to gradle.properties file

    Here is my gradle.properties :

    android.useDeprecatedNdk=true
    

    Upvotes: 2

    Related Questions