-
Notifications
You must be signed in to change notification settings - Fork 252
Refactor the logic of consuming apk built from :wear:watchface #179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,8 @@ | |
| */ | ||
| import java.io.ByteArrayOutputStream | ||
| import java.util.regex.Pattern | ||
| import org.gradle.api.attributes.Attribute | ||
| import java.lang.RuntimeException | ||
|
|
||
| evaluationDependsOn(":wear:watchface") | ||
|
|
||
|
|
@@ -66,9 +68,38 @@ configurations { | |
| isCanBeConsumed = false | ||
| isCanBeResolved = true | ||
| } | ||
|
|
||
| create("watchfaceApkDebug"){ | ||
| isCanBeResolved = true | ||
| isCanBeConsumed = false | ||
|
|
||
| attributes { | ||
| attribute( | ||
| Attribute.of(com.android.build.api.attributes.BuildTypeAttr::class.java), | ||
|
||
| objects.named(com.android.build.api.attributes.BuildTypeAttr::class.java, "debug") | ||
| ) | ||
| attribute(Attribute.of("artifactType", String::class.java), "apk") | ||
| } | ||
| } | ||
|
|
||
| create("watchfaceApkRelease") { | ||
| isCanBeResolved = true | ||
| isCanBeConsumed = false | ||
|
|
||
| attributes { | ||
| attribute( | ||
| Attribute.of(com.android.build.api.attributes.BuildTypeAttr::class.java), | ||
| objects.named(com.android.build.api.attributes.BuildTypeAttr::class.java, "release") | ||
| ) | ||
| attribute(Attribute.of("artifactType", String::class.java), "apk") | ||
| } | ||
| } | ||
|
||
| } | ||
|
|
||
| dependencies { | ||
| configurations.getByName("watchfaceApkDebug").dependencies.add(project(":wear:watchface")) | ||
|
||
| configurations.getByName("watchfaceApkRelease").dependencies.add(project(":wear:watchface")) | ||
|
||
|
|
||
| implementation(projects.wear.common) | ||
| implementation(platform(libs.androidx.compose.bom)) | ||
| implementation(libs.androidx.wear.compose.foundation) | ||
|
|
@@ -93,29 +124,29 @@ dependencies { | |
| androidComponents.onVariants { variant -> | ||
| val capsVariant = variant.name.replaceFirstChar { it.uppercase() } | ||
|
|
||
| val copyTaskProvider = tasks.register<Copy>("copyWatchface${capsVariant}Output") { | ||
| val wfTask = project(":wear:watchface").tasks.named("assemble$capsVariant") | ||
| dependsOn(wfTask) | ||
| val buildDir = project(":wear:watchface").layout.buildDirectory.asFileTree.matching { | ||
| include("**/${variant.name}/**/*.apk") | ||
| exclude("**/*androidTest*") | ||
| val watchfaceApkConfig = when (variant.name) { | ||
| "release" -> configurations.getByName("watchfaceApkRelease") | ||
| "debug" -> configurations.getByName("watchfaceApkDebug") | ||
| else -> throw RuntimeException("Cannot find watchface apk configuration") | ||
| } | ||
|
||
|
|
||
| val copyWatchfaceApkTask = tasks.register<Copy>("copyWatchface${capsVariant}ApkToAssets") { | ||
| from(watchfaceApkConfig) { | ||
| // the resolved directory contains apk and output-metadata.json | ||
| include("*.apk") | ||
| } | ||
| from(buildDir) | ||
| into(layout.buildDirectory.dir("intermediates/watchfaceAssets/${variant.name}")) | ||
|
|
||
| duplicatesStrategy = DuplicatesStrategy.EXCLUDE | ||
|
|
||
| eachFile { | ||
| path = "default_watchface.apk" | ||
| } | ||
| duplicatesStrategy = DuplicatesStrategy.EXCLUDE | ||
| includeEmptyDirs = false | ||
| } | ||
|
|
||
| val tokenTask = tasks.register<ProcessFilesTask>("generateToken${capsVariant}Res") { | ||
| val tokenFile = | ||
| layout.buildDirectory.file("generated/wfTokenRes/${variant.name}/res/values/wf_token.xml") | ||
|
|
||
| inputFile.from(copyTaskProvider.map { it.outputs.files.singleFile }) | ||
| inputFile.from(copyWatchfaceApkTask.map { it.outputs.files.singleFile }) | ||
| outputFile.set(tokenFile) | ||
| cliToolClasspath.set(project.configurations["cliToolConfiguration"]) | ||
| } | ||
|
|
@@ -143,7 +174,8 @@ abstract class ProcessFilesTask : DefaultTask() { | |
|
|
||
| @TaskAction | ||
| fun taskAction() { | ||
| val apkFile = inputFile.singleFile.resolve("default_watchface.apk") | ||
| val apkDirectory = inputFile.singleFile | ||
| val apkFile = apkDirectory.resolve("default_watchface.apk") | ||
|
||
|
|
||
| val stdOut = ByteArrayOutputStream() | ||
| val stdErr = ByteArrayOutputStream() | ||
|
|
@@ -189,4 +221,4 @@ abstract class ProcessFilesTask : DefaultTask() { | |
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be a single configuration that doesn't specify com.android.build.api.attributes.BuildTypeAttr::class.java and we just set that in artifactView at the consumption place. That way you don't accidentally add the project dependency just to a single configuration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also should use register instead of create. Configurations are lazy starting 9.1.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestions! I tried adding build attributes and use artifactView on the consumption side(in the copy task). However, it didn't work for me and gradle keeps telling me "consumer didn't ask for build type attribute and it cannot choose between debugRuntimeElements and releaseRuntimeElements. I am quite confused.
I didn't some research and it seems the reason is "attributes on the artifactView only refine what artifacts are retrieved after the initial variant resolution has occurred. They don't provide the necessary attributes to guide the initial variant selection for the entire configuration"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Today I learn. Thanks for sharing!