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.
Put the jTDS code into a
jtds
sub-directory under the top-level of your Android project.Create a
build.gradle
within thisjtds
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' }
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'
Add the module as a dependency to the
app
module by editing yourapp/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 fromjtds/build.gradle
but I prefer to have all my source code in UTF-8.The
resources.srcDirs = ['src/main']
line injtds/build.gradle
seems to be necessary to include somefoo.properties
files that are part of the jTDS source code into the generated JAR. Otherwise, I was getting an error message sayingno 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 thedependencies
block ofjtds/build.gradle
.