Throwable.initCause 在 Android 中使用 MySQL/JDBC 时未找到

Throwable.initCause not found when using MySQL/JDBC in Android

在使用 Web 界面对我大吼大叫之前,请注意我正在编写 MySQL 客户端应用程序,而不是试图直接访问私有公共数据库。

我已将 mysql-connector-java 依赖项 (5.1.34)(通过 Maven)正确添加到 build.gradle 脚本中。它通过 Android 模拟器正确安装我的应用程序。但是,通过this template传递给Travis-CI时,出现找不到javax.*类等错误。好的,因为这是 Android,我可以理解 javax.* 类 可能不存在。但是我注意到这条线很奇怪,我不明白为什么:

Warning: com.mysql.jdbc.jdbc2.optional.MysqlXAConnection: can't find referenced method 'java.lang.Throwable initCause(java.lang.Throwable)' in program class com.mysql.jdbc.jdbc2.optional.MysqlXAException

为什么 Throwable 中的方法不存在?

这是我的 app/build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        applicationId "application.id.here"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        debug {
            debuggable true
            minifyEnabled true
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

repositories {
    mavenCentral()
}

dependencies {
    provided 'org.projectlombok:lombok:1.12.6'
    compile 'commons-io:commons-io:2.5'
    compile 'mysql:mysql-connector-java:5.1.34'
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:support-v4:24.0.0'
    compile 'com.android.support:design:24.0.0'
}

我不确定哪些文件是相关的,所以如果有任何其他文件与该问题相关,请发表评论。

This is a log 当我从 Android Studio 单击 "Build APK" 时出现的警告消息。

终于...

我还想问一下,有没有办法解决这个问题来正确构建包含 mysql-connector 的 APK?

我会将 minifyEnabled 设置为 false 用于 debug buildTypetrue 用于 release 然后检查相关的proguard规则保留您丢失的 类 是正确的。

buildTypes {
    debug {
        debuggable true
        minifyEnabled false
    }
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

或者,如果您的 proguard 文件已经正确:

buildTypes {
    debug {
        debuggable true
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}