1 package dagger.hilt.android.plugin.util 2 3 import java.io.File 4 import java.io.InputStream 5 import java.util.Properties 6 import java.util.zip.ZipEntry 7 import java.util.zip.ZipInputStream 8 import org.gradle.api.Project 9 10 /* Checks if a file is a .class file. */ isClassFilenull11fun File.isClassFile() = this.isFile && this.extension == "class" 12 13 /* Checks if a Zip entry is a .class file. */ 14 fun ZipEntry.isClassFile() = !this.isDirectory && this.name.endsWith(".class") 15 16 /* Checks if a file is a .jar file. */ 17 fun File.isJarFile() = this.isFile && this.extension == "jar" 18 19 /* Executes the given [block] function over each [ZipEntry] in this [ZipInputStream]. */ 20 fun ZipInputStream.forEachZipEntry(block: (InputStream, ZipEntry) -> Unit) = use { 21 var inputEntry = nextEntry 22 while (inputEntry != null) { 23 block(this, inputEntry) 24 inputEntry = nextEntry 25 } 26 } 27 28 /* Gets the Android Sdk Path. */ Projectnull29fun Project.getSdkPath(): File { 30 val localPropsFile = rootProject.projectDir.resolve("local.properties") 31 if (localPropsFile.exists()) { 32 val localProps = Properties() 33 localPropsFile.inputStream().use { localProps.load(it) } 34 val localSdkDir = localProps["sdk.dir"]?.toString() 35 if (localSdkDir != null) { 36 val sdkDirectory = File(localSdkDir) 37 if (sdkDirectory.isDirectory) { 38 return sdkDirectory 39 } 40 } 41 } 42 return getSdkPathFromEnvironmentVariable() 43 } 44 getSdkPathFromEnvironmentVariablenull45private fun getSdkPathFromEnvironmentVariable(): File { 46 // Check for environment variables, in the order AGP checks. 47 listOf("ANDROID_HOME", "ANDROID_SDK_ROOT").forEach { 48 val envValue = System.getenv(it) 49 if (envValue != null) { 50 val sdkDirectory = File(envValue) 51 if (sdkDirectory.isDirectory) { 52 return sdkDirectory 53 } 54 } 55 } 56 // Only print the error for SDK ROOT since ANDROID_HOME is deprecated but we first check 57 // it because it is prioritized according to the documentation. 58 error("ANDROID_SDK_ROOT environment variable is not set") 59 } 60