Skip to content
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

chore(android): apply spotless to gradle plugin module #1671

Merged
merged 1 commit into from
Jan 2, 2025
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 android/measure-android-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ To integrate Measure in your project, please follow the
instructions [here](../../docs/android/README.md#getting-started).

To know more about the features of the plugin, please refer to
the [documentation](../../docs/android/gradle-plugin.md).
the [documentation](../../docs/android/gradle-plugin.md).
46 changes: 44 additions & 2 deletions android/measure-android-gradle/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import com.diffplug.gradle.spotless.SpotlessExtension
import com.vanniktech.maven.publish.GradlePublishPlugin
import com.vanniktech.maven.publish.SonatypeHost

Expand Down Expand Up @@ -37,7 +38,7 @@ mavenPublishing {
coordinates(group as String, artifactId, version as String)
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL, automaticRelease = true)
configure(
GradlePublishPlugin()
GradlePublishPlugin(),
)

pom {
Expand Down Expand Up @@ -70,6 +71,47 @@ kotlin {
jvmToolchain(17)
}

extensions.configure<SpotlessExtension>("spotless") {
plugins.withId("org.jetbrains.kotlin.jvm") {
configureSpotlessKotlin(this@configure)
}
plugins.withId("org.jetbrains.kotlin.android") {
configureSpotlessKotlin(this@configure)
}
kotlinGradle {
ktlint()
}
format("misc") {
target(
".gitignore",
".gitattributes",
".gitconfig",
".editorconfig",
"*.md",
"src/**/*.md",
"docs/**/*.md",
"src/**/*.properties",
)
indentWithSpaces()
trimTrailingWhitespace()
endWithNewline()
}
}

fun configureSpotlessKotlin(spotlessExtension: SpotlessExtension) {
spotlessExtension.kotlin {
ktlint().apply {
editorConfigOverride(
mapOf(
"max_line_length" to 2147483647,
"ktlint_function_naming_ignore_when_annotated_with" to "Composable",
),
)
}
target("src/**/*.kt")
}
}

dependencies {
compileOnly(libs.agp)
compileOnly(libs.asm.util)
Expand Down Expand Up @@ -106,4 +148,4 @@ tasks.withType<Test>().configureEach {
if (this.name.equals("functionalTest")) {
useJUnitPlatform()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class MeasurePluginTest {
@ParameterizedTest
@MethodSource("versions")
fun `assert tasks are created when assemble task is triggered`(
agpVersion: SemVer, gradleVersion: GradleVersion
agpVersion: SemVer,
gradleVersion: GradleVersion,
) {
val project = MeasurePluginFixture(agpVersion, minifyEnabled = true).gradleProject
val result = build(gradleVersion, project.rootDir, ":app:assembleRelease")
Expand All @@ -45,7 +46,8 @@ class MeasurePluginTest {
@ParameterizedTest
@MethodSource("versions")
fun `assert tasks are created when bundle task is triggered`(
agpVersion: SemVer, gradleVersion: GradleVersion
agpVersion: SemVer,
gradleVersion: GradleVersion,
) {
val project = MeasurePluginFixture(agpVersion, minifyEnabled = true).gradleProject
val result = build(gradleVersion, project.rootDir, ":app:bundleRelease")
Expand All @@ -57,7 +59,8 @@ class MeasurePluginTest {
@ParameterizedTest
@MethodSource("versions")
fun `assert tasks not created when measure is disabled using MeasurePluginExtension`(
agpVersion: SemVer, gradleVersion: GradleVersion
agpVersion: SemVer,
gradleVersion: GradleVersion,
) {
val project = MeasurePluginFixture(agpVersion, minifyEnabled = true, enabled = false).gradleProject
val assembleResult = build(gradleVersion, project.rootDir, ":app:assembleRelease")
Expand All @@ -74,7 +77,8 @@ class MeasurePluginTest {
@ParameterizedTest
@MethodSource("versions")
fun `assert tasks are not created when a task other than assemble or bundle are triggered`(
agpVersion: SemVer, gradleVersion: GradleVersion
agpVersion: SemVer,
gradleVersion: GradleVersion,
) {
val project = MeasurePluginFixture(agpVersion, minifyEnabled = false).gradleProject
val result = build(gradleVersion, project.rootDir, ":app:test")
Expand All @@ -86,7 +90,8 @@ class MeasurePluginTest {
@ParameterizedTest
@MethodSource("versions")
fun `API_KEY is set in manifest, assert upload request is created`(
agpVersion: SemVer, gradleVersion: GradleVersion
agpVersion: SemVer,
gradleVersion: GradleVersion,
) {
server.enqueue(MockResponse().setResponseCode(200))
server.start(8080)
Expand All @@ -98,7 +103,8 @@ class MeasurePluginTest {
@ParameterizedTest
@MethodSource("versions")
fun `API_KEY is not set in manifest, assert logs error`(
agpVersion: SemVer, gradleVersion: GradleVersion
agpVersion: SemVer,
gradleVersion: GradleVersion,
) {
val project = MeasurePluginFixture(agpVersion, setMeasureApiKey = false).gradleProject
val result = build(gradleVersion, project.rootDir, ":app:assembleRelease")
Expand All @@ -108,7 +114,8 @@ class MeasurePluginTest {
@ParameterizedTest
@MethodSource("versions")
fun `API_URL is not set in manifest, assert logs error`(
agpVersion: SemVer, gradleVersion: GradleVersion
agpVersion: SemVer,
gradleVersion: GradleVersion,
) {
val project = MeasurePluginFixture(agpVersion, setMeasureApiUrl = false).gradleProject
val result = build(gradleVersion, project.rootDir, ":app:assembleRelease")
Expand All @@ -118,8 +125,9 @@ class MeasurePluginTest {
@ParameterizedTest
@MethodSource("versions")
fun `assert plugin does not break configuration cache`(
agpVersion: SemVer, gradleVersion: GradleVersion
) {
agpVersion: SemVer,
gradleVersion: GradleVersion,
) {
server.enqueue(MockResponse().setResponseCode(200))
server.enqueue(MockResponse().setResponseCode(200))
server.start(8080)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.autonomousapps.kit.gradle.Plugin
object Plugins {
@JvmStatic
val androidApp: Plugin = Plugin("com.android.application")

@JvmStatic
val measurePlugin: Plugin =
Plugin("sh.measure.android.gradle", AbstractGradleProject.PLUGIN_UNDER_TEST_VERSION, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ class MeasurePluginFixture(
withBuildScript {
buildscript = BuildscriptBlock(
repositories = Repositories(
Repository.MAVEN_CENTRAL, Repository.GOOGLE, Repository.MAVEN_LOCAL
), dependencies = Dependencies(
Repository.MAVEN_CENTRAL,
Repository.GOOGLE,
Repository.MAVEN_LOCAL,
),
dependencies = Dependencies(
Dependency.androidPlugin(agpVersion.toString()),
)
),
)
}
}
Expand All @@ -57,7 +60,7 @@ class MeasurePluginFixture(
targetSdkVersion = 35,
versionCode = 1,
versionName = "1.0",
)
),
)
}

Expand All @@ -77,7 +80,7 @@ class MeasurePluginFixture(
beforeVariants(selector().withBuildType("${variant.buildType}")) {
minifyEnabled = ${variant.minifyEnabled}
}
}"""
}""",
)
}

Expand All @@ -90,7 +93,7 @@ class MeasurePluginFixture(
plugins.add(Plugins.measurePlugin)
android = AndroidBlock.defaultAndroidBlock(name)
withVariant(
Variant(buildType = "release", minifyEnabled = minifyEnabled)
Variant(buildType = "release", minifyEnabled = minifyEnabled),
)
withAndroidManifest(setMeasureApiKey, setMeasureApiUrl)

Expand All @@ -101,15 +104,15 @@ class MeasurePluginFixture(
enabled = $enabled
}
}
""".trimIndent()
""".trimIndent(),
)
}
}
}

private fun AndroidSubproject.Builder.withAndroidManifest(
setApiKey: Boolean,
setApiUrl: Boolean
setApiUrl: Boolean,
) {
val apiKeyMetaData = if (setApiKey) {
"""
Expand Down Expand Up @@ -137,7 +140,7 @@ class MeasurePluginFixture(
$apiUrlMetaData
</application>
</manifest>
""".trimIndent()
""".trimIndent(),
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import com.android.tools.build.bundletool.commands.GetSizeCommand
import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import java.io.File
import java.io.PrintStream
import java.nio.file.Path
import org.gradle.api.tasks.Optional

/**
* Task to measure the size of an app bundle.
Expand Down Expand Up @@ -95,21 +94,21 @@ abstract class AabSizeTask : DefaultTask() {
"""
${lines[1].split(",")[1]}
aab
""".trimIndent()
""".trimIndent(),
)
}

/**
* Finds and returns the location of the aapt2 executable.
*/
private fun getAapt2Location(sdkDirectory: File?): Path {
val sdkLocation = (sdkDirectory ?: File(checkNotNull(System.getenv("ANDROID_HOME")) {
val sdkLocation = sdkDirectory ?: File(checkNotNull(System.getenv("ANDROID_HOME")) {
"Missing 'ANDROID_HOME' environment variable"
}))
})
val sdkHandler =
AndroidSdkHandler.getInstance(AndroidLocationsSingleton, sdkLocation.toPath())
val progressIndicator = object : ProgressIndicatorAdapter() { /* No progress reporting */ }
val buildToolInfo = sdkHandler.getLatestBuildTool(progressIndicator, true)
return buildToolInfo.location.resolve(SdkConstants.FN_AAPT2)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ abstract class ApkSizeTask : DefaultTask() {
"""
$size
apk
""".trimIndent()
""".trimIndent(),
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ abstract class BuildUploadTask : DefaultTask() {
private fun readBuildType(appSizeFile: File): String {
return appSizeFile.readLines()[1]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,28 @@ abstract class ExtractManifestDataTask : DefaultTask() {

if (apiKey == null) {
logger.error(
"[ERROR]: $KEY_API_KEY missing in manifest, Measure SDK will not be initialized."
"[ERROR]: $KEY_API_KEY missing in manifest, Measure SDK will not be initialized.",
)
return
}

if (apiUrl == null) {
logger.error(
"[ERROR]: $KEY_API_URL missing in manifest, Measure SDK will not be initialized."
"[ERROR]: $KEY_API_URL missing in manifest, Measure SDK will not be initialized.",
)
return
}

@Suppress("OPT_IN_USAGE") Json.encodeToStream(
@Suppress("OPT_IN_USAGE")
Json.encodeToStream(
ManifestData(
versionCode = versionCode,
versionName = versionName,
appUniqueId = packageName,
apiKey = apiKey,
apiUrl = apiUrl
), outputFile.outputStream()
apiUrl = apiUrl,
),
outputFile.outputStream(),
)
}

Expand Down Expand Up @@ -104,4 +106,4 @@ abstract class ExtractManifestDataTask : DefaultTask() {
.firstOrNull { it.getAttribute(ATTR_ANDROID_NAME) == KEY_API_URL }
?.getAttribute(ATTR_ANDROID_VALUE)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ data class ManifestData(
val apiUrl: String,
val versionCode: String,
val appUniqueId: String,
val versionName: String
val versionName: String,
)

Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ abstract class MeasureHttpClient : BuildService<MeasureHttpClient.Params>, AutoC
interface Params : BuildServiceParameters {
val timeout: Property<Duration>
}
}
}
Loading
Loading