且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android的工作室 - java.lang.NoClassDefFoundError的

更新时间:2023-09-19 23:19:40

而你的Andr​​oid应用程序直接连接到一个数据库JDBC上不建议,请参阅this对于为何发布通过马克·墨菲。这在技术上是可行的,但你要砍左右,这是不值得的,你应该投资的精力投入到正确的做这件事,通过Web服务接口。

在理想情况下,你会想包你的数据库调用在Web服务中,有一个坐的应用程序服务器上的某个地方,有你的应用程序通过REST交互来获取和更新数据。

引入REST层的一个好处是,你也可以在除了移动应用或iOS等。

一个Web前端

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

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..