Skip to content

Commit 6201761

Browse files
committed
Detect Android
1 parent ef89220 commit 6201761

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

gradle/detekt-android-sdk.gradle

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// https://github.com/mockk/mockk
2+
def pathExists(String envPath) {
3+
if (envPath != null && !envPath.empty) {
4+
return file(envPath).exists()
5+
}
6+
7+
return false
8+
}
9+
10+
def setPropPath(Properties props, String prop, String path) {
11+
if (!file(path).isDirectory()) return
12+
if (props.hasProperty(prop)) return
13+
props[prop] = path
14+
}
15+
16+
def propsPath = file("../local.properties")
17+
def props = new Properties()
18+
19+
if (propsPath.isFile()) {
20+
props.load(new FileReader(propsPath))
21+
}
22+
23+
ext.hasAndroidSdk = false
24+
String newAndroidSdkPath = null
25+
if (props.containsKey("sdk.dir")) {
26+
ext.hasAndroidSdk = true
27+
} else if (pathExists(System.env.ANDROID_HOME)) {
28+
newAndroidSdkPath = System.env.ANDROID_HOME
29+
} else if (pathExists(System.env.ANDROID_SDK_ROOT)) {
30+
newAndroidSdkPath = System.env.ANDROID_SDK_ROOT
31+
} else if (pathExists(System.env.HOME + "/Android/Sdk")) {
32+
newAndroidSdkPath = System.env.HOME + "/Android/Sdk"
33+
}
34+
35+
if (newAndroidSdkPath != null) {
36+
setPropPath(props, "sdk.dir", newAndroidSdkPath)
37+
38+
def oldProps = new Properties(props)
39+
40+
if (props != oldProps) {
41+
def writer = new FileWriter(propsPath)
42+
try {
43+
props.store(writer, "generated by 'detect-android-sdk.gradle'")
44+
} finally {
45+
writer.close()
46+
}
47+
}
48+
49+
ext.hasAndroidSdk = true
50+
}
51+
52+
if (ext.hasAndroidSdk != true){
53+
println '''\
54+
[WARNING] Skipping build of Android related modules because Android SDK has not been found!
55+
Define Android SDK location in 'local.properties' file or with ANDROID_HOME
56+
environment variable to build Android modules
57+
'''
58+
}

settings.gradle.kts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ gradleEnterprise {
3131

3232
rootProject.name = "kotlin-libraries-playground"
3333

34-
34+
apply(from= "gradle/detekt-android-sdk.gradle")
35+
val hasAndroidSdk: Boolean by extra
36+
if (hasAndroidSdk) include("android")
3537
include("kotlin-jvm")
3638
include("kotlin-testing")
3739
include("kotlin-codegen")

0 commit comments

Comments
 (0)