我正在将eclipse的android项目迁移到android studio。在项目中,我使用了JTDS库来连接数据库。问题是它可以与调试Apk一起正常工作,但是当我创建发行Apk时,出现以下错误。
Caused by: java.lang.NoClassDefFoundError: net.sourceforge.jtds.jdbc.BlobImpl
因此,经过一些RND,我猜发行版Apk中存在一个问题,该问题无法在Apk中包含jtds jar文件。请给我建议。
下面是一些代码:
Build.gradle
apply plugin: 'com.android.application'
android {
signingConfigs {
releaseConfig {
storeFile file('F:\\codebase\\android')
storePassword var3
keyAlias = var4
keyPassword var5
}
}
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
applicationId "com.app"
minSdkVersion 16
targetSdkVersion 28
}
buildTypes {
release {
debuggable true
minifyEnabled true
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.releaseConfig
}
}
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
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:support-v4:28.0.0'
implementation files('libs/sun.misc.BASE64Decoder.jar')
implementation files('libs\\jtds-1.2.5.jar')
android {
useLibrary 'org.apache.http.legacy'
}
}
此功能在Apk版本中不起作用
public static Statement connectionOpen() {
try {
// Establish the connection.
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
con = DriverManager.getConnection(connectionUrl);
stmt = con.createStatement();
} catch (Exception e) {
exceptionData.setExceptionData(e.getMessage(), "connectionOpen");
e.printStackTrace();
}
return stmt;
}
Logcat错误
2019-12-24 15:11:45.524 7770-7890/com.dm.rcl_app E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.m.app, PID: 7770
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$4.done(AsyncTask.java:399)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
Caused by: java.lang.NoClassDefFoundError: net.sourceforge.jtds.jdbc.BlobImpl
at c.a.a.a.l0.a(:75)
at c.a.a.a.l0.<clinit>(:87)
at c.a.a.a.l0.a(:117)
at c.a.a.a.h.a(:1500)
at c.a.a.a.n0.M(:3219)
at c.a.a.a.n0.w(:2282)
at c.a.a.a.n0.a(:610)
at c.a.a.a.h.<init>(:345)
at c.a.a.a.m.connect(:187)
at java.sql.DriverManager.getConnection(DriverManager.java:580)
at java.sql.DriverManager.getConnection(DriverManager.java:236)
at b.a.d.g.b(:41)
at com.dm._app.Syncronization$d.a(:225)
at com.dm._app.Syncronization$d.doInBackground(:192)
at android.os.AsyncTask$3.call(AsyncTask.java:378)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
2019-12-24 15:11:45.576 4844-4923/? E/InputDispatcher: channel 'a694dba processing Data (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-12-24 15:11:45.576 4844-4923/? E/InputDispatcher: channel 'f996a44 com.dm.rcl_app/com.dm.Syncronization (server)' ~ Channel is unrecoverably broken and will be disposed!
答案 0 :(得分:0)
更新 在proguard-rules.pro
中添加此行 -keep net.sourceforge.jtds.**
END更新
如果仅在发布模式下发生,则您尚未正确配置proguard规则。 看看this。
在proguard-rules.pro中添加此行应该可以解决问题
-dontwarn net.sourceforge.jtds.**