Android中的Tensorflow,gradle问题

时间:2016-12-14 17:53:26

标签: android-studio gradle tensorflow

我正在尝试使用android中的tensorflow,有点出于默认项目文件夹。我修改了我的build.gradle,但显然我错过了一些东西,因为我的应用程序在运行时崩溃,因为它依赖于本机库的不可用性。我花了好几个小时来解决这个问题,但无济于事。请问有人帮忙吗?

基本上,我比较了工作tf demo和我的.so制作的APK中的.so文件,它们是不同的。所以我想我必须在我的剧本中某处......

def bazel_location = '/usr/local/bin/bazel'
    def tf_location = '/home/poborak/SW/tensorflow-master'

    def cpuType = 'armeabi-v7a'
    def nativeDir = 'src/main/jniLibs/' + cpuType

project.buildDir = 'gradleBuild'
getProject().setBuildDir('gradleBuild')

buildscript {
    System.properties['com.android.build.gradle.overrideVersionCheck'] = 'true'

    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.3'
    }
}

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId "bazinac.aplikacenahouby"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java', tf_location+'/tensorflow/contrib/android/java']
            resources.srcDirs = ['src/main/java']
            aidl.srcDirs = ['src/main/java']
            renderscript.srcDirs = ['src/main/java']
            res.srcDirs = ['src/main/res']
            assets.srcDirs = ['src/main/assets']
            jniLibs.srcDirs = ['src/main/jniLibs']
            jni.srcDirs = []
        }

        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }

    buildTypes {

        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        debug {
            debuggable true
         }
    }
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    testCompile 'junit:junit:4.12'
    compile 'org.apache.commons:commons-io:1.3.2'
}


task buildNative(type:Exec) {
    workingDir tf_location
    commandLine bazel_location, 'build', '-c', 'opt', \
      'tensorflow/examples/android:tensorflow_native_libs', \
       '--crosstool_top=//external:android/crosstool', \
       '--cpu=' + cpuType, \
       '--host_crosstool_top=@bazel_tools//tools/cpp:toolchain'
}

task copyNativeLibs(type: Copy) {
    from(tf_location+'bazel-bin/tensorflow/examples/android') { include '**/*.so' }
    into nativeDir
        duplicatesStrategy = 'include'
    }

    copyNativeLibs.dependsOn buildNative
    assemble.dependsOn copyNativeLibs

1 个答案:

答案 0 :(得分:1)

最后我找到了解决方法。它是将Gradle降级到2.14.xx和Android插件版本2.1.3。然后我附加了assembleDebug.dependsOn copyNativeLibs,它现在可以工作了。当然,我仍然会对更好的解决方案感兴趣。 (在较新的APV中无法识别AssembleDebug属性。