如何在Android Studio中正确安装jdbc驱动程序?

时间:2018-07-05 08:16:16

标签: java android mysql jdbc

我一直试图在android studio中安装jdbc驱动程序以连接到mysql数据库。

我试图用两种方法来做到这一点,这些方法已经在stackoverflow上进行了解释,但是它们不再起作用了。两次我都使用解压缩下载的here文件后得到的jar文件。

第一种方法是将jar文件复制到libs文件夹,然后单击鼠标右键将其添加为库。

第二次尝试是按照本教程进行的:How to Mysql JDBC Driver to android studio

我总是可以构建我的应用程序,但是当我尝试在模拟器或外部Android设备上运行它时,出现以下错误:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app.app/com.app.app.LoginScreen}: java.lang.ClassNotFoundException: Didn't find class "com.app.app.LoginScreen" on path: DexPathList[[zip file "/data/app/com.app.app-XrBFCO1PMt-F537ZaZs6PQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.app.app-XrBFCO1PMt-F537ZaZs6PQ==/lib/x86, /system/lib, /vendor/lib]]
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.app.app.LoginScreen" on path: DexPathList[[zip file "/data/app/com.app.app-XrBFCO1PMt-F537ZaZs6PQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.app.app-XrBFCO1PMt-F537ZaZs6PQ==/lib/x86, /system/lib, /vendor/lib]]

这是使用第二种方式安装了库的build.gradle文件: 应用插件:“ com.android.application”

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "app"
        minSdkVersion 26
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile 'com.android.support:support-annotations:27.1.1'
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation project(':mysql-connector-java-8.0.11')
}

Litte旁注:我知道我的代码中没有错误,因为我在Intellij IDE中成功安装了驱动程序进行了测试。

我希望你们每个人都能帮助我。你会摇滚!

1 个答案:

答案 0 :(得分:1)

  1. 将.jar复制并粘贴到文件夹APP中:

YourApp \ app \ libs

  1. 在Gradle文件中添加:

     dependencies {
     implementation 'com.android.support:appcompat-v7:27.1.1'
     implementation fileTree(dir: 'libs', include: ['*.jar'])
     .
     .
     .
     implementation files('libs/YourFILE')
     }
    
  2. 添加您的课程。嗯,这对我有用。