From 7057fb8a58257c3ea269c50a77943bcc629b9d38 Mon Sep 17 00:00:00 2001 From: Abhay Sood Date: Sat, 28 Dec 2024 06:22:05 +0530 Subject: [PATCH] chore(android): apply spotless to gradle plugin module closes: #519 --- android/measure-android-gradle/README.md | 2 +- .../measure-android-gradle/build.gradle.kts | 46 ++++++++++++++++++- .../kotlin/sh/measure/MeasurePluginTest.kt | 26 +++++++---- .../kotlin/sh/measure/Plugins.kt | 1 + .../measure/fixtures/MeasurePluginFixture.kt | 23 ++++++---- .../src/main/kotlin/sh/measure/AabSizeTask.kt | 11 ++--- .../src/main/kotlin/sh/measure/ApkSizeTask.kt | 4 +- .../main/kotlin/sh/measure/BuildUploadTask.kt | 2 +- .../sh/measure/ExtractManifestDataTask.kt | 14 +++--- .../main/kotlin/sh/measure/ManifestData.kt | 3 +- .../kotlin/sh/measure/MeasureHttpClient.kt | 2 +- .../main/kotlin/sh/measure/MeasurePlugin.kt | 21 +++++---- .../src/main/kotlin/sh/measure/SemVer.kt | 2 +- .../sh/measure/asm/AsmBytecodeTransformer.kt | 7 +-- .../asm/BytecodeTransformationPipeline.kt | 2 +- .../BytecodeTransformationPipelineBuilder.kt | 2 +- .../sh/measure/asm/BytecodeTransformer.kt | 2 +- .../main/kotlin/sh/measure/asm/ModuleInfo.kt | 2 +- .../sh/measure/asm/NavigationTransformer.kt | 26 ++++++++--- .../sh/measure/asm/OkHttpTransformer.kt | 38 ++++++++++----- .../sh/measure/asm/VersionAwareVisitor.kt | 2 +- .../kotlin/sh/measure/utils/StringUtils.kt | 2 +- .../test/kotlin/sh/measure/AabSizeTaskTest.kt | 3 +- .../test/kotlin/sh/measure/ApkSizeTaskTest.kt | 2 +- .../kotlin/sh/measure/BuildUploadTaskTest.kt | 5 +- .../sh/measure/ExtractManifestDataTaskTest.kt | 10 ++-- .../measure/okhttp/OkHttpClassVisitorTest.kt | 2 +- 27 files changed, 176 insertions(+), 86 deletions(-) diff --git a/android/measure-android-gradle/README.md b/android/measure-android-gradle/README.md index d22a67455..51270f3cd 100644 --- a/android/measure-android-gradle/README.md +++ b/android/measure-android-gradle/README.md @@ -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). \ No newline at end of file +the [documentation](../../docs/android/gradle-plugin.md). diff --git a/android/measure-android-gradle/build.gradle.kts b/android/measure-android-gradle/build.gradle.kts index 70c3752ec..69d14d9d4 100644 --- a/android/measure-android-gradle/build.gradle.kts +++ b/android/measure-android-gradle/build.gradle.kts @@ -1,3 +1,4 @@ +import com.diffplug.gradle.spotless.SpotlessExtension import com.vanniktech.maven.publish.GradlePublishPlugin import com.vanniktech.maven.publish.SonatypeHost @@ -37,7 +38,7 @@ mavenPublishing { coordinates(group as String, artifactId, version as String) publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL, automaticRelease = true) configure( - GradlePublishPlugin() + GradlePublishPlugin(), ) pom { @@ -70,6 +71,47 @@ kotlin { jvmToolchain(17) } +extensions.configure("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) @@ -106,4 +148,4 @@ tasks.withType().configureEach { if (this.name.equals("functionalTest")) { useJUnitPlatform() } -} \ No newline at end of file +} diff --git a/android/measure-android-gradle/src/functionalTest/kotlin/sh/measure/MeasurePluginTest.kt b/android/measure-android-gradle/src/functionalTest/kotlin/sh/measure/MeasurePluginTest.kt index a8a711b54..73379fac3 100644 --- a/android/measure-android-gradle/src/functionalTest/kotlin/sh/measure/MeasurePluginTest.kt +++ b/android/measure-android-gradle/src/functionalTest/kotlin/sh/measure/MeasurePluginTest.kt @@ -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") @@ -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") @@ -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") @@ -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") @@ -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) @@ -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") @@ -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") @@ -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) diff --git a/android/measure-android-gradle/src/functionalTest/kotlin/sh/measure/Plugins.kt b/android/measure-android-gradle/src/functionalTest/kotlin/sh/measure/Plugins.kt index 1853e5d2b..166aab3bf 100644 --- a/android/measure-android-gradle/src/functionalTest/kotlin/sh/measure/Plugins.kt +++ b/android/measure-android-gradle/src/functionalTest/kotlin/sh/measure/Plugins.kt @@ -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) diff --git a/android/measure-android-gradle/src/functionalTest/kotlin/sh/measure/fixtures/MeasurePluginFixture.kt b/android/measure-android-gradle/src/functionalTest/kotlin/sh/measure/fixtures/MeasurePluginFixture.kt index b336717fe..dfc8e8e83 100644 --- a/android/measure-android-gradle/src/functionalTest/kotlin/sh/measure/fixtures/MeasurePluginFixture.kt +++ b/android/measure-android-gradle/src/functionalTest/kotlin/sh/measure/fixtures/MeasurePluginFixture.kt @@ -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()), - ) + ), ) } } @@ -57,7 +60,7 @@ class MeasurePluginFixture( targetSdkVersion = 35, versionCode = 1, versionName = "1.0", - ) + ), ) } @@ -77,7 +80,7 @@ class MeasurePluginFixture( beforeVariants(selector().withBuildType("${variant.buildType}")) { minifyEnabled = ${variant.minifyEnabled} } - }""" + }""", ) } @@ -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) @@ -101,7 +104,7 @@ class MeasurePluginFixture( enabled = $enabled } } - """.trimIndent() + """.trimIndent(), ) } } @@ -109,7 +112,7 @@ class MeasurePluginFixture( private fun AndroidSubproject.Builder.withAndroidManifest( setApiKey: Boolean, - setApiUrl: Boolean + setApiUrl: Boolean, ) { val apiKeyMetaData = if (setApiKey) { """ @@ -137,7 +140,7 @@ class MeasurePluginFixture( $apiUrlMetaData - """.trimIndent() + """.trimIndent(), ) } -} \ No newline at end of file +} diff --git a/android/measure-android-gradle/src/main/kotlin/sh/measure/AabSizeTask.kt b/android/measure-android-gradle/src/main/kotlin/sh/measure/AabSizeTask.kt index f6cc5d9f9..645b77742 100644 --- a/android/measure-android-gradle/src/main/kotlin/sh/measure/AabSizeTask.kt +++ b/android/measure-android-gradle/src/main/kotlin/sh/measure/AabSizeTask.kt @@ -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. @@ -95,7 +94,7 @@ abstract class AabSizeTask : DefaultTask() { """ ${lines[1].split(",")[1]} aab - """.trimIndent() + """.trimIndent(), ) } @@ -103,13 +102,13 @@ abstract class AabSizeTask : DefaultTask() { * 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) } -} \ No newline at end of file +} diff --git a/android/measure-android-gradle/src/main/kotlin/sh/measure/ApkSizeTask.kt b/android/measure-android-gradle/src/main/kotlin/sh/measure/ApkSizeTask.kt index 3d5906c62..003d70b22 100644 --- a/android/measure-android-gradle/src/main/kotlin/sh/measure/ApkSizeTask.kt +++ b/android/measure-android-gradle/src/main/kotlin/sh/measure/ApkSizeTask.kt @@ -48,7 +48,7 @@ abstract class ApkSizeTask : DefaultTask() { """ $size apk - """.trimIndent() + """.trimIndent(), ) } -} \ No newline at end of file +} diff --git a/android/measure-android-gradle/src/main/kotlin/sh/measure/BuildUploadTask.kt b/android/measure-android-gradle/src/main/kotlin/sh/measure/BuildUploadTask.kt index 42af608ef..b6e617de2 100644 --- a/android/measure-android-gradle/src/main/kotlin/sh/measure/BuildUploadTask.kt +++ b/android/measure-android-gradle/src/main/kotlin/sh/measure/BuildUploadTask.kt @@ -115,4 +115,4 @@ abstract class BuildUploadTask : DefaultTask() { private fun readBuildType(appSizeFile: File): String { return appSizeFile.readLines()[1] } -} \ No newline at end of file +} diff --git a/android/measure-android-gradle/src/main/kotlin/sh/measure/ExtractManifestDataTask.kt b/android/measure-android-gradle/src/main/kotlin/sh/measure/ExtractManifestDataTask.kt index 71c74c0d0..1b3504ac6 100644 --- a/android/measure-android-gradle/src/main/kotlin/sh/measure/ExtractManifestDataTask.kt +++ b/android/measure-android-gradle/src/main/kotlin/sh/measure/ExtractManifestDataTask.kt @@ -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(), ) } @@ -104,4 +106,4 @@ abstract class ExtractManifestDataTask : DefaultTask() { .firstOrNull { it.getAttribute(ATTR_ANDROID_NAME) == KEY_API_URL } ?.getAttribute(ATTR_ANDROID_VALUE) } -} \ No newline at end of file +} diff --git a/android/measure-android-gradle/src/main/kotlin/sh/measure/ManifestData.kt b/android/measure-android-gradle/src/main/kotlin/sh/measure/ManifestData.kt index 6c1211df5..2af395059 100644 --- a/android/measure-android-gradle/src/main/kotlin/sh/measure/ManifestData.kt +++ b/android/measure-android-gradle/src/main/kotlin/sh/measure/ManifestData.kt @@ -8,6 +8,5 @@ data class ManifestData( val apiUrl: String, val versionCode: String, val appUniqueId: String, - val versionName: String + val versionName: String, ) - diff --git a/android/measure-android-gradle/src/main/kotlin/sh/measure/MeasureHttpClient.kt b/android/measure-android-gradle/src/main/kotlin/sh/measure/MeasureHttpClient.kt index 1ab30303b..b226b1ee2 100644 --- a/android/measure-android-gradle/src/main/kotlin/sh/measure/MeasureHttpClient.kt +++ b/android/measure-android-gradle/src/main/kotlin/sh/measure/MeasureHttpClient.kt @@ -23,4 +23,4 @@ abstract class MeasureHttpClient : BuildService, AutoC interface Params : BuildServiceParameters { val timeout: Property } -} \ No newline at end of file +} diff --git a/android/measure-android-gradle/src/main/kotlin/sh/measure/MeasurePlugin.kt b/android/measure-android-gradle/src/main/kotlin/sh/measure/MeasurePlugin.kt index ac5d3fe5c..e6825afd1 100644 --- a/android/measure-android-gradle/src/main/kotlin/sh/measure/MeasurePlugin.kt +++ b/android/measure-android-gradle/src/main/kotlin/sh/measure/MeasurePlugin.kt @@ -31,7 +31,7 @@ class MeasurePlugin : Plugin { WARNING: Measure gradle plugin can only be applied to Android application projects, that is, projects that have the com.android.application plugin applied. Applying the plugin to other project types has no effect. - """.trimIndent() + """.trimIndent(), ) return } @@ -40,7 +40,8 @@ class MeasurePlugin : Plugin { val androidComponents = project.extensions.getByType(AndroidComponentsExtension::class.java) val sdkDirectory = androidComponents.sdkComponents.sdkDirectory val httpClientProvider = project.gradle.sharedServices.registerIfAbsent( - SHARED_SERVICE_HTTP_CLIENT, MeasureHttpClient::class.java + SHARED_SERVICE_HTTP_CLIENT, + MeasureHttpClient::class.java, ) { spec -> spec.parameters.timeout.set(Duration.ofMillis(DEFAULT_TIMEOUT_MS)) } @@ -67,21 +68,24 @@ class MeasurePlugin : Plugin { sdkDirectory: Provider, ) { val extractManifestDataProvider = project.tasks.register( - extractManifestDataTaskName(variant), ExtractManifestDataTask::class.java + extractManifestDataTaskName(variant), + ExtractManifestDataTask::class.java, ) { it.manifestInputProperty.set(variant.artifacts.get(SingleArtifact.MERGED_MANIFEST)) it.manifestOutputProperty.set(manifestDataFileProvider(project, variant)) } val apkSizeProvider = project.tasks.register( - extractApkSizeTaskName(variant), ApkSizeTask::class.java + extractApkSizeTaskName(variant), + ApkSizeTask::class.java, ) { it.apkDirectoryProperty.set(variant.artifacts.get(SingleArtifact.APK)) it.appSizeOutputFileProperty.set(appSizeFileProvider(project, variant)) } val aabSizeProvider = project.tasks.register( - extractAabSizeTaskName(variant), AabSizeTask::class.java + extractAabSizeTaskName(variant), + AabSizeTask::class.java, ) { it.androidSdkDir.set(sdkDirectory) it.bundleFileProperty.set(variant.artifacts.get(SingleArtifact.BUNDLE)) @@ -90,7 +94,8 @@ class MeasurePlugin : Plugin { } val uploadBuildProvider = project.tasks.register( - buildUploadTaskName(variant), BuildUploadTask::class.java + buildUploadTaskName(variant), + BuildUploadTask::class.java, ) { it.manifestDataProperty.set(manifestDataFileProvider(project, variant)) it.mappingFileProperty.set(variant.artifacts.get(SingleArtifact.OBFUSCATION_MAPPING_FILE)) @@ -110,7 +115,6 @@ class MeasurePlugin : Plugin { } } - // hook up the upload task to run after any assemble or bundle // apkSizeProvider should only run for assemble tasks, while aabSizeProvider should only // run for bundle tasks. @@ -139,7 +143,8 @@ class MeasurePlugin : Plugin { "calculateAabSize${variant.name.capitalize()}" private fun manifestDataFileProvider( - project: Project, variant: Variant, + project: Project, + variant: Variant, ): Provider { return project.layout.buildDirectory.file("intermediates/measure/${variant.name}/manifestData.json") } diff --git a/android/measure-android-gradle/src/main/kotlin/sh/measure/SemVer.kt b/android/measure-android-gradle/src/main/kotlin/sh/measure/SemVer.kt index a706d6438..8c780578f 100644 --- a/android/measure-android-gradle/src/main/kotlin/sh/measure/SemVer.kt +++ b/android/measure-android-gradle/src/main/kotlin/sh/measure/SemVer.kt @@ -60,7 +60,7 @@ data class SemVer( minor = if (result.groupValues[2].isEmpty()) 0 else result.groupValues[2].toInt(), patch = if (result.groupValues[3].isEmpty()) 0 else result.groupValues[3].toInt(), preRelease = if (result.groupValues[4].isEmpty()) null else result.groupValues[4], - buildMetadata = if (result.groupValues[5].isEmpty()) null else result.groupValues[5] + buildMetadata = if (result.groupValues[5].isEmpty()) null else result.groupValues[5], ) } } diff --git a/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/AsmBytecodeTransformer.kt b/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/AsmBytecodeTransformer.kt index c7413e52f..203e6d022 100644 --- a/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/AsmBytecodeTransformer.kt +++ b/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/AsmBytecodeTransformer.kt @@ -40,7 +40,8 @@ abstract class AsmBytecodeTransformer : BytecodeTransformer { project: Project, ) { variant.instrumentation.transformClassesWith( - visitorFactoryClass, InstrumentationScope.ALL + visitorFactoryClass, + InstrumentationScope.ALL, ) { it.minVersion.set(minVersion) it.maxVersion.set(maxVersion) @@ -53,7 +54,7 @@ abstract class AsmBytecodeTransformer : BytecodeTransformer { } } variant.instrumentation.setAsmFramesComputationMode( - FramesComputationMode.COMPUTE_FRAMES_FOR_INSTRUMENTED_METHODS + FramesComputationMode.COMPUTE_FRAMES_FOR_INSTRUMENTED_METHODS, ) } -} \ No newline at end of file +} diff --git a/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/BytecodeTransformationPipeline.kt b/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/BytecodeTransformationPipeline.kt index e9907f1ee..92956f9ae 100644 --- a/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/BytecodeTransformationPipeline.kt +++ b/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/BytecodeTransformationPipeline.kt @@ -17,4 +17,4 @@ class BytecodeTransformationPipeline : BytecodeTransformer { override fun transform(variant: Variant, project: Project) { transformers.forEach { it.transform(variant, project) } } -} \ No newline at end of file +} diff --git a/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/BytecodeTransformationPipelineBuilder.kt b/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/BytecodeTransformationPipelineBuilder.kt index 97390c6f0..db490911a 100644 --- a/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/BytecodeTransformationPipelineBuilder.kt +++ b/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/BytecodeTransformationPipelineBuilder.kt @@ -12,4 +12,4 @@ class BytecodeTransformationPipelineBuilder { } fun build(): BytecodeTransformer = pipeline -} \ No newline at end of file +} diff --git a/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/BytecodeTransformer.kt b/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/BytecodeTransformer.kt index d82582c6f..676f284cf 100644 --- a/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/BytecodeTransformer.kt +++ b/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/BytecodeTransformer.kt @@ -11,4 +11,4 @@ interface BytecodeTransformer { variant: Variant, project: Project, ) -} \ No newline at end of file +} diff --git a/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/ModuleInfo.kt b/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/ModuleInfo.kt index 4fec6d2f2..d0456b30c 100644 --- a/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/ModuleInfo.kt +++ b/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/ModuleInfo.kt @@ -13,4 +13,4 @@ import java.io.Serializable data class ModuleInfo( val group: String, val name: String, -) : Serializable \ No newline at end of file +) : Serializable diff --git a/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/NavigationTransformer.kt b/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/NavigationTransformer.kt index 48b397f1a..519d9c670 100644 --- a/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/NavigationTransformer.kt +++ b/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/NavigationTransformer.kt @@ -19,7 +19,8 @@ class NavigationTransformer : AsmBytecodeTransformer() { override val maxVersion = SemVer(2, 9, 0) } -abstract class NavigationVisitorFactory : AsmClassVisitorFactory, +abstract class NavigationVisitorFactory : + AsmClassVisitorFactory, VersionAwareVisitor { override fun isVersionCompatible( versions: Map, @@ -27,7 +28,10 @@ abstract class NavigationVisitorFactory : AsmClassVisitorFactory not needed as NavHostController is already on the stack. @@ -96,7 +110,7 @@ class NavigationMethodVisitor( "sh/measure/android/navigation/ComposeNavigationCollectorKt", "withMeasureNavigationListener", "(Landroidx/navigation/NavHostController;Landroidx/compose/runtime/Composer;I)Landroidx/navigation/NavHostController;", - false + false, ) super.onMethodExit(opcode) } diff --git a/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/OkHttpTransformer.kt b/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/OkHttpTransformer.kt index a11eed9c3..f8669e071 100644 --- a/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/OkHttpTransformer.kt +++ b/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/OkHttpTransformer.kt @@ -11,13 +11,16 @@ import sh.measure.isVersionCompatible class OkHttpTransformer : AsmBytecodeTransformer() { override val visitorFactoryClass = OkHttpVisitorFactory::class.java + // Tested from 4.7.0, earlier versions do not have all required event factory overrides override val minVersion = SemVer(4, 7, 0) + // Tested up-to 5.0.0-alpha.14 which is the latest version at the time of writing override val maxVersion = SemVer(5, 0, 0) } -abstract class OkHttpVisitorFactory : AsmClassVisitorFactory, +abstract class OkHttpVisitorFactory : + AsmClassVisitorFactory, VersionAwareVisitor { override fun isVersionCompatible( versions: Map, @@ -25,7 +28,10 @@ abstract class OkHttpVisitorFactory : AsmClassVisitorFactory" && descriptor == "(Lokhttp3/OkHttpClient\$Builder;)V") { OkHttpMethodVisitor(Opcodes.ASM9, methodVisitor, access, name, descriptor) - } else methodVisitor + } else { + methodVisitor + } } } class OkHttpMethodVisitor( - apiVersion: Int, originalVisitor: MethodVisitor, access: Int, name: String, descriptor: String, + apiVersion: Int, + originalVisitor: MethodVisitor, + access: Int, + name: String, + descriptor: String, ) : AdviceAdapter( - apiVersion, originalVisitor, access, name, descriptor + apiVersion, + originalVisitor, + access, + name, + descriptor, ) { // Stack changes: @@ -84,21 +100,21 @@ class OkHttpMethodVisitor( "okhttp3/OkHttpClient\$Builder", "getEventListenerFactory\$okhttp", "()Lokhttp3/EventListener\$Factory;", - false + false, ) visitMethodInsn( INVOKESPECIAL, "sh/measure/android/okhttp/MeasureEventListenerFactory", "", "(Lokhttp3/EventListener\$Factory;)V", - false + false, ) visitMethodInsn( INVOKEVIRTUAL, "okhttp3/OkHttpClient\$Builder", "eventListenerFactory", "(Lokhttp3/EventListener\$Factory;)Lokhttp3/OkHttpClient\$Builder;", - false + false, ) // create MeasureOkHttpApplicationInterceptor and add it to the OkHttpClient.Builder @@ -109,14 +125,14 @@ class OkHttpMethodVisitor( "sh/measure/android/okhttp/MeasureOkHttpApplicationInterceptor", "", "()V", - false + false, ) visitMethodInsn( INVOKEVIRTUAL, "okhttp3/OkHttpClient\$Builder", "addInterceptor", "(Lokhttp3/Interceptor;)Lokhttp3/OkHttpClient\$Builder;", - false + false, ) } -} \ No newline at end of file +} diff --git a/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/VersionAwareVisitor.kt b/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/VersionAwareVisitor.kt index ed5ba9953..10f015f46 100644 --- a/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/VersionAwareVisitor.kt +++ b/android/measure-android-gradle/src/main/kotlin/sh/measure/asm/VersionAwareVisitor.kt @@ -33,4 +33,4 @@ interface VersionAwareVisitor : AsmClassVisitorFactor } fun createClassVisitor(nextClassVisitor: ClassVisitor): ClassVisitor -} \ No newline at end of file +} diff --git a/android/measure-android-gradle/src/main/kotlin/sh/measure/utils/StringUtils.kt b/android/measure-android-gradle/src/main/kotlin/sh/measure/utils/StringUtils.kt index 22420929a..b3aaee483 100644 --- a/android/measure-android-gradle/src/main/kotlin/sh/measure/utils/StringUtils.kt +++ b/android/measure-android-gradle/src/main/kotlin/sh/measure/utils/StringUtils.kt @@ -2,4 +2,4 @@ package sh.measure.utils fun String.capitalize(): String { return replaceFirstChar { it.uppercase() } -} \ No newline at end of file +} diff --git a/android/measure-android-gradle/src/test/kotlin/sh/measure/AabSizeTaskTest.kt b/android/measure-android-gradle/src/test/kotlin/sh/measure/AabSizeTaskTest.kt index e9b1722fd..d4d60cefc 100644 --- a/android/measure-android-gradle/src/test/kotlin/sh/measure/AabSizeTaskTest.kt +++ b/android/measure-android-gradle/src/test/kotlin/sh/measure/AabSizeTaskTest.kt @@ -1,6 +1,5 @@ package sh.measure -import com.android.build.api.artifact.SingleArtifact import org.gradle.internal.impldep.org.junit.rules.TemporaryFolder import org.gradle.testfixtures.ProjectBuilder import org.junit.After @@ -50,4 +49,4 @@ internal class AabSizeTaskTest { val bundleFile = File("src/test/resources/test.aab") return bundleFile.copyTo(File(rootDir, "test.aab")) } -} \ No newline at end of file +} diff --git a/android/measure-android-gradle/src/test/kotlin/sh/measure/ApkSizeTaskTest.kt b/android/measure-android-gradle/src/test/kotlin/sh/measure/ApkSizeTaskTest.kt index 2f3a62d5b..aecb6c48d 100644 --- a/android/measure-android-gradle/src/test/kotlin/sh/measure/ApkSizeTaskTest.kt +++ b/android/measure-android-gradle/src/test/kotlin/sh/measure/ApkSizeTaskTest.kt @@ -50,4 +50,4 @@ internal class ApkSizeTaskTest { bundleFile.copyTo(File(rootDir, "test.apk")) return rootDir } -} \ No newline at end of file +} diff --git a/android/measure-android-gradle/src/test/kotlin/sh/measure/BuildUploadTaskTest.kt b/android/measure-android-gradle/src/test/kotlin/sh/measure/BuildUploadTaskTest.kt index cb57b92f9..1a7d723ce 100644 --- a/android/measure-android-gradle/src/test/kotlin/sh/measure/BuildUploadTaskTest.kt +++ b/android/measure-android-gradle/src/test/kotlin/sh/measure/BuildUploadTaskTest.kt @@ -43,7 +43,8 @@ class BuildUploadTaskTest { val buildServiceRegistry = (project as ProjectInternal).services.get(BuildServiceRegistry::class.java) val httpClient = buildServiceRegistry.registerIfAbsent( - "measureHttpClient", MeasureHttpClient::class.java + "measureHttpClient", + MeasureHttpClient::class.java, ) { it.parameters.timeout.set(Duration.ofSeconds(30)) }.get() @@ -93,4 +94,4 @@ class BuildUploadTaskTest { {"apiKey":"api-key","apiUrl":"$url","versionCode":"7575527","appUniqueId":"sh.measure.sample","versionName":"1.23.12"} """.trimIndent() } -} \ No newline at end of file +} diff --git a/android/measure-android-gradle/src/test/kotlin/sh/measure/ExtractManifestDataTaskTest.kt b/android/measure-android-gradle/src/test/kotlin/sh/measure/ExtractManifestDataTaskTest.kt index 2a942e2ef..810364841 100644 --- a/android/measure-android-gradle/src/test/kotlin/sh/measure/ExtractManifestDataTaskTest.kt +++ b/android/measure-android-gradle/src/test/kotlin/sh/measure/ExtractManifestDataTaskTest.kt @@ -33,7 +33,7 @@ internal class ExtractManifestDataTaskTest { task.extractManifestData() val validManifestOutput = """ {"apiKey":"api-key","apiUrl":"api-url","versionCode":"100","appUniqueId":"sh.measure.sample","versionName":"1.0.0"} - """.trimIndent() + """.trimIndent() Assert.assertEquals(validManifestOutput, outputFile.readText()) } @@ -77,7 +77,7 @@ internal class ExtractManifestDataTaskTest { android:value="api-url" /> - """.trimIndent() + """.trimIndent() private val manifestWithoutApiKey = """ @@ -91,7 +91,7 @@ internal class ExtractManifestDataTaskTest { android:value="api-url" /> - """.trimIndent() + """.trimIndent() private val manifestWithoutApiUrl = """ @@ -105,5 +105,5 @@ internal class ExtractManifestDataTaskTest { android:value="api-key" /> - """.trimIndent() -} \ No newline at end of file + """.trimIndent() +} diff --git a/android/measure-android-gradle/src/test/kotlin/sh/measure/okhttp/OkHttpClassVisitorTest.kt b/android/measure-android-gradle/src/test/kotlin/sh/measure/okhttp/OkHttpClassVisitorTest.kt index f5f59555b..fbd76f4cf 100644 --- a/android/measure-android-gradle/src/test/kotlin/sh/measure/okhttp/OkHttpClassVisitorTest.kt +++ b/android/measure-android-gradle/src/test/kotlin/sh/measure/okhttp/OkHttpClassVisitorTest.kt @@ -15,7 +15,7 @@ class OkHttpClassVisitorTest { @Test fun `modified bytecode is valid`() { val inputStream = FileInputStream( - "src/test/resources/OkHttpClient.class" + "src/test/resources/OkHttpClient.class", ) val classReader = ClassReader(inputStream) val classWriter = ClassWriter(classReader, ClassWriter.COMPUTE_FRAMES)