Android Studio - java.lang.NoClassDefFoundError

2019-08-14 02:45发布

问题:

I added the .jar file to the lib folder in the project directory:

Next I right clicked on each .jar file and selected "Add as library".

Then I added the dependencies to the build.gradle file:

apply plugin: 'com.android.application'

    android {
        compileSdkVersion 21
        buildToolsVersion "21.1.2"

        defaultConfig {
            applicationId "com.ryzomarmory.crm.ryzomarmory"
            minSdkVersion 16
            targetSdkVersion 21
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }

    dependencies {
        compile fileTree(dir: 'lib', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:21.0.3'
        compile files('commons-dbcp-1.4.jar')
        compile files('commons-pool-1.6.jar')
        compile files('mysql-connector-java-5.1.30-bin.jar')
    }

When I attempt to run the android application it displays the java.lang.NoClassDefFoundError. Here is the full log:

02-26 11:13:54.671  20960-20960/com.ryzomarmory.crm.ryzomarmory E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.ryzomarmory.crm.ryzomarmory, PID: 20960
    java.lang.NoClassDefFoundError: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
            at com.ryzomarmory.crm.ryzomarmory.DataSourceFactory.getMySQLDataSource(DataSourceFactory.java:19)
            at com.ryzomarmory.crm.ryzomarmory.LoginService.<init>(LoginService.java:13)
            at com.ryzomarmory.crm.ryzomarmory.LoginActivity.<init>(LoginActivity.java:18)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.Class.newInstance(Class.java:1572)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

I ran the command gradlew.bat clean in the app directory, as suggested in most similar questions. But this doesn't solve the issue for me.

edit: The code in this try block is causing the error:

import javax.sql.DataSource;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

// establishes a persistent database connection
public class DataSourceFactory {

    public static DataSource getMySQLDataSource() {
        MysqlDataSource mysqlDS         = null;
        final Configuration conf        = new Configuration();
        final String dbHostName         = conf.getDbHostname();
        final String dbDatabaseName     = conf.getDbDatabaseName();
        final String dbUsername         = conf.getDbUsername();
        final String dbPassword         = conf.getDbPassword();

        try {
            mysqlDS = new MysqlDataSource();
            mysqlDS.setURL(dbHostName + dbDatabaseName);
            mysqlDS.setUser(dbUsername);
            mysqlDS.setPassword(dbPassword);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
        return mysqlDS;
    }
}

回答1:

Having your android app connect directly to a DB over JDBC is not advised, see this post by Mark Murphy for reasons why. It is technically possible, but you have to hack around, it's not worth it, you should invest that effort into doing it properly, by interfacing with a web service.

Ideally you'll want to wrap up your database calls in a web service, have that sitting on an application server somewhere and have your app interact via REST to fetch and update data.

A benefit of introducing a REST layer is that you could also have a web front end in addition to a mobile app, or iOS etc..



回答2:

Go to "libs" folder of your project, select and right click on your library.jar, Select "Add As Library...". Select OK. Clear the project and run again.

Hope this helps.