1616import java.io.ByteArrayOutputStream
1717import java.util.regex.Pattern
1818import org.gradle.api.attributes.Attribute
19- import java.lang.RuntimeException
20-
21- evaluationDependsOn(" :wear:watchface" )
19+ import com.android.build.api.attributes.BuildTypeAttr
2220
2321plugins {
2422 alias(libs.plugins.android.application)
@@ -69,36 +67,26 @@ configurations {
6967 isCanBeResolved = true
7068 }
7169
72- create(" watchfaceApkDebug" ){
73- isCanBeResolved = true
74- isCanBeConsumed = false
75-
76- attributes {
77- attribute(
78- Attribute .of(com.android.build.api.attributes.BuildTypeAttr ::class .java),
79- objects.named(com.android.build.api.attributes.BuildTypeAttr ::class .java, " debug" )
80- )
81- attribute(Attribute .of(" artifactType" , String ::class .java), " apk" )
82- }
83- }
84-
85- create(" watchfaceApkRelease" ) {
86- isCanBeResolved = true
87- isCanBeConsumed = false
88-
89- attributes {
90- attribute(
91- Attribute .of(com.android.build.api.attributes.BuildTypeAttr ::class .java),
92- objects.named(com.android.build.api.attributes.BuildTypeAttr ::class .java, " release" )
93- )
94- attribute(Attribute .of(" artifactType" , String ::class .java), " apk" )
70+ listOf (" debug" , " release" ).forEach { buildType ->
71+ create(" watchfaceApk${buildType.replaceFirstChar { it.uppercase() }} " ) {
72+ isCanBeResolved = true
73+ isCanBeConsumed = false
74+
75+ attributes {
76+ attribute(
77+ Attribute .of(BuildTypeAttr ::class .java),
78+ objects.named(BuildTypeAttr ::class .java, buildType)
79+ )
80+ attribute(Attribute .of(" artifactType" , String ::class .java), " apk" )
81+ }
9582 }
9683 }
9784}
9885
9986dependencies {
100- configurations.getByName(" watchfaceApkDebug" ).dependencies.add(project(" :wear:watchface" ))
101- configurations.getByName(" watchfaceApkRelease" ).dependencies.add(project(" :wear:watchface" ))
87+ configurations.matching { it.name.startsWith(" watchfaceApk" ) }.all {
88+ dependencies.add(project(" :wear:watchface" ))
89+ }
10290
10391 implementation(projects.wear.common)
10492 implementation(platform(libs.androidx.compose.bom))
@@ -123,12 +111,7 @@ dependencies {
123111
124112androidComponents.onVariants { variant ->
125113 val capsVariant = variant.name.replaceFirstChar { it.uppercase() }
126-
127- val watchfaceApkConfig = when (variant.name) {
128- " release" -> configurations.getByName(" watchfaceApkRelease" )
129- " debug" -> configurations.getByName(" watchfaceApkDebug" )
130- else -> throw RuntimeException (" Cannot find watchface apk configuration" )
131- }
114+ val watchfaceApkConfig = configurations.getByName(" watchfaceApk$capsVariant " )
132115
133116 val copyWatchfaceApkTask = tasks.register<Copy >(" copyWatchface${capsVariant} ApkToAssets" ) {
134117 from(watchfaceApkConfig) {
@@ -146,7 +129,13 @@ androidComponents.onVariants { variant ->
146129 val tokenTask = tasks.register<ProcessFilesTask >(" generateToken${capsVariant} Res" ) {
147130 val tokenFile =
148131 layout.buildDirectory.file(" generated/wfTokenRes/${variant.name} /res/values/wf_token.xml" )
149- inputFile.from(copyWatchfaceApkTask.map { it.outputs.files.singleFile })
132+
133+ apkDirectory.set(
134+ layout.dir( copyWatchfaceApkTask.map {
135+ it.destinationDir
136+ })
137+ )
138+
150139 outputFile.set(tokenFile)
151140 cliToolClasspath.set(project.configurations[" cliToolConfiguration" ])
152141 }
@@ -159,8 +148,8 @@ androidComponents.onVariants { variant ->
159148}
160149
161150abstract class ProcessFilesTask : DefaultTask () {
162- @get:InputFiles
163- abstract val inputFile : ConfigurableFileCollection
151+ @get:InputDirectory
152+ abstract val apkDirectory : DirectoryProperty
164153
165154 @get:OutputFile
166155 abstract val outputFile: RegularFileProperty
@@ -174,8 +163,7 @@ abstract class ProcessFilesTask : DefaultTask() {
174163
175164 @TaskAction
176165 fun taskAction () {
177- val apkDirectory = inputFile.singleFile
178- val apkFile = apkDirectory.resolve(" default_watchface.apk" )
166+ val apkFile = apkDirectory.asFile.get().resolve(" default_watchface.apk" )
179167
180168 val stdOut = ByteArrayOutputStream ()
181169 val stdErr = ByteArrayOutputStream ()
0 commit comments