找不到Gradle DSL方法:' compile()'使用tesseract库时

时间:2015-09-26 06:33:12

标签: android android-studio android-gradle

我试图在android studio中导入tesseract ocr项目。在构建gradle时,它会显示错误,如下所示

  

错误:(8,0)未找到Gradle DSL方法:' compile()'   可能的原因:

  • 项目' textfairy'可能正在使用不包含该方法的Gradle版本。   打开Gradle包装器文件
  • 构建文件可能缺少Gradle插件。   申请Gradle插件
  • 我的app / build.gradle文件

        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;
        }
    
    
    }
    

    我的项目root build.gradle文件

        // 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')
    }
    

    我的设置。草稿文件

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

    我的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
    

    我也添加了依赖项。请告诉我是什么问题。 谢谢

    1 个答案:

    答案 0 :(得分:2)

    您必须更改顶级build.gradle,删除编译行,然后添加gradle和fabric插件。

    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"
            }
    
        }
    
    }
    

    如果您想在项目中使用库:tess-two,则必须在dependecies文件的app/build.gradle块中添加此行

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

    最后在apply plugin: 'osgi'文件

    的开头移动app/build.gradle

    要解决ndk问题,您必须:

    1. 将gradle.properties文件添加到项目的根文件夹
    2. 添加' android.useDeprecatedNdk = true'到gradle.properties文件
    3. 这是我的gradle.properties:

      android.useDeprecatedNdk=true