2015-11-01 84 views
0

我現在嘗試幾個小時將hf/leveldb-android包含到現有的Android項目中。要重現此問題,只需將該項目下載爲* .zip文件並將其添加爲現有模塊。到目前爲止這麼神,但如果gradle開始構建過程,它總是失敗並顯示以下消息。無法在android工作室中包含hf/leveldb-android

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'應該是一個工具,從來沒有聽說過它,谷歌並沒有幫助我。我嘗試了幾件事來修改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 $0` 
PREBUILD_CHECKSUM=`find $PREBUILD_DIR -type f -name "*" -exec md5sum {} + | awk '{print $1}' | 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 

回答

0

我不認爲你需要的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") 
      } 
     } 
    } 
} 
+0

尚未解決,但在正確的道路上。 'sh'看起來像某種腳本。將文件擴展名更改爲* .bat(在windwos計算機上工作),腳本可以執行,但在以下行上失敗:PREBUILD_CHECKSUM。認爲這一行有一些問題。將更新預構建內容的問題。 – sandkasten

+0

關於'如何將unix shell(sh)腳本轉換爲Windows批處理(bat)腳本',您應該打開第二個問題。以下是一些參考資料:https://en.wikipedia.org/wiki/Shell_script。我猜測它的失敗行'PREBUILD_CHECKSUM = \'find $ PREBUILD_DIR -type f -name「*」-exec md5sum {} + | awk'{print $ 1}'|排序| md5sum \''這是因爲命令'find','awk','sort'和'md5sum'是unix系統中'Bourne Shell'使用的unix命令。您可以在Cygwin終端中創建,因爲它可以在Windows環境中使用許多bash函數。 – JBirdVegas

+0

問題的第二部分可能會重寫'powershell'或'cmd'的''''sh'文件。或者建立在Cygwin終端。或者建立在unix vm上。無論哪種方式第一和第二個問題是不相關 – JBirdVegas