Skip to content

Commit 733502f

Browse files
authored
Android: move AndroidBuildConfig to AndroidModule (#6145)
Resolves #6141 ### Motivation - BuildConfig was an additional trait to be extended only by AndroidAppModules, but [regular Modules](https://github.com/android/compose-samples/blob/main/Jetcaster/core/data/build.gradle.kts#L43) should have it as well - Missing `package` field in the generated config - There was no task to add custom fields ### Changes - Moved BuildConfig generation to AndroidModule. - It is configurable with the `enableBuildConfig` flag, which defaults to true ([like gradle's](https://developer.android.com/reference/tools/gradle-api/7.4/com/android/build/api/dsl/BuildFeatures#buildConfig())). - Added package name that defaults to `androidNamespace` - Added a task for the config's members. (e.g. AndroidAppModule overrides this to add its `applicationID`
1 parent 51a76bd commit 733502f

File tree

4 files changed

+72
-43
lines changed

4 files changed

+72
-43
lines changed

example/thirdparty/androidtodo/build.mill

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ object androidSdkModule0 extends AndroidSdkModule {
2222
object app
2323
extends AndroidAppKotlinModule,
2424
AndroidR8AppModule,
25-
AndroidBuildConfig,
2625
AndroidHiltSupport {
2726

2827
def kotlinVersion = Versions.kotlinVersion

libs/androidlib/src/mill/androidlib/AndroidAppModule.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ trait AndroidAppModule extends AndroidModule { outer =>
119119
*/
120120
def androidLintArgs: T[Seq[String]] = Task { Seq.empty[String] }
121121

122+
override def androidBuildConfigMembers: T[Seq[String]] = Task {
123+
val buildType = if (androidIsDebug()) "debug" else "release"
124+
Seq(
125+
s"boolean DEBUG = ${androidIsDebug()}",
126+
s"""String BUILD_TYPE = "$buildType"""",
127+
s"""String APPLICATION_ID = "$androidApplicationId"""",
128+
s"""int VERSION_CODE = ${androidVersionCode()}""",
129+
s"""String VERSION_NAME = "${androidVersionName()}""""
130+
)
131+
}
132+
122133
@internal
123134
override def bspCompileClasspath(
124135
needsToMergeResourcesIntoCompileDest: Boolean

libs/androidlib/src/mill/androidlib/AndroidBuildConfig.scala

Lines changed: 0 additions & 42 deletions
This file was deleted.

libs/androidlib/src/mill/androidlib/AndroidModule.scala

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,67 @@ trait AndroidModule extends JavaModule { outer =>
582582
*/
583583
def androidNamespace: String
584584

585+
/**
586+
* If true, a BuildConfig.java file will be generated.
587+
* Defaults to true.
588+
*
589+
* [[https://developer.android.com/reference/tools/gradle-api/7.4/com/android/build/api/dsl/BuildFeatures#buildConfig()]]
590+
*/
591+
def enableBuildConfig: Boolean = true
592+
593+
/**
594+
* The package name where the BuildInfo.java file will be generated.
595+
* Defaults to [[androidNamespace]].
596+
*/
597+
def androidBuildInfoPackageName: String = androidNamespace
598+
599+
/**
600+
* The members to include in the generated BuildConfig.java file.
601+
* Format is "type NAME = value"
602+
*/
603+
def androidBuildConfigMembers: T[Seq[String]] = Task {
604+
val buildType = if (androidIsDebug()) "debug" else "release"
605+
Seq(
606+
s"boolean DEBUG = ${androidIsDebug()}",
607+
s"""String BUILD_TYPE = "$buildType"""",
608+
s"""String LIBRARY_PACKAGE_NAME = "$androidBuildInfoPackageName""""
609+
)
610+
}
611+
612+
/**
613+
* Generates a BuildConfig.java file in the [[androidBuildInfoPackageName]] package
614+
* This is a basic implementation of AGP's build config feature!
615+
*/
616+
def androidGeneratedBuildConfigSources: T[Seq[PathRef]] = Task {
617+
val parsedMembers: Seq[String] = androidBuildConfigMembers().map { member =>
618+
s"public static final $member;"
619+
}
620+
val content: String =
621+
s"""
622+
|package $androidBuildInfoPackageName;
623+
|public final class BuildConfig {
624+
| ${parsedMembers.mkString("\n ")}
625+
|}
626+
""".stripMargin
627+
628+
val destination = Task.dest / "source" / os.SubPath(androidBuildInfoPackageName.replace(
629+
".",
630+
"/"
631+
)) / "BuildConfig.java"
632+
633+
os.write(destination, content, createFolders = true)
634+
635+
Seq(PathRef(destination))
636+
}
637+
638+
override def generatedSources: T[Seq[PathRef]] = if enableBuildConfig then
639+
Task {
640+
super.generatedSources() ++ androidGeneratedBuildConfigSources()
641+
}
642+
else {
643+
super.generatedSources()
644+
}
645+
585646
/**
586647
* Gets the extracted android resources from the dependencies using [[androidLibraryResources]]
587648
* and compiles them into flata files using aapt2. This allows for the resources to be linked

0 commit comments

Comments
 (0)