Skip to content

Commit

Permalink
VBLOCKS-2853: Updated agp version (#19)
Browse files Browse the repository at this point in the history
Updated to no longer use deprecated features and to use AGP 8.3.0
  • Loading branch information
afalls-twilio authored Mar 20, 2024
1 parent b35d461 commit 88ca8cb
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 35 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# Changelog
### 0.1.7 (Mar 19, 2024)

Updated Requirements

- Updated to use gradle `8.4` and android.build.tools `8.3.0`.
- Removed/replaced deprecated methods.

### 0.1.6 (July 12, 2023)

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ nexusPublishing {

dependencies {
implementation 'com.google.code.gson:gson:2.8.8'
implementation 'org.gradle:gradle-tooling-api:7.3-20210825160000+0000'
implementation gradleApi()
implementation 'com.android.tools.build:gradle:8.0.2'
implementation 'com.android.tools.build:gradle-api:8.3.0'
implementation 'com.android.tools.build:gradle:8.3.0'
implementation 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21'

testImplementation gradleTestKit()
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Wed Jul 22 10:53:58 CDT 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
Expand Down
5 changes: 4 additions & 1 deletion src/main/kotlin/com/twilio/apkscale/ApkscaleGradlePlugin.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.twilio.apkscale

import com.android.build.api.variant.LibraryAndroidComponentsExtension
import com.android.build.gradle.LibraryExtension
import com.twilio.apkscale.tasks.MeasureAndroidLibrarySizeTask
import org.gradle.api.Plugin
Expand All @@ -9,8 +10,10 @@ class ApkscaleGradlePlugin : Plugin<Project> {
override fun apply(project: Project) {
val libraryExtension = project.extensions.findByType(LibraryExtension::class.java)
?: throw error("Apkscale can only be used with an Android Library.")
val componentsExtension = project.extensions.findByType(LibraryAndroidComponentsExtension::class.java)
?: throw error("Apkscale can only be used with an Android Library.")
val apkscaleExtension = project.extensions.create("apkscale", ApkscaleExtension::class.java)

MeasureAndroidLibrarySizeTask.create(project, libraryExtension, apkscaleExtension)
MeasureAndroidLibrarySizeTask.create(project, libraryExtension, componentsExtension, apkscaleExtension)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.twilio.apkscale.tasks

import com.android.build.api.variant.LibraryAndroidComponentsExtension
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.api.LibraryVariant
import com.google.gson.Gson
import com.twilio.apkscale.ApkscaleExtension
import com.twilio.apkscale.model.ApkscaleReport
import org.gradle.api.DefaultTask
import org.gradle.api.DomainObjectSet
import org.gradle.api.Project
import org.gradle.api.artifacts.DependencySet
import org.gradle.api.tasks.TaskAction
Expand All @@ -31,7 +30,21 @@ open class MeasureAndroidLibrarySizeTask @Inject constructor(
companion object {
const val MEASURE_TASK_NAME = "measureSize"

fun create(project: Project, libraryExtension: LibraryExtension, apkscaleExtension: ApkscaleExtension) {
fun create(
project: Project,
libraryExtension: LibraryExtension,
componentsExtension: LibraryAndroidComponentsExtension,
apkscaleExtension: ApkscaleExtension,
) {
// register to capture variants
val variantDependencies = HashMap<String, DependencySet>()
componentsExtension.onVariants {
variantDependencies.put(
it.name,
it.compileConfiguration.allDependencies,
)
}
// after evaluation step assign
project.afterEvaluate {
val measureTask = project.tasks.create(
MEASURE_TASK_NAME,
Expand All @@ -40,34 +53,23 @@ open class MeasureAndroidLibrarySizeTask @Inject constructor(
apkscaleExtension.humanReadable,
libraryExtension.defaultConfig.minSdkVersion?.apiLevel,
libraryExtension.defaultConfig.targetSdkVersion?.apiLevel,
getVariantDependencies(libraryExtension.libraryVariants),
variantDependencies,
libraryExtension.ndkVersion ?: "",
)

// Ensure that measure task runs after assemble tasks
measureTask.mustRunAfter(project.tasks.named("assemble"))
libraryExtension.buildTypes.forEach {
measureTask.mustRunAfter(project.tasks.named("assemble${it.name.capitalize()}"))
measureTask.mustRunAfter(project.tasks.named("assemble${it.name.capitalizeLocalAware()}"))
}
libraryExtension.libraryVariants.forEach {
measureTask.mustRunAfter(project.tasks.named("assemble${it.name.capitalize()}"))
variantDependencies.forEach {
measureTask.mustRunAfter(project.tasks.named("assemble${it.key.capitalizeLocalAware()}"))
}
}
}

private fun getVariantDependencies(libraryVariants: DomainObjectSet<LibraryVariant>): Map<String, DependencySet> {
/*
* Create a map of the library variants to the variant's dependencies so that apkscale pulls in the
* correct dependencies for each variant that is measured.
*/
return libraryVariants.associate {
it.name.lowercase(Locale.getDefault()) to it.compileConfiguration.allDependencies
}
}
}

private val outputAarDir = project.buildDir.resolve("outputs/aar")
private val apkscaleDir = File("${project.buildDir}/apkscale")
private val outputAarDir = project.layout.buildDirectory.dir("outputs/aar").get().asFile
private val apkscaleDir = project.layout.buildDirectory.dir("apkscale").get().asFile
private val appMainDir = File("$apkscaleDir/src/main")
private val apkscaleOutputDir = File("$apkscaleDir/build/outputs/reports")
private val buildFile = File(apkscaleDir, "build.gradle")
Expand Down Expand Up @@ -197,7 +199,7 @@ open class MeasureAndroidLibrarySizeTask @Inject constructor(
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.0.2'
classpath 'com.android.tools.build:gradle:8.3.0'
}
}
apply plugin: 'com.android.application'
Expand Down Expand Up @@ -309,6 +311,13 @@ open class MeasureAndroidLibrarySizeTask @Inject constructor(
return aarFileName.substringAfter("${project.name}-")
.substringBefore(".aar")
.replace("-", "")
.lowercase(Locale.getDefault())
}
}

fun String.capitalizeLocalAware(): String {
return this.replaceFirstChar {
if (it.isLowerCase()) {
it.titlecase(Locale.getDefault())
} else it.toString()
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/tasks/MeasureAndroidLibrarySizeTaskTest.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package tasks

import com.twilio.apkscale.tasks.MeasureAndroidLibrarySizeTask
import junit.framework.Assert.assertEquals
import junitparams.JUnitParamsRunner
import junitparams.Parameters
import org.gradle.api.artifacts.DependencySet
import org.gradle.testfixtures.ProjectBuilder
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

Expand Down
16 changes: 8 additions & 8 deletions src/test/kotlin/util/AndroidLibraryProject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class AndroidLibraryProject(
mavenLocal()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.0.2'
classpath 'com.android.tools.build:gradle:8.3.0'
}
}
plugins {
Expand All @@ -73,12 +73,12 @@ class AndroidLibraryProject(
}
${resolveApkscaleConfig()}
android {
compileSdkVersion 31
compileSdkVersion 33
${resolveNdkVersion()}
namespace 'com.twilio.apkscale'
defaultConfig {
minSdkVersion 21
targetSdkVersion 31
targetSdkVersion 33
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
Expand Down Expand Up @@ -112,11 +112,11 @@ class AndroidLibraryProject(

private fun resolveApkscaleConfig(): String {
return """
apkscale {
${resolveApkscaleAbis()}
humanReadable = $humanReadable
}
""".trimIndent()
apkscale {
${resolveApkscaleAbis()}
humanReadable = $humanReadable
}
"""
}

private fun resolveApkscaleAbis(): String {
Expand Down

0 comments on commit 88ca8cb

Please sign in to comment.