Breandan
Breandan

Reputation: 13

Android Studio preDexDebug error when compiling MySQL Connector Jar file

Trying to connect to a MySQL database in Android Studio 1.4.1. Placed MySQL Connector JAR file in libs directory and added it as library by doing a compile files('libs/mysql.jar').

When I try run the app I get the following error:

:app:preDexDebug
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: Exception parsing classes
at com.android.dx.command.dexer.Main.processClass(Main.java:752)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:718)
at com.android.dx.command.dexer.Main.access$1200(Main.java:85)
at com.android.dx.command.dexer.Main$FileBytesConsumer.processFileBytes(Main.java:1645)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
at com.android.dx.command.dexer.Main.processOne(Main.java:672)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:574)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:311)
at com.android.dx.command.dexer.Main.run(Main.java:277)
at com.android.dx.command.dexer.Main.main(Main.java:245)
at com.android.dx.command.Main.main(Main.java:106)

Caused by: com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)
at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:472)
at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
at com.android.dx.command.dexer.Main.parseClass(Main.java:764)
at com.android.dx.command.dexer.Main.access$1500(Main.java:85)
at com.android.dx.command.dexer.Main$ClassParserTask.call(Main.java:1684)
at com.android.dx.command.dexer.Main.processClass(Main.java:749)
... 12 more
1 error; aborting
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_74\bin\java.exe'' finished with non-zero exit value 1

Here's my build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.breandan.dishfinder"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile files('libs/mysql.jar')
}

When I remove the compile files('libs/mysql.jar') line, the code runs but it doesn't do any of my database code.

Upvotes: 1

Views: 1058

Answers (1)

Mohammad
Mohammad

Reputation: 6148

If you want to add MySQL JAR to your project, you can do it in three standard ways. If you do it in these standard ways, Android studio will recognize them. Please read this link https://stackoverflow.com/a/35369267/5475941. In this post I explained how to import your JAR files in Android studio and I explained all possible ways step by step with screenshots.

This is where you can find the JAR file (you should have a free Oracle account to download):

https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.38.tar.gz

You can also find it in your local MySQL installation directory. This is the address in my machine.

enter image description here

Then, add it to your Android studio:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

Then, you use it like this:

public class MysqlSample01Activity extends Activity {

private static final String url = "jdbc:mysql://<server>:<port>/<database>";
private static final String user = "<your username>";
private static final String pass = "<your password>";

/** This will run when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    connectToDB();
}

public void connectToDB() {
    ...
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(url, user, pass);
        System.out.println("Database connection was successful");

        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from table_name");
        ResultSetMetaData rsmd = rs.getMetaData();

        while(rs.next()) {
            result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
            result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
        }
        ...
    }
    catch(Exception e) {
        e.printStackTrace();
        tv.setText(e.toString());
    }   

}
}

Upvotes: 2

Related Questions