aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/examples/android/build.gradle
blob: 4f241027f4b6e80089be2bca179a8d4d4f565032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// This file provides basic support for building the TensorFlow demo
// in Android Studio with Gradle.
//
// Note that Bazel is still used by default to compile the native libs,
// and should be installed at the location noted below. This build file
// automates the process of calling out to it and copying the compiled
// libraries back into the appropriate directory.
//
// Alternatively, experimental support for Makefile builds is provided by
// setting buildWithMake below to true. This will allow building the demo
// on Windows machines, but note that full equivalence with the Bazel
// build is not yet guaranteed. See comments below for caveats and tips
// for speeding up the build, such as as enabling ccache.

// Set to true to build with make.
// NOTE: Running a make build will cause subsequent Bazel builds to *fail*
// unless the contrib/makefile/downloads/ and gen/ dirs are deleted afterwards.
def buildWithMake = false

// Controls output directory in APK and CPU type for Bazel builds.
// NOTE: Does not affect the Makefile build target API (yet), which currently
// assumes armeabi-v7a. If building with make, changing this will require
// editing the Makefile as well.
def cpuType = 'armeabi-v7a'

// Output directory in the local directory for packaging into the APK.
def nativeOutDir = 'libs/' + cpuType

// Default to building with Bazel and override with make if requested.
def nativeBuildRule = 'buildNativeBazel'
def demoLibPath = '../../../bazel-bin/tensorflow/examples/android/libtensorflow_demo.so'
def inferenceLibPath = '../../../bazel-bin/tensorflow/contrib/android/libtensorflow_inference.so'
if (buildWithMake) {
    nativeBuildRule = 'buildNativeMake'
    demoLibPath = '../../../tensorflow/contrib/makefile/gen/lib/libtensorflow_demo.so'
    inferenceLibPath = '../../../tensorflow/contrib/makefile/gen/lib/libtensorflow_inference.so'
}

// Defines the NDK location for Makefile builds. Does *not* affect Bazel builds.
// Override with your absolute NDK location if this fails to get the location
// automatically.
def makeNdkRoot = System.getenv('NDK_ROOT')

// If building with Bazel, this is the location of the bazel binary.
// NOTE: Bazel does not yet support building for Android on Windows,
// so in this case the Makefile build must be used as described above.
def bazelLocation = '/usr/local/bin/bazel'

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

// import DownloadModels task
project.ext.ASSET_DIR = projectDir.toString() + '/assets'
project.ext.TMP_DIR   = project.buildDir.toString() + '/downloads'

buildscript {
    repositories {
        jcenter()
    }

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

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "25.0.2"

    lintOptions {
        abortOnError false
    }

    sourceSets {
        main {
            // TensorFlow Java API sources.
            java {
                srcDir '../../java/src/main/java'
                exclude '**/examples/**'
            }

            // Android TensorFlow wrappers, etc.
            java {
                srcDir '../../contrib/android/java'
            }

            // Android demo app sources.
            java {
                srcDir 'src'
            }

            manifest.srcFile 'AndroidManifest.xml'
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = [project.ext.ASSET_DIR]
            jniLibs.srcDirs = ['libs']
        }

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

task buildNativeBazel(type: Exec) {
    workingDir '../../..'
    commandLine bazelLocation, '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 buildNativeMake(type: Exec) {
    environment "NDK_ROOT", makeNdkRoot
    // Tip: install ccache and uncomment the following to speed up
    // builds significantly.
    // environment "CC_PREFIX", 'ccache'
    workingDir '../../..'
    commandLine 'tensorflow/contrib/makefile/build_all_android.sh',  \
         '-s',  \
         'tensorflow/contrib/makefile/sub_makefiles/android/Makefile.in',  \
         '-t',  \
         'libtensorflow_inference.so libtensorflow_demo.so'  \
         //, '-T'  // Uncomment to skip protobuf and speed up subsequent builds.
}


task copyNativeLibs(type: Copy) {
    from demoLibPath
    from inferenceLibPath
    into nativeOutDir
    duplicatesStrategy = 'include'
    dependsOn nativeBuildRule
    fileMode 0644
}


tasks.whenTaskAdded { task ->
    if (task.name == 'assembleDebug') {
        task.dependsOn 'copyNativeLibs'
    }
    if (task.name == 'assembleRelease') {
        task.dependsOn 'copyNativeLibs'
    }
}

// Download default models; if you wish to use your own models then
// place them in the "assets" directory and comment out this line.
apply from: "download-models.gradle"