build.gradle 8.52 KB
Newer Older
1 2 3 4 5
final ASSETS_CHECKSUM_FIELD_TYPE = "String";
final ASSETS_CHECKSUM_FIELD_NAME = "ASSETS_CHECKSUMS";
final BUILD_CONFIG_DIR           = "${buildDir}/generated/source/buildConfig/"
final BUILD_CONFIG_ENCODING      = 'UTF-8'

6
android {
7
    buildToolsVersion "20.0.0"
8
    compileSdkVersion 21
9

10 11 12 13 14
    packagingOptions {
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
    }

15 16 17
    lintOptions {
        abortOnError false
    }
18

19
    defaultConfig {
20
        targetSdkVersion 17
21 22
        versionName tablexiaVersionName
        versionCode tablexiaVersionCode
23 24
        applicationId rootProject.applicationBaseId
        testApplicationId rootProject.applicationBaseId + ".test"
25
    }
26 27 28

    buildTypes {
        debug {
29
            applicationIdSuffix rootProject.applicationIdBranch + rootProject.applicationIdDebugSuffix
30
            buildConfigField ASSETS_CHECKSUM_FIELD_TYPE, ASSETS_CHECKSUM_FIELD_NAME, rootProject.ext.assetsChecksumPattern
31
            resValue "string", "app_name", "${tablexiaAppName}"
32 33 34
        }
        release {
            debuggable false
35
            applicationIdSuffix rootProject.applicationIdBranch
36
            buildConfigField ASSETS_CHECKSUM_FIELD_TYPE, ASSETS_CHECKSUM_FIELD_NAME, rootProject.ext.assetsChecksumPattern
37
            resValue "string", "app_name", "${tablexiaAppName}"
38 39 40 41
        }
        devel.initWith(buildTypes.debug)
        devel {
            applicationIdSuffix rootProject.applicationIdDevelSuffix
42
            buildConfigField ASSETS_CHECKSUM_FIELD_TYPE, ASSETS_CHECKSUM_FIELD_NAME, rootProject.ext.assetsChecksumPattern
43
            resValue "string", "app_name", "${tablexiaAppName}"
44 45 46
        }
    }

47 48 49
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
50
            res.srcDirs = ['res/main']
51
            assets.srcDirs = ['assets']
52
            java.srcDir file('src/main/java')
53
            jniLibs.srcDirs = ['libs']
54 55 56
        }
        androidTest {
            java.srcDir file('src/androidTest/java')
57
        }
58 59 60 61
        test {
            java.srcDir file('src/test/java')
        }

62 63 64 65 66 67
        release {
            res.srcDirs = ['res/release']
        }
        debug {
            res.srcDirs = ['res/debug']
        }
68 69 70
        devel {
            res.srcDirs = ['res/devel']
        }
71 72
    }

73
    assemble.dependsOn = ['assembleRelease', 'assembleDebug']
74 75

    applicationVariants.all { variant ->
76
        if (!variant.buildType.name.equals("devel")) {
77 78 79 80
            variant.outputs.each { output ->
                def file = output.outputFile
                output.outputFile = new File(file.parent, file.name.replace(".apk", "-" + tablexiaVersionName + ".apk").replace("android", appName))
            }
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        }
    }

    if (project.hasProperty('TABLEXIA_RELEASE_KEYSTORE') && project.hasProperty('TABLEXIA_RELEASE_KEYSTORE_PASSWORD')
            && project.hasProperty('TABLEXIA_RELEASE_KEY_ALIAS') && project.hasProperty('TABLEXIA_RELEASE_KEY_PASSWORD')) {
        signingConfigs {
            release {
                storeFile file(TABLEXIA_RELEASE_KEYSTORE)
                storePassword TABLEXIA_RELEASE_KEYSTORE_PASSWORD
                keyAlias TABLEXIA_RELEASE_KEY_ALIAS
                keyPassword TABLEXIA_RELEASE_KEY_PASSWORD
            }
        }

        buildTypes {
            release {
                signingConfig signingConfigs.release
            }
        }
    }
101 102 103 104
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

    command {
        // list manufacturer and model for all attached devices
        task('uninstallAllDevices') << {
            devices().each {
                applicationVariants.all { variant ->
                    if (variant.buildType.name.equals("debug")) {
                        variant.outputs.each { output ->
                            print "Uninstalling " + variant.name + "(" + variant.applicationId + ") from device " + it.id + ": .. "
                            [adb, '-s', it.id, 'shell', 'pm', 'uninstall', variant.applicationId].execute()
                            println "done"
                        }
                    }
                }
                // Uninstall test apk
                print "Uninstalling test(" + defaultConfig.testApplicationId + ") from device " + it.id + ": .. "
                [adb, '-s', it.id, 'shell', 'pm', 'uninstall', defaultConfig.testApplicationId].execute()
                println "done"
            }
        }
    }
