Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
649 views
in Technique[技术] by (71.8m points)

java - How can I set up a simple gradle project that uses sqlite4java?

I'm starting a simple java test project using sqlite4java and building using java.

I can get the core sqlite4java library downloaded easily, but I'm not sure what the best (any!) way to get gradle to download the native libraries and put them in the right place.

This is my build.gradle file:

apply plugin: 'java'

/* We use Java 1.7 */
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'

repositories {
    mavenCentral()
}

sourceSets {
    main {
        java.srcDir 'src'
        output.classesDir = 'build/main'
    }
    test {
        java.srcDir 'test'
        output.classesDir = 'build/test'
    }
}


dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile "com.almworks.sqlite4java:sqlite4java:1.0.392"
    compile "com.almworks.sqlite4java:libsqlite4java-osx:1.0.392"
}

But when I run a simple test I get:

TestTest > testSqlite4Basic FAILED
    com.almworks.sqlite4java.SQLiteException at TestTest.java:15
        Caused by: java.lang.UnsatisfiedLinkError at TestTest.java:15

(I'm building from within IntelliJ, but am using the gradle build options - so I don't think it's ItelliJ stuffing up the class path when running the tests...)

I'm pretty sure the first time I tried to build I got some message about being unable to unpack the libsqlite4java-osx ZIP file, (not surprisingly as maven central says its a dylib file).

What do I need to do to make gradle do the right thing?

ASIDE: I'm able to get the code to run by manually copying the downloaded .dylib from the maven cache into somewhere listed in my java.library.path (Think I used $HOME/Library/Java/Extensions on my mac). But this seems contrary to the whole point of packaging all setup and dependencies in the .gradle file, and wont let me distribute anything easily later.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suppose, you need to use additional gradle plugin to handle native libraries or make your own specific tasks, to upload and put native libs in right place, in order to they could be found and linked.

At the moment I know only about one such a plugin, hope it can solve your problem https://github.com/cjstehno/gradle-natives

Edit: The problem with plugin in your case is the fact, that your dependecny "com.almworks.sqlite4java:libsqlite4java-osx:1.0.392" is native lib by itself, not a jar with included native lib as I supposed. So, in that case, you can simply add this dependency in dependencies par of build script, as it'a already done, and then create a custom copy task, to put it in any place you need. Tried to do it with gradle 2.6 on Win7, look like:

task copyNtiveDeps(type: Copy) {
  from (configurations.compile+configurations.testCompile) {
    include "libsqlite4java-osx-1.0.392.dylib"
  }
  into "c:\tmp"
}

In your case, you just need to set "into" property to some path from java.library.path. And the second, you can make this task runs automaticaly with gradle task properties dependsOn and mustRunAfter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...