build.gradle 9.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (C) 2016 CZ.NIC, z.s.p.o. (http://www.nic.cz/)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

18 19 20 21 22
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'

23
android {
24
    buildToolsVersion "24.0.3"
25
    compileSdkVersion 21
26

27 28 29 30 31
    packagingOptions {
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
    }

32 33 34
    lintOptions {
        abortOnError false
    }
35

36
    defaultConfig {
37
        targetSdkVersion 21
38 39
        versionName tablexiaVersionName
        versionCode tablexiaVersionCode
40 41
        applicationId rootProject.applicationBaseId
        testApplicationId rootProject.applicationBaseId + ".test"
42 43 44
	    jackOptions {
            enabled true
	    }
45
    }
46 47 48

    buildTypes {
        debug {
49
            applicationIdSuffix rootProject.applicationIdBranch + rootProject.applicationIdDebugSuffix
50
            buildConfigField ASSETS_CHECKSUM_FIELD_TYPE, ASSETS_CHECKSUM_FIELD_NAME, rootProject.ext.assetsChecksumPattern
51
            resValue "string", "app_name", "${tablexiaAppName}"
52
            resValue "string", "sentry_dsn", project.hasProperty('TABLEXIA_SENTRY_DSN_DEBUG') ? "$TABLEXIA_SENTRY_DSN_DEBUG" : "$project.sentryDSNFallbackValue";
53 54 55
        }
        release {
            debuggable false
56
            applicationIdSuffix rootProject.applicationIdBranch
57
            buildConfigField ASSETS_CHECKSUM_FIELD_TYPE, ASSETS_CHECKSUM_FIELD_NAME, rootProject.ext.assetsChecksumPattern
58
            resValue "string", "app_name", "${tablexiaAppName}"
59
            resValue "string", "sentry_dsn", project.hasProperty('TABLEXIA_SENTRY_DSN_RELEASE') ? "$TABLEXIA_SENTRY_DSN_RELEASE" : "$project.sentryDSNFallbackValue";
60 61 62 63
        }
        devel.initWith(buildTypes.debug)
        devel {
            applicationIdSuffix rootProject.applicationIdDevelSuffix
64
            buildConfigField ASSETS_CHECKSUM_FIELD_TYPE, ASSETS_CHECKSUM_FIELD_NAME, rootProject.ext.assetsChecksumPattern
65
            resValue "string", "app_name", "${tablexiaAppName}"
66
            resValue "string", "sentry_dsn", "$project.sentryDSNFallbackValue";
67 68 69
        }
    }

70 71 72
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
73
            res.srcDirs = ['res/main']
74
            assets.srcDirs = ['assets']
75
            java.srcDir file('src/main/java')
76
            jniLibs.srcDirs = ['libs']
77 78 79 80 81
        }
        test {
            java.srcDir file('src/test/java')
        }

82 83 84 85 86 87
        release {
            res.srcDirs = ['res/release']
        }
        debug {
            res.srcDirs = ['res/debug']
        }
88 89 90
        devel {
            res.srcDirs = ['res/devel']
        }
91 92
    }

93
    assemble.dependsOn = ['assembleRelease', 'assembleDebug']
94 95

    applicationVariants.all { variant ->
96
        if (!variant.buildType.name.equals("devel")) {
97 98
            variant.outputs.each { output ->
                def file = output.outputFile
99
                output.outputFile = new File(file.parent, file.name.replace(".apk", "-" + tablexiaVersionName + ".apk").replace("android", tablexiaAppName))
100
            }
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        }
    }

    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
            }
        }
    }
121
    compileOptions {
122 123
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
124
    }
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

    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"
            }
        }
    }
146
}
147

148 149 150 151 152 153 154 155 156 157 158 159 160
// 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
161
                dependsOn(':util:checksum:runChecksum')
162 163 164 165 166 167 168 169 170 171 172 173
                // 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)

174
                        buildConfigContent = buildConfigContent.replace(rootProject.ext.assetsChecksumPattern, '\"' + getMapConvertedToString(rootProject.ext.assetsChecksum) + '\"')
175 176 177 178 179 180 181 182 183 184 185
                        buildConfigFile.write(buildConfigContent, BUILD_CONFIG_ENCODING)
                    }
                }
            }

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

186 187 188
// 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.
189
task copyAndroidNatives() {
190 191 192 193 194 195
    file("libs/armeabi/").mkdirs();
    file("libs/armeabi-v7a/").mkdirs();
    file("libs/x86/").mkdirs();

    configurations.natives.files.each { jar ->
        def outputDir = null
196 197 198 199
        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) {
200 201 202 203 204 205 206 207
            copy {
                from zipTree(jar)
                into outputDir
                include "*.so"
            }
        }
    }
}
208

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
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'
}
230

231 232 233 234
// sets up the Android Idea project, using the old Ant based build.
idea {
    module {
        sourceDirs += file("src");
235
        scopes = [COMPILE: [plus: [project.configurations.compile]]]
236 237 238 239 240 241 242 243 244

        iml {
            withXml {
                def node = it.asNode()
                def builder = NodeBuilder.newInstance();
                builder.current = node;
                builder.component(name: "FacetManager") {
                    facet(type: "android", name: "Android") {
                        configuration {
245
                            option(name: "UPDATE_PROPERTY_FILES", value: "true")
246 247 248 249 250 251 252
                        }
                    }
                }
            }
        }
    }
}