Skip to content

Commit

Permalink
Assets目录的flavor分化
Browse files Browse the repository at this point in the history
大致实现了Assets目录的flavor分化,共同的文件放在main中,其它的存放于各自的assets目录里,避免因为assets文件混在一起导致打包模板体积过大
@SuperMonster003
这个PR只是提供一个思路(不会有后续提交了),如果与你当前修改有冲突,你也可以做个简单的说明并关闭它。另外就是如果没什么特殊情况,建议审核PR的时间不宜过长,比如不要超过一个月,因为时间太长的话即使有什么好的想法也可能早都忘光光了,那样体验可能就不那么好了
  • Loading branch information
LZX284 committed Nov 8, 2023
1 parent b9f65cf commit 9f9e2f3
Show file tree
Hide file tree
Showing 334 changed files with 75 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ sync.ffs_db

/local.properties
/sign.properties

/app/app
/app/inrt
**/assets/declarations
**/assets/sample/declarations
88 changes: 71 additions & 17 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,14 @@ android {

gradle.taskGraph.whenReady(object : Action<TaskExecutionGraph> {
override fun execute(taskGraph: TaskExecutionGraph) {
val taskName = "$buildActionAssemble${flavorNameInrt.uppercaseFirstChar()}${buildTypeRelease.uppercaseFirstChar()}"
val taskName =
"$buildActionAssemble${flavorNameInrt.uppercaseFirstChar()}${buildTypeRelease.uppercaseFirstChar()}"
project.getTasksByName(taskName, true)
.firstOrNull()
?.doLast {
copy {
val src = "build/outputs/apk/$flavorNameInrt/$buildTypeRelease"
val dst = "src/main/assets"
val dst = "src/main/assets_$flavorNameApp"
val ext = Utils.FILE_EXTENSION_APK

if (!file(src).isDirectory) {
Expand All @@ -379,13 +380,23 @@ android {
from(src); into(dst)

val verName = versionName?.replace(Regex("\\s"), "-")?.lowercase()
val srcFileName = "$flavorNameInrt-v$verName-universal.$ext" /* e.g. inrt-v6.4.0-beta-universal.apk */
val srcFileName =
"$flavorNameInrt-v$verName-universal.$ext" /* e.g. inrt-v6.4.0-beta-universal.apk */
val dstFileName = "$templateName.$ext"
val isOverridden = file(File(dst, dstFileName)).exists()
include(srcFileName)
rename(srcFileName, dstFileName)
println("Source: ${file(File(src, srcFileName))}")
println("Destination: ${file(File(dst, dstFileName))}${if (isOverridden) " [overridden]" else ""}")
println(
"Destination: ${
file(
File(
dst,
dstFileName
)
)
}${if (isOverridden) " [overridden]" else ""}"
)
}
}
?: println("$taskName doesn't exist in project ${project.name}")
Expand All @@ -397,17 +408,36 @@ android {
if (gradle.startParameter.taskNames.any { it.contains(Regex("^(:?$flavorNameApp:)?$buildActionAssemble")) }) {
ignoreAssetsPatterns.add(".idea")
}
if (gradle.startParameter.taskNames.any { it.contains(Regex("^(:?$flavorNameApp:)?$buildActionAssemble$flavorNameInrt", IGNORE_CASE)) }) {
if (gradle.startParameter.taskNames.any {
it.contains(
Regex(
"^(:?$flavorNameApp:)?$buildActionAssemble$flavorNameInrt",
IGNORE_CASE
)
)
}) {
// @Hint by SuperMonster003 on Oct 16, 2023.
// ! Nothing needs to be added into assets for flavor "inrt",
// ! as assets will be copied from flavor "app"
// ! while building an apk on org.autojs.autojs.ui.project.BuildActivity.
ignoreAssetsPatterns.add("!*") /* Ignore everything. */
// ignoreAssetsPatterns.add("!*") /* Ignore everything. */
}
}

}

sourceSets {
getByName("main") {
assets { srcDirs("src/main/assets") }
}
getByName(flavorNameApp) {
assets { srcDirs("src/main/assets_$flavorNameApp") }
}
getByName(flavorNameInrt) {
assets { srcDirs("src/main/assets_$flavorNameInrt") }
}
}

compileOptions {
isCoreLibraryDesugaringEnabled = true
sourceCompatibility = versions.javaVersion
Expand Down Expand Up @@ -494,13 +524,22 @@ android {
javaCompileOptions {
annotationProcessorOptions {
mapOf(
"resourcePackageName" to (this@defaultConfig.applicationId ?: this@Build_gradle.applicationId),
"resourcePackageName" to (this@defaultConfig.applicationId
?: this@Build_gradle.applicationId),
"androidManifestFile" to ("$projectDir/src/main/AndroidManifest.xml")
).let { arguments(it) }
}
}
buildConfigField("String", "VERSION_DATE", "\"${Utils.getDateString("MMM d, yyyy", "GMT+08:00")}\"")
buildConfigField("String", "VSCODE_EXT_REQUIRED_VERSION", "\"${versions.vscodeExtRequiredVersion}\"")
buildConfigField(
"String",
"VERSION_DATE",
"\"${Utils.getDateString("MMM d, yyyy", "GMT+08:00")}\""
)
buildConfigField(
"String",
"VSCODE_EXT_REQUIRED_VERSION",
"\"${versions.vscodeExtRequiredVersion}\""
)
}

applicationVariants.all {
Expand All @@ -515,7 +554,9 @@ android {
}
outputs
.map { it as BaseVariantOutputImpl }
.forEach { it.outputFileName = Utils.getOutputFileName(this@all as ApplicationVariantImpl, it) }
.forEach {
it.outputFileName = Utils.getOutputFileName(this@all as ApplicationVariantImpl, it)
}
}

splits {
Expand Down Expand Up @@ -716,11 +757,14 @@ class Versions(filePath: String) {
val title = "Version information for AutoJs6 app library"

val infoVerName = "Version name: $appVersionName"
val infoVerCode = "Version code: ${if (isBuildNumberAutoIncremented) "${appVersionCode + 1} [auto-incremented]" else appVersionCode}"
val infoVerSdk = "SDK versions: min [$sdkVersionMin] / target [$sdkVersionTarget] / compile [$sdkVersionCompile]"
val infoVerCode =
"Version code: ${if (isBuildNumberAutoIncremented) "${appVersionCode + 1} [auto-incremented]" else appVersionCode}"
val infoVerSdk =
"SDK versions: min [$sdkVersionMin] / target [$sdkVersionTarget] / compile [$sdkVersionCompile]"
val infoVerJava = "Java version: $javaVersion$javaVersionInfoSuffix"

val maxLength = arrayOf(title, infoVerName, infoVerCode, infoVerSdk, infoVerJava).maxOf { it.length }
val maxLength =
arrayOf(title, infoVerName, infoVerCode, infoVerSdk, infoVerJava).maxOf { it.length }

arrayOf(
"=".repeat(maxLength),
Expand All @@ -739,7 +783,14 @@ class Versions(filePath: String) {
project.gradle.taskGraph.whenReady(object : Action<TaskExecutionGraph> {
override fun execute(taskGraph: TaskExecutionGraph) {
for (buildType in targetBuildType) {
if (taskGraph.hasTask(Utils.getAssembleFullTaskName(project.name, flavorName, buildType))) {
if (taskGraph.hasTask(
Utils.getAssembleFullTaskName(
project.name,
flavorName,
buildType
)
)
) {
return appendToTask(project, flavorName, buildType)
}
}
Expand All @@ -758,7 +809,8 @@ object Utils {

fun getDateString(format: String, zone: String): String {
// e.g. May 23, 2011
return SimpleDateFormat(format).apply { timeZone = TimeZone.getTimeZone(zone) }.format(Date())
return SimpleDateFormat(format).apply { timeZone = TimeZone.getTimeZone(zone) }
.format(Date())
}

fun getOutputFileName(variant: ApplicationVariantImpl, output: BaseVariantOutputImpl): String {
Expand All @@ -770,9 +822,11 @@ object Utils {
return "$autojs-v$version-$architecture.$extension".lowercase(Locale.getDefault())
}

fun getAssembleTaskName(flavorName: String, buildType: String) = "assemble${capitalize(flavorName)}${capitalize(buildType)}"
fun getAssembleTaskName(flavorName: String, buildType: String) =
"assemble${capitalize(flavorName)}${capitalize(buildType)}"

fun getAssembleFullTaskName(projectName: String, flavorName: String, buildType: String) = ":$projectName:${getAssembleTaskName(flavorName, buildType)}"
fun getAssembleFullTaskName(projectName: String, flavorName: String, buildType: String) =
":$projectName:${getAssembleTaskName(flavorName, buildType)}"

fun digestCRC32(file: File): String {
val fis = FileInputStream(file)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
Loading

0 comments on commit 9f9e2f3

Please sign in to comment.