当 minifyenabled 为 true 时,Tesseract 错误 java.lang.NoSuchFieldError

Posted

技术标签:

【中文标题】当 minifyenabled 为 true 时,Tesseract 错误 java.lang.NoSuchFieldError【英文标题】:Tes-s-ract error java.lang.NoSuchFieldError when minifyenable is true 【发布时间】:2016-03-16 19:53:57 【问题描述】:

当我使用 minifyenable true 构建我的应用程序时,我收到以下错误。奇怪的是,当我使用 minifyenable false 编译时,应用程序运行良好。这些应用程序使用 Tesseract 来扫描文档。

我错过了什么?

    java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:304)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NoSuchFieldError: no "J" field "mNativeData" in class "Lcom/googlecode/tesseract/android/TessBaseAPI;" or its superclasses
    at com.googlecode.tesseract.android.TessBaseAPI.nativeClassInit(Native Method)
    at com.googlecode.tesseract.android.TessBaseAPI.<clinit>(Unknown Source)
    at br.com.cfb.doarnf.el.a(Unknown Source)
    at br.com.cfb.doarnf.a.a(Unknown Source)
    at br.com.cfb.doarnf.a.doInBackground(Unknown Source)
    at android.os.AsyncTask$2.call(AsyncTask.java:292)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)

build.gradle(模块:app)

apply plugin: 'com.android.application'

android 
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig 
        applicationId "br.com.cfb.doarnf"
        minSdkVersion 17
        targetSdkVersion 23
        versionCode 5
        versionName "2.1.0"
        multiDexEnabled true
    
    buildTypes 
        release 
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        
        debug 
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        
    

    dexOptions 
        javaMaxHeapSize "4g"
    



dependencies 
    compile files('libs/YouTubeAndroidPlayerApi.jar')
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile project(':tess-two')
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.google.code.gson:gson:2.3.1'
    compile 'org.parceler:parceler:1.0.1'
    compile 'org.parceler:parceler-api:1.0.1'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
    compile 'com.squareup.okhttp:okhttp:2.2.0'
    compile 'com.github.orangegangsters:swipy:1.2.0@aar'
    compile 'com.google.android.gms:play-services:8.3.0'
    compile 'com.google.android.gms:play-services-gcm:8.3.0'
    compile 'com.google.android.gms:play-services-ads:8.3.0'
    compile 'com.google.android.gms:play-services-identity:8.3.0'
    compile 'com.google.zxing:core:3.2.1'

build.gradle(模块:tess-two)

buildscript 
    repositories 
        mavenCentral()
    
    dependencies 
        classpath 'com.android.tools.build:gradle:1.3.0'
    

apply plugin: 'com.android.library'
android 

    compileSdkVersion 23
    buildToolsVersion "23.0.1"


    defaultConfig 
        minSdkVersion 17
        targetSdkVersion 23
    
    buildTypes 
        release 
            minifyEnabled false
        
    

    sourceSets.main 
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        resources.srcDirs = ['src']
        res.srcDirs = ['res']
        jniLibs.srcDirs = ['libs']
    

dependencies 

【问题讨论】:

【参考方案1】:

我找到了解决方法。只是避免 ProGuard 处理 tess-two 库,如下:

将 consumerProguardFiles 添加到 build.gradle(模块:tess-two)

buildscript 
    repositories 
        mavenCentral()
    
    dependencies 
        classpath 'com.android.tools.build:gradle:1.3.0'
    

apply plugin: 'com.android.library'
android 

    compileSdkVersion 23
    buildToolsVersion "23.0.1"


    defaultConfig 
        minSdkVersion 17
        targetSdkVersion 23
        consumerProguardFiles 'proguard-tess-two-rules.txt'
    


    sourceSets.main 
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        resources.srcDirs = ['src']
        res.srcDirs = ['res']
        jniLibs.srcDirs = ['libs']
    

dependencies 

proguard-tess-two-rules.txt 文件

# Add tess-two specific ProGuard rules here.
-dontoptimize
-dontobfuscate
-dontpreverify
-dontshrink
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontusemixedcaseclassnames
-dontnote

我希望它可以帮助某人。

【讨论】:

谢谢,您为我指明了正确的方向。不过,我们只需要 3 条规则。 -dontoptimize,-dontpreverify,-dontshrink

以上是关于当 minifyenabled 为 true 时,Tesseract 错误 java.lang.NoSuchFieldError的主要内容,如果未能解决你的问题,请参考以下文章

当 minifyenabled 为 true 时,Tesseract 错误 java.lang.NoSuchFieldError

当我设置 minifyEnabled = true 时,应用程序无法使用 API

Android - 当 minifyEnabled true 导致 java.lang.ExceptionInInitializerError 时,带有混淆 AAR 库的应用程序崩溃

设置 minifyEnabled 为 true 时无法使用 GSON 解析 json 对象

如何解决此问题“在 app gradle 中 minifyEnabled 为 true 时未生成签名的 apk”

使用 Picasso 和 minifyEnabled 时应用程序崩溃 true