126
}
127

128 129 130 131 132 133 134 135 136 137 138 139 140
// hack to add assets checksum to BuildConfig -> build config fields are generated at script startup
// -> calculated values from script runtime have to be added to file manually
tasks.whenTaskAdded { compileTask ->
    // select all "compile{BUILD_TYPE}Java" android tasks
    def matcher = (compileTask.name =~ 'compile(.+)Java')
    if (matcher.matches()) {
        String buildName = matcher[0][1]
        // without test tasks
        if (!buildName.contains("Test")) {

            // for all selected tasks create dependency task "checksum{BUILD_TYPE}Assets"
            task("checksum${buildName}Assets") {
                // calculated checksum is dependency
141
                dependsOn(':util:checksum:runChecksum')
142 143 144 145 146 147 148 149 150 151 152 153
                // wait for generating android sources
                dependsOn("generate${buildName}Sources")

                // modify BuildConfig.java -> write checksum in
                doLast{
                    ConfigurableFileTree cft = getProject().fileTree(BUILD_CONFIG_DIR + buildName.toLowerCase())
                    cft.include("**/BuildConfig.java")
                    Iterator<File> iter = cft.iterator()
                    while (iter.hasNext()){
                        File buildConfigFile = iter.next()
                        String buildConfigContent = buildConfigFile.getText(BUILD_CONFIG_ENCODING)

154
                        buildConfigContent = buildConfigContent.replace(rootProject.ext.assetsChecksumPattern, '\"' + getMapConvertedToString(rootProject.ext.assetsChecksum) + '\"')
155 156 157 158 159 160 161 162 163 164 165
                        buildConfigFile.write(buildConfigContent, BUILD_CONFIG_ENCODING)
                    }
                }
            }

            // set as dependency for "compile{BUILD_TYPE}Java" task
            compileTask.dependsOn("checksum${buildName}Assets")
        }
    }
}

166 167 168
// called every time gradle gets executed, takes the native dependencies of
// the natives configuration, and extracts them to the proper libs/ folders
// so they get packed with the APK.
169
task copyAndroidNatives() {
170 171 172 173 174 175
    file("libs/armeabi/").mkdirs();
    file("libs/armeabi-v7a/").mkdirs();
    file("libs/x86/").mkdirs();

    configurations.natives.files.each { jar ->
        def outputDir = null
176 177 178 179
        if (jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a")
        if (jar.name.endsWith("natives-armeabi.jar")) outputDir = file("libs/armeabi")
        if (jar.name.endsWith("natives-x86.jar")) outputDir = file("libs/x86")
        if (outputDir != null) {
180 181 182 183 184 185 186 187
            copy {
                from zipTree(jar)
                into outputDir
                include "*.so"
            }
        }
    }
}
188

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
task run(type: Exec) {
    def path
    def localProperties = project.file("../local.properties")
    if (localProperties.exists()) {
        Properties properties = new Properties()
        localProperties.withInputStream { instr ->
            properties.load(instr)
        }
        def sdkDir = properties.getProperty('sdk.dir')
        if (sdkDir) {
            path = sdkDir
        } else {
            path = "$System.env.ANDROID_HOME"
        }
    } else {
        path = "$System.env.ANDROID_HOME"
    }

    def adb = path + "/platform-tools/adb"
    commandLine "$adb", 'shell', 'am', 'start', '-n', 'cz.nic.tablexia.android/cz.nic.tablexia.android.AndroidLauncher'
}
210

211 212 213 214
// sets up the Android Idea project, using the old Ant based build.
idea {
    module {
        sourceDirs += file("src");
215
        scopes = [COMPILE: [plus: [project.configurations.compile]]]
216 217 218 219 220 221 222 223 224

        iml {
            withXml {
                def node = it.asNode()
                def builder = NodeBuilder.newInstance();
                builder.current = node;
                builder.component(name: "FacetManager") {
                    facet(type: "android", name: "Android") {
                        configuration {
225
                            option(name: "UPDATE_PROPERTY_FILES", value: "true")
226 227 228 229 230 231 232
                        }
                    }
                }
            }
        }
    }
}
Luboš Horáček's avatar
Luboš Horáček committed
233 234

spoon {
235 236 237 238 239
    debug = true
    failOnFailure = false
    //testSizes = ['small', 'medium']
    adbTimeout = 10*60
    failIfNoDeviceConnected = false
240
}