Home > database >  Compile sub-project with regular JDK, add the result to my Android App
Compile sub-project with regular JDK, add the result to my Android App

Time:09-28

I'm trying to add a local copy of jTDS into my Android App, because there seems to be a bug where interrupting a thread running a query leads to a finalizer timeout in the JtdsStatement class, and I'd like to debug and fix this.

The problem is, jTDS won't build with the Android SDK. I have to build it with a regular JDK, because it uses some obscure JDK features like for instance the org.ietf.jgss Java package.

What certainly works is building a JAR of jTDS by compiling it outside Android Studio, then adding that JAR into my Android project. But better would be if I could have the source code of jTDS within my project and build it all together, so I can more rapidly test changes to jTDS in my app.

So is there a way to have a sub-project in Android Studio built with regular JDK, say for instance to generate a JAR, and then add that JAR as a dependency to the main project?

CodePudding user response:

I seem to have figured it out.

  1. Put the jTDS code into a jtds sub-directory under the top-level of your Android project.

  2. Create a build.gradle within this jtds sub-directory:

    apply plugin: 'java-library'
    
    java {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    
        sourceSets.main {
            java.srcDirs = ['src/main']
            resources.srcDirs = ['src/main']
        }
    }
    
    dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'lib')
    }
    
    compileJava.options.encoding = 'UTF-8'
    
    tasks.withType(JavaCompile) {
        options.encoding = 'UTF-8'
    }
    
  3. Include the sub-project (the correct term might be "module", I'm not sure) in your top-level settings.gradle file:

    include ':app', ':lib1', ':lib2', ':lib3', ':jtds'
    
  4. Add the module as a dependency to the app module by editing your app/build.gradle:

    // ...
    
    dependencies {
        // ...
        implementation project(':jtds')
        // ...
    }
    
    // ...
    

That's all, it works for me.

Some notes, for those who wish to understand more details:

  • The original jTDS source code was encoded in the Windows-1252 charset. I re-encoded the files to UTF-8 which is why I had to include the corresponding lines about file encoding in jtds/build.gradle. I could have probably left the files in the Windows encoding and omitted those lines from jtds/build.gradle but I prefer to have all my source code in UTF-8.

  • The resources.srcDirs = ['src/main'] line in jtds/build.gradle seems to be necessary to include some foo.properties files that are part of the jTDS source code into the generated JAR. Otherwise, I was getting an error message saying no message resource found for message property prop.servertype upon trying to connect to SQL Server.

  • The jTDS sources include a number of JAR files which they depend on, hence including them via the implementation fileTree(...) declaration in the dependencies block of jtds/build.gradle.

  • Related