Skip to content
This repository was archived by the owner on Jul 16, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion adapter/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import com.android.build.gradle.LibraryExtension

setupBase<LibraryExtension>(Module.Adapter)
setupBase<LibraryExtension>(LibModule.Adapter)

dependencies {
apis(
Expand Down
10 changes: 5 additions & 5 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
setupApp(appPackageName, appName)
setupApp(AppModule.App)

dependencies {
implementations(
// projects
project(Module.Login.moduleName),
project(Module.Main.moduleName),
// project(Module.Map.moduleName),
project(Module.Web.moduleName)
project(LibModule.Login.moduleName),
project(LibModule.Main.moduleName),
project(LibModule.Web.moduleName)
)
debugImplementations(Libs.leakCanary)
}
2 changes: 1 addition & 1 deletion base/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import com.android.build.gradle.LibraryExtension

setupBase<LibraryExtension>()
setupBase<LibraryExtension>(LibModule.Base)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inject module name now in setupBase.


dependencies {
apis(
Expand Down
37 changes: 15 additions & 22 deletions buildSrc/src/main/kotlin/Enums.kt
Original file line number Diff line number Diff line change
@@ -1,39 +1,32 @@
@file:Suppress("SpellCheckingInspection")

enum class Module(val tag: String, val runAlone: Boolean = false) {
// TODO: Make Module sealed
interface Module {
val tag: String
}
Comment on lines +3 to +6
Copy link
Owner Author

@Goooler Goooler Jan 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems issues in buildSrc module make Kotlin lang level is 1.4.


enum class LibModule(override val tag: String) : Module {
//---------------------base-------------------------------//
Base("base"),
Common("common"),

//---------------------biz-------------------------------//
Login("login"),
Main("main"),
Map("map"),
Web("web"),

//---------------------func-------------------------------//
Adapter("adapter"),

//---------------------app-------------------------------//
App("app"),
Test("test")
}

enum class Flavor {
Daily,
Online
Adapter("adapter")
}

enum class BuildConfigField(val tag: String) {
VersionCode("VERSION_CODE"),
VersionName("VERSION_NAME"),
CdnPrefix("CDN_PREFIX"),
ApiHost("API_HOST"),
DoraemonKitKey("DORAEMON_KIT_KEY")
enum class AppModule(override val tag: String, val appName: String, val appId: String) : Module {
App("app", "Demo", "io.goooler.demoapp"),
Test("app", "Test", "io.goooler.test")
Comment on lines +22 to +24
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate app modules to new enum classes.

}

enum class ApiKey(val key: String) {
DoraemonKit("4a8c3eef29f029bc197705faad83f43d"),
AmapDebug("419ff22368500a49f0d894ac80d08725"),
AmapRelease("ab0ee784fe95f3de2af3db07e11c37ed")
enum class BuildConfigField(val key: String, val value: Any) {
VersionCode("VERSION_CODE", appVersionCode),
VersionName("VERSION_NAME", appVersionName),
CdnPrefix("CDN_PREFIX", "https://raw.githubusercontent.com/"),
ApiHost("API_HOST", "https://api.github.com/")
Comment on lines +27 to +31
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bundle keys and values.

}
82 changes: 25 additions & 57 deletions buildSrc/src/main/kotlin/Extensions.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
@file:Suppress("UnstableApiUsage")

import com.android.build.api.dsl.CommonExtension
import com.android.build.api.dsl.VariantDimension
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.internal.api.ApkVariantOutputImpl
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import java.io.File
import java.util.Locale
import java.util.Properties
import kotlin.math.pow
import org.gradle.api.JavaVersion
Expand All @@ -23,15 +20,6 @@ import org.gradle.kotlin.dsl.get
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions
import org.jetbrains.kotlin.gradle.plugin.KaptExtension

const val cdnPrefix = "https://raw.githubusercontent.com/"
val apiHosts = mapOf(
Flavor.Daily.name to "https://api.github.com/",
Flavor.Online.name to "https://api.github.com/"
)

// app
const val appPackageName = "io.goooler.demoapp"
const val appName = "Demo"
const val extraScriptPath = "gradle/extra.gradle.kts"
val javaVersion = JavaVersion.VERSION_11
const val appVersionName = "1.5.0"
Expand Down Expand Up @@ -77,12 +65,12 @@ fun PluginAware.applyPlugins(vararg names: String) {
}
}

fun VariantDimension.putBuildConfigStringField(name: String, value: String?) {
buildConfigField("String", name, "\"$value\"")
}

fun VariantDimension.putBuildConfigIntField(name: String, value: Int) {
buildConfigField("Integer", name, value.toString())
Comment on lines -80 to -85
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge two functions to a new one.

fun VariantDimension.buildConfigField(field: BuildConfigField) {
if (field.value is Int) {
buildConfigField("Integer", field.key, field.value.toString())
} else if (field.value is String) {
buildConfigField("String", field.key, "\"${field.value}\"")
}
}

fun BaseExtension.kotlinOptions(block: KotlinJvmOptions.() -> Unit) {
Expand All @@ -92,7 +80,7 @@ fun BaseExtension.kotlinOptions(block: KotlinJvmOptions.() -> Unit) {
fun Project.kapt(block: KaptExtension.() -> Unit) = configure(block)

inline fun <reified T : BaseExtension> Project.setupBase(
module: Module? = null,
module: Module,
crossinline block: T.() -> Unit = {}
) {
when (T::class) {
Expand All @@ -103,15 +91,13 @@ inline fun <reified T : BaseExtension> Project.setupBase(

applyPlugins(Plugins.kotlinAndroid, Plugins.kotlinKapt)
extensions.configure<BaseExtension>("android") {
resourcePrefix = "${module.tag}_"
compileSdkVersion(32)
defaultConfig {
minSdk = 21
vectorDrawables.useSupportLibrary = true
ndk.abiFilters += setOf("arm64-v8a")
module?.let {
resourcePrefix = "${it.tag}_"
versionNameSuffix = "_${it.tag}"
}
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
sourceSets.configureEach {
java.srcDirs("src/$name/kotlin")
Expand Down Expand Up @@ -164,21 +150,20 @@ inline fun <reified T : BaseExtension> Project.setupBase(
}

fun Project.setupLib(
module: Module? = null,
module: LibModule,
block: LibraryExtension.() -> Unit = {}
) = setupCommon(module, block)

fun Project.setupApp(
appPackageName: String,
appName: String,
module: AppModule,
block: BaseAppModuleExtension.() -> Unit = {}
) = setupCommon<BaseAppModuleExtension> {
) = setupCommon<BaseAppModuleExtension>(module) {
defaultConfig {
applicationId = appPackageName
applicationId = module.appId
targetSdk = 32
versionCode = appVersionCode
versionName = appVersionName
manifestPlaceholders += mapOf("appName" to appName)
manifestPlaceholders += mapOf("appName" to module.appName)
resourceConfigurations += setOf("en", "zh-rCN", "xxhdpi")
}
signingConfigs.create("release") {
Expand All @@ -191,16 +176,16 @@ fun Project.setupApp(
}
buildTypes {
release {
resValue("string", "app_name", appName)
resValue("string", "app_name", module.appName)
signingConfig = signingConfigs["release"]
isMinifyEnabled = true
isShrinkResources = true
proguardFiles("$rootDir/gradle/proguard-rules.pro")
}
debug {
resValue("string", "app_name", "${appName}.debug")
resValue("string", "app_name", "${module.appName}.debug")
signingConfig = signingConfigs["release"]
applicationIdSuffix = ".debug"
versionNameSuffix = ".debug"
isJniDebuggable = true
isRenderscriptDebuggable = true
isCrunchPngs = false
Expand All @@ -209,22 +194,21 @@ fun Project.setupApp(
dependenciesInfo.includeInApk = false
applicationVariants.all {
outputs.all {
(this as? ApkVariantOutputImpl)?.outputFileName =
"../../../../${appName}_${versionName}_${versionCode}_" +
"${flavorName.toLowerCase(Locale.ROOT)}_${buildType.name}.apk"
(this as? ApkVariantOutputImpl)?.outputFileName = "../../../../" +
"${module.appName}_${versionName}_${versionCode}_${flavorName}_${buildType.name}.apk"
}
}
block()
}

private inline fun <reified T : BaseExtension> Project.setupCommon(
module: Module? = null,
module: Module,
crossinline block: T.() -> Unit = {}
) = setupBase<T>(module) {
flavorDimensions("channel")
productFlavors {
create(Flavor.Daily.name)
create(Flavor.Online.name)
create("daily")
create("online")
}
kapt {
arguments {
Expand All @@ -233,30 +217,14 @@ private inline fun <reified T : BaseExtension> Project.setupCommon(
}
}
dependencies {
if (module != Module.Common) {
implementations(project(Module.Common.moduleName))
if (module != LibModule.Common) {
implementations(project(LibModule.Common.moduleName))
}
implementations(
project(Module.Base.moduleName),
project(Module.Adapter.moduleName),

// router
Libs.arouter,

// UI
Libs.constraintLayout,
Libs.cardView,
Libs.material,
*Libs.smartRefreshLayout,
Libs.photoView,

// utils
*Libs.hilt,
*Libs.room,
*Libs.rx,
*Libs.moshi,
Libs.collection,
Libs.utils
Comment on lines -240 to -259
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move unnecessary deps into common module.

*Libs.moshi
)
kapts(Libs.arouterCompiler, Libs.moshiCompiler, Libs.roomCompiler, Libs.hiltCompiler)
}
Expand Down
38 changes: 22 additions & 16 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
setupLib(Module.Common) {
setupLib(LibModule.Common) {
buildFeatures.buildConfig = true
productFlavors.all {
putBuildConfigIntField(BuildConfigField.VersionCode.tag, appVersionCode)
putBuildConfigStringField(BuildConfigField.VersionName.tag, appVersionName)
putBuildConfigStringField(BuildConfigField.CdnPrefix.tag, cdnPrefix)
putBuildConfigStringField(BuildConfigField.ApiHost.tag, apiHosts[name])
putBuildConfigStringField(BuildConfigField.DoraemonKitKey.tag, ApiKey.DoraemonKit.key)
buildConfigField(BuildConfigField.VersionCode)
buildConfigField(BuildConfigField.VersionName)
buildConfigField(BuildConfigField.CdnPrefix)
buildConfigField(BuildConfigField.ApiHost)
}
}

dependencies {
implementations(
// network
*Libs.coil
)
debugImplementations(
Libs.leakCanary,
Libs.chuckerDebug
)
releaseImplementations(
Libs.chuckerRelease
apis(
// project
project(LibModule.Base.moduleName),
project(LibModule.Adapter.moduleName),
// UI
Libs.constraintLayout,
Libs.cardView,
Libs.material,
*Libs.smartRefreshLayout,
Libs.photoView,
// other
*Libs.rx,
Libs.collection,
Libs.utils
)
implementations(*Libs.coil)
debugImplementations(Libs.chuckerDebug)
releaseImplementations(Libs.chuckerRelease)
}
2 changes: 1 addition & 1 deletion login/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
setupLib(Module.Login)
setupLib(LibModule.Login)
4 changes: 1 addition & 3 deletions main/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
setupLib(Module.Main) {
defaultConfig.testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

setupLib(LibModule.Main) {
sourceSets["main"].res.srcDirs(
"src/main/res/core",
"src/main/res/other"
Expand Down
6 changes: 1 addition & 5 deletions test/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
setupApp("$appPackageName.test", "test") {
defaultConfig {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
}
setupApp(AppModule.Test)

dependencies {
implementations(
Expand Down
2 changes: 1 addition & 1 deletion web/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
setupLib(Module.Web) {
setupLib(LibModule.Web) {
buildFeatures.buildConfig = true
}

Expand Down