3

I'm trying to learn how to use the JDBC to connect a application to a MySQL database.

I'm using the Android Studio.

I downloaded "mysql-connector-java-5.1.37" from the MySQL website.

Just for put the "mysql-connector-java-5.1.37-bin.jar" in libs folder of a Hello World application and compile I receive :

Error:Execution failed for task ':app:preDexDebug'. com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_45\bin\java.exe'' finished with non-zero exit value 1

the build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "br.com.myaplication.helloworld"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.0'
}
Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
phtoxa
  • 41
  • 7

3 Answers3

0

Try to sync your project Tools -> Android -> Sync Project with Gradle Files

And then rebuild your project: build -> rebuild project

Tobi
  • 924
  • 1
  • 10
  • 39
  • You get the error you posted while building the project? Can you please post your complete gradle log when building – Tobi Oct 17 '15 at 22:21
  • yes, while building.. dont fit here, but starts bug here:app:processDebugJavaRes UP-TO-DATE :app:compileDebugJavaWithJavac UP-TO-DATE :app:compileDebugNdk UP-TO-DATE :app:compileDebugSources UP-TO-DATE :app:preDexDebug AGPBI: {"kind":"simple","text":"warning: Ignoring InnerClasses attribute for an anonymous inner class","sources":[{}]} AGPBI: {"kind":"simple","text":"(com.mysql.jdbc.Connection$1) that doesn\u0027t come with an","sources":[{}]} AGPBI: {"kind":"simple","text":"associated EnclosingMethod attribute. – phtoxa Oct 17 '15 at 22:33
0

I had the same issue and the problem was in the mysql-connector-java-5.1.37.jar file. I downloaded an older version (mysql-connector-java-3.0.17-ga-bin.jar) from here and worked like a charm.

To get the connection use:

Class.forName("com.mysql.jdbc.Driver").newInstance();
String connection = "jdbc:mysql://"+ your_ip + ":"+ your_port +"/"+ database_name;
conn = DriverManager.getConnection(connection, user, pass); 

Just in case, don't forget to compile it in your build.gradle:

dependencies {
    //other stuff
    compile files('libs/mysql-connector-java-3.0.17-ga-bin.jar')
}

PD: This only works if you use the connection from and AsyncTask

Carlos Borau
  • 1,433
  • 1
  • 24
  • 34
0

Because Distributed mysql-connector-java-5.1.37-bin.jar is compiled for Java1.8, current Android dex compiler cannot recognize it.

$ javap -v com/mysql/jdbc/JDBC42Helper.class| grep "major version"
  major version: 52

On the oher hand, mysql-connector-java-5.1.36.jar is compiled for Java1.6.

$ javap -v com/mysql/jdbc/JDBC4Connection.class |grep "major version"
  major version: 50

5.1.37 supports JDBC4.2, so requires Java1.8.