简体   繁体   English

如何设置使用sqlite4java的简单gradle项目?

[英]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. 我正在使用sqlite4java开始一个简单的java测试项目并使用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. 我可以轻松地下载核心sqlite4java库,但我不确定最好(任何!)方式来获取gradle来下载本机库并将它们放在正确的位置。

This is my build.gradle file: 这是我的build.gradle文件:

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...) (我在IntelliJ中构建,但是使用了gradle构建选项 - 所以我不认为ItelliJ在运行测试时会填充类路径......)

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). 我很确定第一次尝试构建时,我得到了一些关于无法解压缩libsqlite4java-osx ZIP文件的消息,(毫不奇怪,maven central称其为dylib文件)。

What do I need to do to make gradle do the right thing? 我需要做些什么来让gradle做正确的事情?

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). ASIDE :我能够通过手动将下载的.dylib从maven缓存复制到我的java.library.path列出的地方来运行代码(想想我在我的mac上使用了$HOME/Library/Java/Extensions )。 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. 但这似乎与.gradle文件中包装所有设置和依赖关系的整个观点相反,并且.gradle我稍后轻松分发任何内容。

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. 我想,您需要使用额外的gradle插件来处理本机库或制定您自己的特定任务,以便将原生库上传并放置在正确的位置,以便找到并链接它们。

At the moment I know only about one such a plugin, hope it can solve your problem https://github.com/cjstehno/gradle-natives 目前我只知道一个这样的插件,希望它可以解决你的问题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. 编辑:在您的情况下插件的问题是这样的事实,您的dependecny“com.almworks.sqlite4java:libsqlite4java-osx:1.0.392”本身就是本机库,而不是像我想的那样包含本机库的jar。 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: 试图用Win7上的gradle 2.6来做,看起来像:

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. 在您的情况下,您只需要将“into”属性设置为java.library.path中的某个路径。 And the second, you can make this task runs automaticaly with gradle task properties dependsOn and mustRunAfter. 第二,您可以使用gradle任务属性dependsOn和mustRunAfter自动运行此任务。

Here's a complete answer based on Stanislav's comments. 这是基于斯坦尼斯拉夫的评论的完整答案。

apply plugin: 'java'

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

repositories { mavenCentral() }

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"
}

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

/* Copy the native files */
task copyNativeDeps(type: Copy) {
    from (configurations.compile+configurations.testCompile) {
        include "*.dylib"
    }
    into 'build/libs'
}

/* Make sure we setup the tests to actually copy 
 * the native files and set the paths correctly. */
test {
    dependsOn copyNativeDeps
    systemProperty "java.library.path", 'build/libs'
}

And example test source to run for it: 并运行示例测试源:

import com.almworks.sqlite4java.SQLiteConnection;
import com.almworks.sqlite4java.SQLiteStatement;
import org.junit.Test;

import java.io.File;

public class SqliteTest {

    @Test public void aTest() throws Exception {
        SQLiteConnection db = new SQLiteConnection(new File("/tmp/database"));
        db.open(true);  

        SQLiteStatement st = db.prepare("SELECT name FROM dummy");
        try {
            while(st.step()) {
                System.err.printf("name = %s\n", st.columnString(1));
            }
        } finally {
            st.dispose();
        }
    }
}

I used this method to setup sqlite4java in anroid studio 3.1.4. 我使用这种方法在anroid studio 3.1.4中设置sqlite4java。

First download the library from this link: https://bitbucket.org/almworks/sqlite4java 首先从以下链接下载库: https//bitbucket.org/almworks/sqlite4java

The download will contain a zip file containing a folder named android and other .jar , .so and .dll files. 下载将包含一个zip文件,其中包含一个名为android的文件夹和其他.jar,.so和.dll文件。

Copy the three folders in android ie 'armeabi', 'armeabi-v7a' and 'x86' into a folder named 'jniLibs'. 将android中的三个文件夹即'armeabi','armeabi-v7a'和'x86'复制到名为'jniLibs'的文件夹中。

Copy the above folder ie 'jniLibs' into yourproject/app/src/main/ folder. 将上面的文件夹即'jniLibs'复制到yourproject / app / src / main /文件夹中。

Then implement the gradle dependencies for java4sqlite: 然后实现java4sqlite的gradle依赖项:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "your.project.name"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}




dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:design:26.1.0'
    implementation 'com.readystatesoftware.sqliteasset:sqliteassethelper:2.0.1'
    implementation "com.almworks.sqlite4java:sqlite4java:1.0.392"
    implementation "com.almworks.sqlite4java:libsqlite4java-osx:1.0.392"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'


}

For compiling your own sqlite3 libraries , you can check github android-sqlite3 要编译自己的sqlite3库 ,可以检查github android-sqlite3

For the current Android Studio (3.3.1), you can simply add the .so files inside app/build.gradle like below: 对于当前的Android Studio(3.3.1),您只需在app/build.gradle添加.so文件,如下所示:

android {
    ...

    sourceSets {
        main {
            jniLibs.srcDirs += ['<your-path>/your-libs']
        }
    }

    ...
}

Explanation 说明

jniLibs.srcDirs is the gradle path pointing at jni libraries, operator += denotes the additional jni libraries besides the default ones inside app/src/main/jniLibs as below: jniLibs.srcDirs是指向jni库的gradle路径,operator +=表示除了app/src/main/jniLibs的默认库之外的其他 jni库,如下所示:

app/
├──libs/
|  └── *.jar           <-- java libs
├──src/
   └── main/
       ├── AndroidManifest.xml
       ├── java/
       └── jniLibs/    <-- default directory for jni libs, i.e. <ANDROID_ABI>/**.so

And please note that the jni libraries have to be organized according to android ABI, ie 请注意,jni库必须根据android ABI进行组织,即

your-libs/
├── arm64-v8a/                       <-- ARM 64bit
│   └── lib-your-abc.so
├── armeabi-v7a/                     <-- ARM 32bit
│   └── lib-your-abc.so
├── x86_64/                          <-- Intel 64bit
│   └── lib-your-abc.so
└── x86/                             <-- Intel 32bit
    └── lib-your-abc.so

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM