Android 4.3:java.lang.NoClassDefFoundError:org / apache / commons / io / Charsets

时间:2018-12-20 08:20:49

标签: android apache-commons

Android 3.2,Gradle 4.6,Java 1.8

dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'

在app / build.gradle上

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.myproject"
        minSdkVersion 18
        targetSdkVersion 28
        versionCode 423
        versionName "2.1.423"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

   lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }

def AAVersion = '4.5.2'

dependencies {
    annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
    annotationProcessor "org.androidannotations:ormlite:$AAVersion"

    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:animated-vector-drawable:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.android.support:customtabs:28.0.0'
    implementation 'com.android.support:exifinterface:28.0.0'
    implementation 'com.android.support:support-media-compat:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'

    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.7'
    implementation 'com.google.android.gms:play-services-gcm:16.0.0'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.j256.ormlite:ormlite-android:5.1'
    implementation 'commons-codec:commons-codec:1.11'
    implementation 'commons-io:commons-io:2.6'
    implementation 'org.apache.commons:commons-lang3:3.8.1'
    implementation 'org.apache.httpcomponents:httpclient:4.3.6'
    implementation "org.androidannotations:androidannotations-api:$AAVersion"
    implementation "org.androidannotations:ormlite-api:$AAVersion"

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
}

以下是Java类代码段:

import android.content.Context;
import android.util.Log;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

private String getFileContent(String fileName) {
    try {
        File file = new File(fileName);
        String fileContent = FileUtils.readFileToString(file); // error here
        return fileContent;
    } catch (IOException e) {
        if(BuildConfig.DEBUG)Log.e(TAG, e.getMessage(), e);
    }
        return null;
    }

Android 4.4 + 项目上成功运行。

但是在 Android 4.3 上我收到错误消息:

org/apache/commons/io/Charsets                                             
java.lang.NoClassDefFoundError: org/apache/commons/io/Charsets                             
    at org.apache.commons.io.IOUtils.copy(IOUtils.java:2440)                           
    at org.apache.commons.io.IOUtils.toString(IOUtils.java:1084)                           
    at org.apache.commons.io.IOUtils.toString(IOUtils.java:1064)                           
    at com.myproject.android.fointeraction.util.StringUtil.getStringFromResource(StringUtil.java:82)           
    at com.myproject.transport.lite.FOServiceLite.executeCommand(FOServiceLite.java:669)               
    at com.myproject.service.DeviceContextService.registerDeviceContext(DeviceContextService.java:112)     
    at com.myproject.service.DeviceContextService.update(DeviceContextService.java:149)            
    at com.myproject.service.DeviceContextService.getDeviceId(DeviceContextService.java:160)           
    at com.myproject.service.AppContextService.getUserName(AppContextService.java:16)              
    at com.myproject.service.NotificationsSyncModule.sync(NotificationsSyncModule.java:118)        
    at com.myproject.activity.FirstStartActivity$3.run(FirstStartActivity.java:135)            
at java.lang.Thread.run(Thread.java:841)

此行中的错误:

String fileContent = FileUtils.readFileToString(file);

1 个答案:

答案 0 :(得分:0)

不太可能图书馆不知道不同API级别的类

...启用multidex,将所有库打包为Dalvik

dependencies的数量显然指向这个方向。


错误实际上来自那里:

StringUtil.getStringFromResource(StringUtil.java:82)

另一种可能是Charsets被混淆。

你能import org.apache.commons.io.Charsets;吗?