aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/android/cmake/build.gradle
blob: 17a57b99fd6c9efc09bda0ce1249b1f51bd5af5c (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
apply plugin: 'com.android.library'

// TensorFlow repo root dir on local machine
def TF_SRC_DIR = projectDir.toString() + "/../../../.."

android {
    compileSdkVersion 24
    // Check local build_tools_version as this is liable to change within Android Studio.
    buildToolsVersion '25.0.2'

    // for debugging native code purpose
    publishNonDefault true

    defaultConfig {
        archivesBaseName = "Tensorflow-Android-Inference"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters  'armeabi-v7a'
        }
        externalNativeBuild {
            cmake {
                arguments '-DANDROID_TOOLCHAIN=gcc',
                          '-DANDROID_STL=gnustl_static'
            }
        }
    }
    sourceSets {
        main {
            java {
                srcDir "${TF_SRC_DIR}/tensorflow/contrib/android/java"
                srcDir "${TF_SRC_DIR}/tensorflow/java/src/main/java"
                exclude '**/examples/**'
            }
        }
    }

    externalNativeBuild {
        cmake {
            path 'CMakeLists.txt'
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                          'proguard-rules.pro'
        }
    }
}

// Build libtensorflow-core.a if necessary
// Note: the environment needs to be set up already
//    [ such as installing autoconfig, make, etc ]
// How to use:
//    1) install all of the necessary tools to build libtensorflow-core.a
//    2) inside Android Studio IDE, uncomment buildTensorFlow in
//       whenTaskAdded{...}
//    3) re-sync and re-build. It could take a long time if NOT building
//       with multiple processes.
import org.apache.tools.ant.taskdefs.condition.Os

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties')
                .newDataInputStream())
def ndkDir = properties.getProperty('ndk.dir')
if (ndkDir == null || ndkDir == "") {
    ndkDir = System.getenv('ANDROID_NDK_HOME')
}

if(! Os.isFamily(Os.FAMILY_WINDOWS)) {
    // This script is for non-Windows OS. For Windows OS, MANUALLY build
    // (or copy the built) libs/headers to the
    //    ${TENSORFLOW_ROOT_DIR}/tensorflow/contrib/makefile/gen
    // refer to CMakeLists.txt about lib and header directories for details
    task buildTensorflow(type: Exec) {
        group 'buildTensorflowLib'
        workingDir getProjectDir().toString() + '/../../../../'
        environment PATH: '/opt/local/bin:/opt/local/sbin:' +
                          System.getenv('PATH')
        environment NDK_ROOT: ndkDir
        commandLine 'tensorflow/contrib/makefile/build_all_android.sh'
    }

    tasks.whenTaskAdded { task ->
        group 'buildTensorflowLib'
        if (task.name.toLowerCase().contains('sources')) {
            def tensorflowTarget = new File(getProjectDir().toString() +
                    '/../../makefile/gen/lib/libtensorflow-core.a')
            if (!tensorflowTarget.exists()) {
                // Note:
                //    just uncomment this line to use it:
                //    it can take long time to build by default
                //    it is disabled to avoid false first impression
                task.dependsOn buildTensorflow
            }
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}