1

I have been trying to install the jdbc driver in android studio to connect to a mysql database.

I have tried to do it in 2 ways which were already explained on stackoverflow but they don't work anymore. Both times I used the jar-file I got after unzipping the file I downloaded here.

The first way was by copying the jar file into the libs folder and adding it as library with a right-mouse click.

The second try was by following this tutorial: How to Mysql JDBC Driver to android studio.

I can always build my app but when I try to run it on the emulator or on an extern android device I got these errors:

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]]

This is the build.gradle file with the library installed the second way: apply plugin: '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 sidenote: I know that there is no error in my code because I tested it int he Intellij IDE where I did succeed to install the driver.

I hope anybody of you guys can help me. You would rock!

Luigi-stx
  • 21
  • 1
  • 1
  • 2
  • You really should not use JDBC drivers from Android applications; direct database access like that is unsafe and likely performs badly. But if you really want to: the driver is on Maven Central, so just specify the maven coordinates `'mysql:mysql-connector-java:8.0.11'` – Mark Rotteveel Jul 05 '18 at 08:20
  • 1
    The error messages says nothing on not finding mysql libs! – Gyro Gearless Jul 05 '18 at 08:21
  • @MarkRotteveel Which alternative do you think is the best to connect to a database? – Luigi-stx Jul 05 '18 at 08:23
  • You need something like a REST service. Your android app talks to the webservice (with appropriate authentication), the rest service talks to the database. This means that your database doesn't need to be publicly available, and the rest endpoint should reduce the attack surface by restricting access to only what is necessary. – Mark Rotteveel Jul 05 '18 at 08:41

1 Answers1

2
  1. Copy and Paste the .jar in your folder APP:

YourApp\app\libs

  1. In the Gradle file add:

     dependencies {
     implementation 'com.android.support:appcompat-v7:27.1.1'
     implementation fileTree(dir: 'libs', include: ['*.jar'])
     .
     .
     .
     implementation files('libs/YourFILE')
     }
    
  2. Add your class. Well that worked for me.

Roga Lu
  • 162
  • 1
  • 3
  • 14