From 810cf4dee86d33cda5be1cc2d9bd84b6be90a12a Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Wed, 8 Sep 2021 00:45:15 +0800 Subject: [PATCH] Move config to buildSrc --- app/build.gradle.kts | 10 +++- app/shared/build.gradle.kts | 3 +- build.gradle.kts | 98 --------------------------------- buildSrc/build.gradle.kts | 5 ++ buildSrc/src/main/java/Setup.kt | 68 +++++++++++++++++++++++ native/build.gradle.kts | 5 +- settings.gradle.kts | 10 ++++ stub/build.gradle.kts | 24 +++----- 8 files changed, 103 insertions(+), 120 deletions(-) create mode 100644 buildSrc/src/main/java/Setup.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 742804a21..4e027a474 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -20,6 +20,8 @@ kapt { } } +setupApp() + android { defaultConfig { applicationId = "com.topjohnwu.magisk" @@ -30,7 +32,7 @@ android { } buildTypes { - getByName("release") { + release { isMinifyEnabled = true isShrinkResources = true proguardFiles("proguard-rules.pro") @@ -135,7 +137,9 @@ val syncResources by tasks.registering(Sync::class) { } } -tasks["preBuild"]?.dependsOn(syncResources) +tasks.preBuild { + dependsOn(syncResources) +} android.applicationVariants.all { val keysDir = rootProject.file("tools/keys") @@ -203,7 +207,7 @@ dependencies { implementation("androidx.room:room-ktx:${vRoom}") kapt("androidx.room:room-compiler:${vRoom}") - val vNav: String by rootProject.extra + val vNav = "2.4.0-alpha07" implementation("androidx.navigation:navigation-fragment-ktx:${vNav}") implementation("androidx.navigation:navigation-ui-ktx:${vNav}") diff --git a/app/shared/build.gradle.kts b/app/shared/build.gradle.kts index ad8a82bc1..ce53671e1 100644 --- a/app/shared/build.gradle.kts +++ b/app/shared/build.gradle.kts @@ -2,9 +2,10 @@ plugins { id("com.android.library") } +setupCommon() + android { defaultConfig { - vectorDrawables.useSupportLibrary = true consumerProguardFiles("proguard-rules.pro") } } diff --git a/build.gradle.kts b/build.gradle.kts index 4b2fe765d..ab4e178a9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,105 +1,7 @@ -import com.android.build.gradle.BaseExtension - plugins { id("MagiskPlugin") } -// Top-level build file where you can add configuration options common to all sub-projects/modules. - -buildscript { - repositories { - google() - mavenCentral() - } - - val vNav = "2.4.0-alpha06" - extra["vNav"] = vNav - - dependencies { - classpath("com.android.tools.build:gradle:7.0.2") - classpath(kotlin("gradle-plugin", version = "1.5.30")) - classpath("androidx.navigation:navigation-safe-args-gradle-plugin:${vNav}") - - // NOTE: Do not place your application dependencies here; they belong - // in the individual module build.gradle files - } -} - tasks.register("clean", Delete::class) { delete(rootProject.buildDir) } - -fun Project.android(configuration: BaseExtension.() -> Unit) = - extensions.getByName("android").configuration() - -subprojects { - repositories { - google() - mavenCentral() - maven { url = uri("https://jitpack.io") } - } - - afterEvaluate { - if (plugins.hasPlugin("com.android.library") || - plugins.hasPlugin("com.android.application") - ) { - android { - compileSdkVersion(31) - buildToolsVersion = "31.0.0" - ndkPath = "${System.getenv("ANDROID_SDK_ROOT")}/ndk/magisk" - - defaultConfig { - if (minSdk == null) - minSdk = 21 - targetSdk = 31 - } - - compileOptions { - sourceCompatibility = JavaVersion.VERSION_11 - targetCompatibility = JavaVersion.VERSION_11 - } - } - } - - if (plugins.hasPlugin("java")) { - tasks.withType { - // If building with JDK 9+, we need additional flags to generate compatible bytecode - if (JavaVersion.current() > JavaVersion.VERSION_1_8) { - options.compilerArgs.addAll(listOf("--release", "8")) - } - } - } - - if (name == "app" || name == "stub") { - android { - signingConfigs { - create("config") { - Config["keyStore"]?.also { - storeFile = rootProject.file(it) - storePassword = Config["keyStorePass"] - keyAlias = Config["keyAlias"] - keyPassword = Config["keyPass"] - } - } - } - - buildTypes { - signingConfigs.getByName("config").also { - getByName("debug") { - signingConfig = if (it.storeFile?.exists() == true) it - else signingConfigs.getByName("debug") - } - getByName("release") { - signingConfig = if (it.storeFile?.exists() == true) it - else signingConfigs.getByName("debug") - } - } - } - - lintOptions { - disable += "MissingTranslation" - } - } - } - } -} diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 7c3b72c8d..12fad4b73 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -2,6 +2,7 @@ plugins { `kotlin-dsl` } repositories { + google() mavenCentral() } @@ -15,5 +16,9 @@ gradlePlugin { } dependencies { + implementation(kotlin("gradle-plugin", "1.5.30")) + implementation("com.android.tools.build:gradle:7.0.2") + implementation("androidx.navigation:navigation-safe-args-gradle-plugin:2.4.0-alpha07") + implementation("io.michaelrocks:paranoid-gradle-plugin:0.3.5") implementation("org.eclipse.jgit:org.eclipse.jgit:5.12.0.202106070339-r") } diff --git a/buildSrc/src/main/java/Setup.kt b/buildSrc/src/main/java/Setup.kt new file mode 100644 index 000000000..821bd2b26 --- /dev/null +++ b/buildSrc/src/main/java/Setup.kt @@ -0,0 +1,68 @@ +import com.android.build.gradle.BaseExtension +import org.gradle.api.Action +import org.gradle.api.JavaVersion +import org.gradle.api.Project +import org.gradle.api.tasks.compile.JavaCompile +import org.gradle.kotlin.dsl.withType + +private fun Project.android(configure: Action) = extensions.configure("android", configure) + +fun Project.setupCommon() { + android { + compileSdkVersion(31) + buildToolsVersion = "31.0.0" + ndkPath = "${System.getenv("ANDROID_SDK_ROOT")}/ndk/magisk" + + defaultConfig { + minSdk = 21 + targetSdk = 31 + } + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } + } + + if (plugins.hasPlugin("java")) { + tasks.withType { + // If building with JDK 9+, we need additional flags to generate compatible bytecode + if (JavaVersion.current() > JavaVersion.VERSION_1_8) { + options.compilerArgs.addAll(listOf("--release", "8")) + } + } + } +} + +fun Project.setupApp() { + setupCommon() + android { + signingConfigs { + create("config") { + Config["keyStore"]?.also { + storeFile = rootProject.file(it) + storePassword = Config["keyStorePass"] + keyAlias = Config["keyAlias"] + keyPassword = Config["keyPass"] + } + } + } + + buildTypes { + signingConfigs.getByName("config").also { + getByName("debug") { + signingConfig = if (it.storeFile?.exists() == true) it + else signingConfigs.getByName("debug") + } + getByName("release") { + signingConfig = if (it.storeFile?.exists() == true) it + else signingConfigs.getByName("debug") + } + } + } + + lintOptions { + disable += "MissingTranslation" + } + } +} diff --git a/native/build.gradle.kts b/native/build.gradle.kts index ccbcc3182..1c89d22b7 100644 --- a/native/build.gradle.kts +++ b/native/build.gradle.kts @@ -2,8 +2,9 @@ plugins { id("com.android.library") } -android { +setupCommon() +android { externalNativeBuild { ndkBuild { path("jni/Android.mk") @@ -15,7 +16,7 @@ android { ndkBuild { // Pass arguments to ndk-build. arguments("B_MAGISK=1", "B_INIT=1", "B_BOOT=1", "B_TEST=1", - "MAGISK_DEBUG=1", "MAGISK_VERSION=debug", "MAGISK_VER_CODE=INT_MAX") + "MAGISK_DEBUG=1", "MAGISK_VERSION=debug", "MAGISK_VER_CODE=INT_MAX") } } } diff --git a/settings.gradle.kts b/settings.gradle.kts index f84d1cf00..774ef403e 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1 +1,11 @@ +@Suppress("UnstableApiUsage") +dependencyResolutionManagement { + repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) + repositories { + google() + mavenCentral() + maven("https://jitpack.io") + } +} +rootProject.name = "Magisk" include(":app", ":app:shared", ":native", ":stub") diff --git a/stub/build.gradle.kts b/stub/build.gradle.kts index 4e0137b5f..9eb7c4d9a 100644 --- a/stub/build.gradle.kts +++ b/stub/build.gradle.kts @@ -1,5 +1,3 @@ - -import io.michaelrocks.paranoid.plugin.ParanoidExtension import org.gradle.internal.os.OperatingSystem import java.io.OutputStream import java.io.PrintStream @@ -7,24 +5,16 @@ import java.nio.file.Paths plugins { id("com.android.application") + id("io.michaelrocks.paranoid") } -buildscript { - repositories { - mavenCentral() - } - dependencies { - classpath("io.michaelrocks:paranoid-gradle-plugin:0.3.5") - } -} - -apply(plugin = "io.michaelrocks.paranoid") - -extensions.configure("paranoid") { +paranoid { obfuscationSeed = if (RAND_SEED != 0) RAND_SEED else null includeSubprojects = true } +setupApp() + android { val canary = !Config.version.contains(".") @@ -40,7 +30,7 @@ android { } buildTypes { - getByName("release") { + release { isMinifyEnabled = true isShrinkResources = false proguardFiles("proguard-rules.pro") @@ -63,7 +53,9 @@ val ensureManifest by tasks.registering { } } -tasks["preBuild"]?.dependsOn(ensureManifest) +tasks.preBuild { + dependsOn(ensureManifest) +} android.applicationVariants.all { val manifest = file("src/main/AndroidManifest.xml")