【问题标题】:Gradle DSL method not found: 'compile()' while using tesseract library未找到 Gradle DSL 方法:使用 tesseract 库时出现“compile()”
【发布时间】:2015-09-26 06:33:12
【问题描述】:

我尝试在 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;
        }
    
    
    }
    

    我的项目根 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')
    }
    

    我的 settings.gradle 文件

        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
    

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

    【问题讨论】:

      标签: android android-studio android-gradle-plugin


      【解决方案1】:

      你必须改变你的顶级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 文件的dependecies 块中添加这一行

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

      最后将apply plugin: 'osgi'移到app/build.gradle文件的开头

      要解决 ndk 问题,您必须:

      1. 将 gradle.properties 文件添加到项目的根文件夹
      2. 将“android.useDeprecatedNdk=true”添加到 gradle.properties 文件中

      这是我的 gradle.properties :

      android.useDeprecatedNdk=true
      

      【讨论】:

      • 嗨安吉拉,我也试过了。但同样的错误显示。 :(
      • 我在我的问题中添加了 gradle-wrapper.properties 文件,因为是否存在任何问题。在打开的 gradle-wrapper.properties 文件中查看我的错误,如下所示
      • 正确。您也可以使用 2.4。确保从顶层 build.gradle 中删除 compile 语句
      • 在顶级 build.gradle 文件中删除编译行后。现在显示 Error:(68, 0) Error: NDK integration is deprecated in the current plugin。考虑尝试新的实验性插件。详情请见tools.android.com/tech-docs/new-build-system/…。在 gradle.properties 中设置“android.useDeprecatedNdk=true”以继续使用当前的 NDK 集成。
      • 是的,我添加了那行。现在显示以前的错误 dsl 方法未找到。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 2015-02-21
      • 2015-03-24
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多