diff --git a/app-common-io/build.gradle.kts b/app-common-io/build.gradle.kts index 4d690a428..919947c4c 100644 --- a/app-common-io/build.gradle.kts +++ b/app-common-io/build.gradle.kts @@ -4,14 +4,15 @@ plugins { id("kotlin-android") id("kotlin-kapt") id("kotlin-parcelize") + id("projectConfig") } apply(plugin = "dagger.hilt.android.plugin") android { - namespace = "${ProjectConfig.packageName}.common.io" + namespace = "${projectConfig.packageName}.common.io" - setupLibraryDefaults() + setupLibraryDefaults(projectConfig) setupModuleBuildTypes() diff --git a/app-common-pkgs/build.gradle.kts b/app-common-pkgs/build.gradle.kts index 8390057d9..54ebb18ed 100644 --- a/app-common-pkgs/build.gradle.kts +++ b/app-common-pkgs/build.gradle.kts @@ -4,14 +4,15 @@ plugins { id("kotlin-android") id("kotlin-kapt") id("kotlin-parcelize") + id("projectConfig") } apply(plugin = "dagger.hilt.android.plugin") android { - namespace = "${ProjectConfig.packageName}.common.pkgs" + namespace = "${projectConfig.packageName}.common.pkgs" - setupLibraryDefaults() + setupLibraryDefaults(projectConfig) setupModuleBuildTypes() diff --git a/app-common-root/build.gradle.kts b/app-common-root/build.gradle.kts index bf07c4bbd..c0d0dae2b 100644 --- a/app-common-root/build.gradle.kts +++ b/app-common-root/build.gradle.kts @@ -4,14 +4,15 @@ plugins { id("kotlin-android") id("kotlin-kapt") id("kotlin-parcelize") + id("projectConfig") } apply(plugin = "dagger.hilt.android.plugin") android { - namespace = "${ProjectConfig.packageName}.common.root" + namespace = "${projectConfig.packageName}.common.root" - setupLibraryDefaults() + setupLibraryDefaults(projectConfig) setupModuleBuildTypes() diff --git a/app-common-shell/build.gradle.kts b/app-common-shell/build.gradle.kts index 01a073393..fc4a12777 100644 --- a/app-common-shell/build.gradle.kts +++ b/app-common-shell/build.gradle.kts @@ -4,14 +4,15 @@ plugins { id("kotlin-android") id("kotlin-kapt") id("kotlin-parcelize") + id("projectConfig") } apply(plugin = "dagger.hilt.android.plugin") android { - namespace = "${ProjectConfig.packageName}.common.shell" + namespace = "${projectConfig.packageName}.common.shell" - setupLibraryDefaults() + setupLibraryDefaults(projectConfig) setupModuleBuildTypes() diff --git a/app-common-shizuku/build.gradle.kts b/app-common-shizuku/build.gradle.kts index 3323c3e18..6a870974a 100644 --- a/app-common-shizuku/build.gradle.kts +++ b/app-common-shizuku/build.gradle.kts @@ -4,14 +4,15 @@ plugins { id("kotlin-android") id("kotlin-kapt") id("kotlin-parcelize") + id("projectConfig") } apply(plugin = "dagger.hilt.android.plugin") android { - namespace = "${ProjectConfig.packageName}.common.shizuku" + namespace = "${projectConfig.packageName}.common.shizuku" - setupLibraryDefaults() + setupLibraryDefaults(projectConfig) setupModuleBuildTypes() diff --git a/app-common-test/build.gradle.kts b/app-common-test/build.gradle.kts index 65985c502..cd7267d36 100644 --- a/app-common-test/build.gradle.kts +++ b/app-common-test/build.gradle.kts @@ -3,12 +3,13 @@ plugins { id("org.jetbrains.kotlin.android") id("kotlin-android") id("kotlin-kapt") + id("projectConfig") } android { - namespace = "eu.darken.sdmse.common" + namespace = "${projectConfig.packageName}.common" - setupLibraryDefaults() + setupLibraryDefaults(projectConfig) setupModuleBuildTypes() diff --git a/app-common/build.gradle.kts b/app-common/build.gradle.kts index bc96a357f..9cbb946cf 100644 --- a/app-common/build.gradle.kts +++ b/app-common/build.gradle.kts @@ -4,14 +4,15 @@ plugins { id("kotlin-android") id("kotlin-kapt") id("kotlin-parcelize") + id("projectConfig") } apply(plugin = "dagger.hilt.android.plugin") android { - namespace = "${ProjectConfig.packageName}.common" + namespace = "${projectConfig.packageName}.common" - setupLibraryDefaults() + setupLibraryDefaults(projectConfig) setupModuleBuildTypes() diff --git a/app/build.gradle.kts b/app/build.gradle.kts index a84030fee..392221dd9 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -3,6 +3,7 @@ plugins { id("kotlin-android") id("kotlin-kapt") id("kotlin-parcelize") + id("projectConfig") } apply(plugin = "dagger.hilt.android.plugin") apply(plugin = "androidx.navigation.safeargs.kotlin") @@ -10,28 +11,28 @@ apply(plugin = "androidx.navigation.safeargs.kotlin") val commitHashProvider = providers.of(CommitHashValueSource::class) {} android { - compileSdk = ProjectConfig.compileSdk + compileSdk = projectConfig.compileSdk defaultConfig { - namespace = ProjectConfig.packageName + namespace = projectConfig.packageName - minSdk = ProjectConfig.minSdk - targetSdk = ProjectConfig.targetSdk + minSdk = projectConfig.minSdk + targetSdk = projectConfig.targetSdk - versionCode = ProjectConfig.Version.code - versionName = ProjectConfig.Version.name + versionCode = projectConfig.version.code.toInt() + versionName = projectConfig.version.name testInstrumentationRunner = "eu.darken.sdmse.HiltTestRunner" - buildConfigField("String", "PACKAGENAME", "\"${ProjectConfig.packageName}\"") + buildConfigField("String", "PACKAGENAME", "\"${projectConfig.packageName}\"") buildConfigField("String", "GITSHA", "\"${commitHashProvider.get()}\"") - buildConfigField("String", "VERSION_CODE", "\"${ProjectConfig.Version.code}\"") - buildConfigField("String", "VERSION_NAME", "\"${ProjectConfig.Version.name}\"") + buildConfigField("String", "VERSION_CODE", "\"${projectConfig.version.code}\"") + buildConfigField("String", "VERSION_NAME", "\"${projectConfig.version.name}\"") } signingConfigs { - val basePath = File(System.getProperty("user.home"), ".appconfig/${ProjectConfig.packageName}") + val basePath = File(System.getProperty("user.home"), ".appconfig/${projectConfig.packageName}") create("releaseFoss") { setupCredentials(File(basePath, "signing-foss.properties")) } @@ -95,7 +96,7 @@ android { val variantName: String = variantOutputImpl.name if (listOf("release", "beta").any { variantName.lowercase().contains(it) }) { - val outputFileName = ProjectConfig.packageName + + val outputFileName = projectConfig.packageName + "-v${defaultConfig.versionName}-${defaultConfig.versionCode}" + "-${variantName.uppercase()}-${commitHashProvider.get()}.apk" diff --git a/build.gradle.kts b/build.gradle.kts index 056efd366..53bf222a5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,3 +1,7 @@ +plugins { + id("projectConfig") +} + buildscript { repositories { google() diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index e9e765cd9..cc6a0081a 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -3,6 +3,15 @@ plugins { `java-library` } +gradlePlugin { + plugins { + create("projectConfigPlugin") { + id = "projectConfig" + implementationClass = "ProjectConfigPlugin" + } + } +} + repositories { google() mavenCentral() diff --git a/buildSrc/src/main/java/CommitHashValueSource.kt b/buildSrc/src/main/java/CommitHashValueSource.kt new file mode 100644 index 000000000..38e47ff56 --- /dev/null +++ b/buildSrc/src/main/java/CommitHashValueSource.kt @@ -0,0 +1,20 @@ +import org.gradle.api.provider.ValueSource +import org.gradle.api.provider.ValueSourceParameters +import org.gradle.process.ExecOperations +import java.io.ByteArrayOutputStream +import java.nio.charset.Charset +import javax.inject.Inject + +abstract class CommitHashValueSource : ValueSource { + @get:Inject + abstract val execOperations: ExecOperations + + override fun obtain(): String { + val output = ByteArrayOutputStream() + execOperations.exec { + commandLine("git", "rev-parse", "--short", "HEAD") + standardOutput = output + } + return String(output.toByteArray(), Charset.defaultCharset()).trim() + } +} \ No newline at end of file diff --git a/buildSrc/src/main/java/ProjectConfigPlugin.kt b/buildSrc/src/main/java/ProjectConfigPlugin.kt new file mode 100644 index 000000000..cb943d30f --- /dev/null +++ b/buildSrc/src/main/java/ProjectConfigPlugin.kt @@ -0,0 +1,56 @@ +import org.gradle.api.Plugin +import org.gradle.api.Project +import java.io.File +import java.io.FileInputStream +import java.util.Properties + +open class ProjectConfig { + val packageName = "eu.darken.sdmse" + val minSdk = 26 + + val compileSdk = 34 + val targetSdk = 34 + + lateinit var version: Version + + override fun toString(): String { + return "ProjectConfig($packageName, min=$minSdk, compile=$compileSdk, target=$targetSdk, version=$version)" + } + + fun init(project: Project) { + val versionProperties = Properties().apply { + val propsPath = File(project.rootDir, "version.properties") + println("Version: From $propsPath:") + load(FileInputStream(propsPath)) + println("$this") + } + version = Version( + major = versionProperties.getProperty("project.versioning.major").toInt(), + minor = versionProperties.getProperty("project.versioning.minor").toInt(), + patch = versionProperties.getProperty("project.versioning.patch").toInt(), + build = versionProperties.getProperty("project.versioning.build").toInt(), + type = versionProperties.getProperty("project.versioning.type"), + ) + } + + data class Version( + val major: Int, + val minor: Int, + val patch: Int, + val build: Int, + val type: String, + ) { + val name: String + get() = "${major}.${minor}.${patch}-$type${build}" + val code: Long + get() = major * 10000000 + minor * 100000 + patch * 1000 + build * 10L + } +} + +class ProjectConfigPlugin : Plugin { + override fun apply(project: Project) { + val extension = project.extensions.create("projectConfig", ProjectConfig::class.java) + extension.init(project) + project.afterEvaluate { println("ProjectConfigPlugin loaded: $extension") } + } +} \ No newline at end of file diff --git a/buildSrc/src/main/java/ProjectConfig.kt b/buildSrc/src/main/java/ProjectExtensions.kt similarity index 73% rename from buildSrc/src/main/java/ProjectConfig.kt rename to buildSrc/src/main/java/ProjectExtensions.kt index 7a000c245..ffed122bc 100644 --- a/buildSrc/src/main/java/ProjectConfig.kt +++ b/buildSrc/src/main/java/ProjectExtensions.kt @@ -1,46 +1,18 @@ -import com.android.build.api.dsl.Packaging import com.android.build.gradle.BaseExtension import com.android.build.gradle.LibraryExtension import org.gradle.api.Action import org.gradle.api.JavaVersion -import org.gradle.api.provider.ValueSource -import org.gradle.api.provider.ValueSourceParameters +import org.gradle.api.Project import org.gradle.api.tasks.testing.Test import org.gradle.api.tasks.testing.TestDescriptor import org.gradle.api.tasks.testing.TestListener import org.gradle.api.tasks.testing.TestResult import org.gradle.api.tasks.testing.logging.TestExceptionFormat import org.gradle.api.tasks.testing.logging.TestLogEvent -import org.gradle.process.ExecOperations import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions -import java.io.ByteArrayOutputStream import java.io.File import java.io.FileInputStream -import java.nio.charset.Charset -import java.util.* -import javax.inject.Inject - -object ProjectConfig { - const val packageName = "eu.darken.sdmse" - - const val minSdk = 26 - const val compileSdk = 34 - const val targetSdk = 34 - - object Version { - val versionProperties = Properties().apply { - load(FileInputStream(File("version.properties"))) - } - val major = versionProperties.getProperty("project.versioning.major").toInt() - val minor = versionProperties.getProperty("project.versioning.minor").toInt() - val patch = versionProperties.getProperty("project.versioning.patch").toInt() - val build = versionProperties.getProperty("project.versioning.build").toInt() - val type = versionProperties.getProperty("project.versioning.type") - - val name = "${major}.${minor}.${patch}-$type${build}" - val code = major * 10000000 + minor * 100000 + patch * 1000 + build * 10 - } -} +import java.util.Properties /** * Configures the [kotlinOptions][org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions] extension. @@ -48,15 +20,18 @@ object ProjectConfig { private fun LibraryExtension.kotlinOptions(configure: Action): Unit = (this as org.gradle.api.plugins.ExtensionAware).extensions.configure("kotlinOptions", configure) -fun LibraryExtension.setupLibraryDefaults() { - compileSdk = ProjectConfig.compileSdk +fun LibraryExtension.setupLibraryDefaults(projectConfig: ProjectConfig) { + compileSdk = projectConfig.compileSdk defaultConfig { - minSdk = ProjectConfig.minSdk + minSdk = projectConfig.minSdk testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } } +val Project.projectConfig: ProjectConfig + get() = extensions.findByType(ProjectConfig::class.java)!! + fun com.android.build.api.dsl.CommonExtension< com.android.build.api.dsl.LibraryBuildFeatures, com.android.build.api.dsl.LibraryBuildType, @@ -91,7 +66,8 @@ fun BaseExtension.setupKotlinOptions() { "-Xopt-in=kotlin.time.ExperimentalTime", "-Xopt-in=kotlin.RequiresOptIn", "-Xjvm-default=all", - "-XXLanguage:+DataObjects" + "-XXLanguage:+DataObjects", + "-Xcontext-receivers" ) } } @@ -164,17 +140,3 @@ fun Test.setupTestLogging() { }) } } - -abstract class CommitHashValueSource : ValueSource { - @get:Inject - abstract val execOperations: ExecOperations - - override fun obtain(): String { - val output = ByteArrayOutputStream() - execOperations.exec { - commandLine("git", "rev-parse", "--short", "HEAD") - standardOutput = output - } - return String(output.toByteArray(), Charset.defaultCharset()).trim() - } -} \ No newline at end of file