将JDBC导入Android Studio错误

时间:2022-11-21 16:09:39

I am currently working on an Android app and I am attempting to retrieve information from a MySQL database. I am trying to use JDBC as instructed by my professors, but I keep running into problems.

我目前正在开发Android应用程序,我正在尝试从MySQL数据库中检索信息。我正在尝试按照我的教授的指示使用JDBC,但我一直遇到问题。

I have installed the MySQL Connector/ODBC 5.3, set a CLASSPATH environment variable for the driver I am attempting to use, which is "mysql-connector-java-5.1.38-bin.jar", and I attempted to import that JAR into my project.

我已经安装了MySQL Connector / ODBC 5.3,为我试图使用的驱动程序设置了一个CLASSPATH环境变量,即“mysql-connector-java-5.1.38-bin.jar”,我试图将该JAR导入我的项目。

I have attempted to import the JAR in two ways, which are the following:

我试图以两种方式导入JAR,具体如下:

File > New Module > Import .JAR/.AAR Package > import the mysql connector JAR Then I proceeded to go to File > Project Structure > app > Dependencies > add module dependency

文件>新模块>导入.JAR / .AAR包>导入mysql连接器JAR然后我继续转到文件>项目结构>应用程序>依赖项>添加模块依赖项

The other way I have attempted to do this was by adding the JAR into the "libs" folder of my project and follow the Project structure path to add a dependency from file.

我尝试这样做的另一种方法是将JAR添加到项目的“libs”文件夹中,并按照Project结构路径从文件中添加依赖项。

I am running the latest version of Android Studio 2.0

我正在运行最新版本的Android Studio 2.0

The build completes successfully when following either of the importing steps however when doing so I get the following error when attempting to run the app:

在执行任一导入步骤时,构建成功完成,但是在执行此操作时,尝试运行应用程序时出现以下错误:

    Execution failed for task ':app:transformClassesWithInstantRunForDebug'.
    > JSR/RET are not supported with computeFrames option

The only solution I found relevant to this was going to Android Studio Settings and turning the Instant Run option off.

我发现与此相关的唯一解决方案是进入Android Studio设置并关闭“即时运行”选项。

After doing so I received the following error instead:

执行此操作后,我收到以下错误:

    Execution failed for task ':app:transformClassesWithDexForDebug'.
    > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException:  java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_77\bin\java.exe'' finished with non-zero exit value 1

The following is my build.gradle(Module:app):

以下是我的build.gradle(模块:app):

  apply plugin: 'com.android.application'

  android {
compileSdkVersion 23
buildToolsVersion '23.0.3'

defaultConfig {
    applicationId "com.example.JohnDoe.myapp"  //This is not actually the name or the name of the app on there but I changed this for privacy.
    minSdkVersion 14
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    multiDexEnabled true

}

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

dexOptions {
    incremental true
    javaMaxHeapSize "2048M"
    jumboMode = true
}
  }

  dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.2.1'
compile 'com.squareup.retrofit2:retrofit:2.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
compile 'com.android.support:multidex:1.0.1'
compile project(':mysql-connector-java-5.1.38-bin')
  }

The following is a link to the whole gradle console output after turning "Instant Run" off: pastebin.com/5kj6Cgeb

以下是关闭“Instant Run”后整个gradle控制台输出的链接:pastebin.com/5kj6Cgeb

Please advise me where to go from here to get the JDBC driver working so I can finish this project. Thank you for your time.

请告诉我从哪里开始使用JDBC驱动程序,这样我才能完成这个项目。感谢您的时间。

UPDATE 1: Apparently the latest JDBC driver is not fully compatible with Android and causes issues. I installed and imported the previous version (5.0.8) and so far the app at least runs. Will be attempting to implement the driver now and actually develop with it, at which time I will post another update on the outcome.

更新1:显然最新的JDBC驱动程序与Android不完全兼容并导致问题。我安装并导入了以前的版本(5.0.8),到目前为止应用程序至少运行。将尝试现在实现驱动程序并实际使用它进行开发,此时我将发布关于结果的另一个更新。

1 个解决方案

#1


1  

You should not use JDBC in Android Apps as it is targetted to high bandwith, reliable connections to a database server, something you will not achieve on a mobile device. Try to wrap your database in a REST web service instead.

您不应该在Android应用中使用JDBC,因为它的目标是高带宽,与数据库服务器的可靠连接,这是您在移动设备上无法实现的。尝试将数据库包装在REST Web服务中。

I've read several times that people struggled with using JDBC in Android, it's just not built for beeing used in the Android ecosystem. Not every library is fully compatible with Android, your gradle output suggests that the "dexer" could not convert the jar file to a .dex.

我曾多次读到人们在Android中使用JDBC的困难,它只是不是为Android生态系统中使用的内容而构建的。并非每个库都与Android完全兼容,您的gradle输出表明“dexer”无法将jar文件转换为.dex。

Your provided gradle output show the following error:

您提供的gradle输出显示以下错误:

com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)

See this question for a possible solution. Ensure you are building with the right Java SDK.

请参阅此问题以获得可能的解决方案确保使用正确的Java SDK构建。

#1


1  

You should not use JDBC in Android Apps as it is targetted to high bandwith, reliable connections to a database server, something you will not achieve on a mobile device. Try to wrap your database in a REST web service instead.

您不应该在Android应用中使用JDBC,因为它的目标是高带宽,与数据库服务器的可靠连接,这是您在移动设备上无法实现的。尝试将数据库包装在REST Web服务中。

I've read several times that people struggled with using JDBC in Android, it's just not built for beeing used in the Android ecosystem. Not every library is fully compatible with Android, your gradle output suggests that the "dexer" could not convert the jar file to a .dex.

我曾多次读到人们在Android中使用JDBC的困难,它只是不是为Android生态系统中使用的内容而构建的。并非每个库都与Android完全兼容,您的gradle输出表明“dexer”无法将jar文件转换为.dex。

Your provided gradle output show the following error:

您提供的gradle输出显示以下错误:

com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)

See this question for a possible solution. Ensure you are building with the right Java SDK.

请参阅此问题以获得可能的解决方案确保使用正确的Java SDK构建。