我加入.jar
文件到lib
项目目录文件夹:
接下来我右点击每个.jar文件,并选择“添加为库”。
然后,我添加的依赖到的build.gradle文件:
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')
}
当我试图运行Android应用中显示java.lang.NoClassDefFoundError
。 下面是完整的日志:
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)
我跑的命令gradlew.bat clean
应用程序目录,因为在大多数类似的问题建议。 但是,这并没有解决我的问题。
编辑:在此代码try
块导致错误:
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;
}
}