无法将 hf/leveldb-android 添加到 android 工作室

Unable to include hf/leveldb-android in android studio

我现在尝试了几个小时将 hf/leveldb-android 包含到现有的 android 项目中。要重现该问题,只需将项目下载为 *.zip 文件并将其添加为现有模块。到目前为止,上帝,但是如果 gradle 开始构建过程,它总是失败并显示以下消息。

Error:Execution failed for task ':leveldb:preBuildLevelDB'.
> A problem occurred starting process 'command 'sh''

我认为是由下面这行代码引起的。

task preBuildLevelDB {
doLast {
    exec {
        commandLine 'sh', projectDir.getAbsolutePath() + '/src/main/jni-prebuild/prebuild'
    }
  }
}

我不知道 'sh' 应该是什么工具,从未听说过,google 也帮不了我。我尝试了一些方法来修改 gradle.build 文件,但没有解决问题。有什么想法吗?

完成gradle.build

buildscript {
repositories {
    mavenLocal()
    mavenCentral()
}
dependencies {
    classpath 'com.android.tools.build:gradle:1.3.0'
}
}

apply plugin: 'android-library'

repositories {
mavenCentral()
}

android {
compileSdkVersion 22
buildToolsVersion "23.0.1"

defaultConfig {
    minSdkVersion 10
    targetSdkVersion 22

    versionCode 1
    versionName "1.0"

    testApplicationId "com.github.hf.leveldb.test"
    testInstrumentationRunner "android.test.InstrumentationTestRunner"
    testHandleProfiling true
    testFunctionalTest true
}
}

dependencies {
androidTestCompile 'commons-io:commons-io:2.+'
androidTestCompile 'org.assertj:assertj-core:1.6.+'
    }
task preBuildLevelDB {
doLast {
    exec {
        commandLine 'sh', projectDir.getAbsolutePath() + '/src/main/jni-prebuild/prebuild'
    }
    }
    }

preBuild.dependsOn preBuildLevelDB
preBuild {}.mustRunAfter preBuildLevelDB

预建

#!/bin/bash

PREBUILD_DIR=`dirname [=15=]`
PREBUILD_CHECKSUM=`find $PREBUILD_DIR -type f -name "*" -exec md5sum {} + | awk '{print }' | sort | md5sum`

JNI_DIR=$PREBUILD_DIR/../jni
JNI_LIBS_DIR=$PREBUILD_DIR/../jniLibs

LIBS_DIR=$PREBUILD_DIR/../libs

BUILD_DIR=$PREBUILD_DIR/../../../build
CHECKSUM_FILE=$BUILD_DIR/jni-prebuild-checksum

if [ -f $CHECKSUM_FILE ] && [ -d $JNI_LIBS_DIR ]
  then

  echo $PREBUILD_CHECKSUM | cmp $CHECKSUM_FILE -
  CMP_STATUS=$?

  if [ $CMP_STATUS -eq 0 ]
     then

    echo "Not compiling, sources have not changed."
    exit 0
 fi
fi

rm -f $CHECKSUM_FILE
rm -rf $JNI_DIR
rm -fr $JNI_LIBS_DIR
cp -r $PREBUILD_DIR $JNI_DIR

ndk-build --directory $JNI_DIR

BUILD_STATUS=$?

if [ $BUILD_STATUS -eq 0 ]
  then
  echo $PREBUILD_CHECKSUM > $CHECKSUM_FILE
  cp -r $LIBS_DIR $JNI_LIBS_DIR
fi

rm -rf $JNI_DIR

exit $BUILD_STATUS

我认为您不需要 sh,您可以使用 GString 辅助方法来简化代码:

task preBuildLevelDB {
    doLast {
        exec {
            // start the execution of the command
            Process process = "${projectDir.getAbsolutePath()}/src/main/jni-prebuild/prebuild".execute()
            // wait for the command to finish then check if successful
            // if we don't explicitly wait the command is executed async from the build
            // this can mean the task ends before the command is finished executing
            if (process.waitFor() != 0) {
                // If the prebuild failed then we probably want to fail the entire build
                throw new Exception("Failed during jni prebuild, check compiler output for error")
            }
        }
    }
}