// deployment.properties is the only source for app deployment addresses. def configFile = rootProject.file(project.findProperty('deploymentConfig') ?: 'deployment.properties') if (!configFile.isFile()) throw new GradleException("Deployment config not found: ${configFile}") def deployment = new Properties() configFile.withReader('UTF-8') { deployment.load(it) } def required = { String key -> def value = deployment.getProperty(key, '').trim() if (!value) throw new GradleException("${configFile.name}: ${key} must not be empty") value } def validateHost = { String host -> // Host names and IPv4 only; reject URL syntax and XML metacharacters. if (!(host ==~ /[A-Za-z0-9](?:[A-Za-z0-9.-]*[A-Za-z0-9])?/)) { throw new GradleException("Invalid deployment host '${host}': use an IP or domain without scheme, port or path") } host.toLowerCase(java.util.Locale.ROOT) } def baseUrl = required('CLOUD_BASE_URL') def baseUri try { baseUri = new URI(baseUrl) } catch (Exception e) { throw new GradleException('CLOUD_BASE_URL must be a valid http/https URL', e) } if (!(baseUri.scheme in ['http', 'https']) || !baseUri.host || baseUri.userInfo || baseUri.query || baseUri.fragment || baseUri.port == 0 || baseUri.port > 65535) { throw new GradleException('CLOUD_BASE_URL must be an http/https URL without credentials, query or fragment') } def platformHost = validateHost(baseUri.host) def fallbackHost = deployment.getProperty('CLOUD_FALLBACK_HOST', '').trim() if (fallbackHost) fallbackHost = validateHost(fallbackHost) def httpHosts = ([platformHost, fallbackHost] + deployment.getProperty('HTTP_EXTRA_HOSTS', '').split(',').toList()) .collect { it.trim() }.findAll { it }.collect { validateHost(it) }.unique() def rtmpPortText = required('RTMP_PORT') if (!rtmpPortText.isInteger() || !(rtmpPortText.toInteger() in 1..65535)) { throw new GradleException('RTMP_PORT must be between 1 and 65535') } def rtmpApp = required('RTMP_DEFAULT_APP') if (!(rtmpApp ==~ /[A-Za-z0-9_-]+/)) throw new GradleException('RTMP_DEFAULT_APP must be a single app name') android.defaultConfig { buildConfigField 'String', 'CLOUD_BASE_URL', groovy.json.JsonOutput.toJson(baseUrl) buildConfigField 'String', 'CLOUD_PLATFORM_HOST', groovy.json.JsonOutput.toJson(platformHost) buildConfigField 'String', 'CLOUD_FALLBACK_HOST', groovy.json.JsonOutput.toJson(fallbackHost) buildConfigField 'int', 'CLOUD_RTMP_PORT', rtmpPortText.toInteger().toString() buildConfigField 'String', 'CLOUD_RTMP_DEFAULT_APP', groovy.json.JsonOutput.toJson(rtmpApp) } def generatedRes = layout.buildDirectory.dir('generated/res/deployment') android.sourceSets.main.res.srcDir(generatedRes) def generateNetworkConfig = tasks.register('generateDeploymentNetworkConfig') { inputs.file(configFile) inputs.property('httpHosts', httpHosts) outputs.dir(generatedRes) doLast { def output = new File(generatedRes.get().asFile, 'xml/network_security_config.xml') output.parentFile.mkdirs() output.setText(''' ''' + httpHosts.collect { ' ' + it + '' }.join('\n') + ''' ''', 'UTF-8') } } tasks.named('preBuild').configure { dependsOn(generateNetworkConfig) }