Skip to content

Commit ad6a775

Browse files
committed
Add gradle.properties support
1 parent 30cc53c commit ad6a775

File tree

5 files changed

+93
-8
lines changed

5 files changed

+93
-8
lines changed

build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ task copyFilesToProjectTemeplate {
286286
from "$TEST_APP_PATH/build.gradle"
287287
into "$DIST_FRAMEWORK_PATH"
288288
}
289+
copy {
290+
from "$TEST_APP_PATH/paths.gradle"
291+
into "$DIST_FRAMEWORK_PATH"
292+
}
293+
copy {
294+
from "$TEST_APP_PATH/user_properties_reader.gradle"
295+
into "$DIST_FRAMEWORK_PATH"
296+
}
289297
copy {
290298
from "$TEST_APP_PATH/gradle"
291299
into "$DIST_FRAMEWORK_PATH/gradle"

test-app/app/build.gradle

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ import java.security.MessageDigest
3030

3131
apply plugin: "com.android.application"
3232

33-
def useKotlin = project.hasProperty("useKotlin") && project.ext.useKotlin=="true"
34-
if (useKotlin) {
33+
def enableKotlin = (project.hasProperty("useKotlin") && project.useKotlin == "true")
34+
35+
if (enableKotlin) {
3536
apply plugin: 'kotlin-android'
3637
apply plugin: 'kotlin-android-extensions'
3738
}
@@ -228,7 +229,7 @@ def setAppIdentifier = { ->
228229

229230
android {
230231

231-
if (useKotlin) {
232+
if (enableKotlin) {
232233
kotlinOptions {
233234
jvmTarget = '1.8'
234235
}
@@ -380,7 +381,7 @@ dependencies {
380381
}
381382

382383
def kotlinVersion = computeKotlinVersion()
383-
if (useKotlin) {
384+
if (enableKotlin) {
384385
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
385386
}
386387

test-app/build.gradle

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4-
def useKotlin = project.hasProperty("useKotlin") && project.ext.useKotlin == "true"
4+
5+
def initialize = { ->
6+
def userDir = "${rootProject.projectDir}/../.."
7+
apply from: "$rootDir/user_properties_reader.gradle"
8+
apply from: "$rootDir/paths.gradle"
9+
rootProject.ext.userDefinedGradleProperties = getUserProperties("${getAppResourcesPath(userDir)}/Android")
10+
}
11+
initialize()
12+
513
def computeKotlinVersion = { -> project.hasProperty("kotlinVersion") ? kotlinVersion : "1.3.41"
614
}
715
def kotlinVersion = computeKotlinVersion()
16+
817
repositories {
918
google()
1019
jcenter()
1120
}
1221
dependencies {
1322
classpath 'com.android.tools.build:gradle:3.5.0'
14-
if (useKotlin) {
15-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
16-
}
23+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
1724
}
1825
}
1926

@@ -22,6 +29,16 @@ allprojects {
2229
google()
2330
jcenter()
2431
}
32+
beforeEvaluate { project ->
33+
if (rootProject.hasProperty("userDefinedGradleProperties")) {
34+
rootProject.ext.userDefinedGradleProperties.each { entry ->
35+
def propertyName = entry.getKey()
36+
def propertyValue = entry.getValue()
37+
project.ext.set(propertyName, propertyValue)
38+
}
39+
}
40+
41+
}
2542
}
2643

2744
task clean(type: Delete) {

test-app/paths.gradle

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import groovy.json.JsonSlurper
2+
3+
ext.getAppPath = { userDir ->
4+
def relativePathToApp = "app"
5+
def nsConfigFile = file("$userDir/nsconfig.json")
6+
def nsConfig
7+
8+
if (nsConfigFile.exists()) {
9+
nsConfig = new JsonSlurper().parseText(nsConfigFile.getText("UTF-8"))
10+
}
11+
12+
if (nsConfig != null && nsConfig.appPath != null) {
13+
relativePathToApp = nsConfig.appPath
14+
}
15+
16+
return java.nio.file.Paths.get(userDir).resolve(relativePathToApp).toAbsolutePath()
17+
}
18+
19+
ext.getAppResourcesPath = { userDir ->
20+
def relativePathToAppResources
21+
def absolutePathToAppResources
22+
def nsConfigFile = file("$userDir/nsconfig.json")
23+
def nsConfig
24+
25+
if (nsConfigFile.exists()) {
26+
nsConfig = new JsonSlurper().parseText(nsConfigFile.getText("UTF-8"))
27+
}
28+
29+
if (nsConfig != null && nsConfig.appResourcesPath != null) {
30+
relativePathToAppResources = nsConfig.appResourcesPath
31+
absolutePathToAppResources = java.nio.file.Paths.get(userDir).resolve(relativePathToAppResources).toAbsolutePath()
32+
} else {
33+
absolutePathToAppResources = "${getAppPath(userDir)}/App_Resources"
34+
}
35+
36+
return absolutePathToAppResources
37+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def GRADLE_PROPERTIES_FILENAME = "gradle.properties"
2+
3+
def getFile = { dir, filename ->
4+
File file = new File("$dir$File.separator$filename")
5+
file?.exists() ? file : null
6+
}
7+
8+
def getPropertyFile = { dir ->
9+
return getFile(dir, GRADLE_PROPERTIES_FILENAME)
10+
}
11+
12+
ext.getUserProperties = { dir ->
13+
def file = getPropertyFile(dir)
14+
if (!file) {
15+
return null
16+
}
17+
18+
Properties properties = new Properties()
19+
properties.load(file.newInputStream())
20+
21+
return properties
22+
}

0 commit comments

Comments
 (0)