From 134c895cd426599796e4b534e26b21ac2eb3b17d Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Fri, 20 Oct 2023 07:49:58 +0200 Subject: [PATCH] The .profm sorting in build.gradle instead of excluding them from the build Signed-off-by: Balazs Toldi --- app/build.gradle | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index fdaee058..234240d3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -121,11 +121,6 @@ android { } } } - tasks.whenTaskAdded { - if (name.contains("ArtProfile")) { - enabled = false - } - } } @@ -275,8 +270,34 @@ dependencies { //debugImplementation 'com.squareup.leakcanary:leakcanary-android:x.y' } +// NB: Android Studio can't find the imports; this does not affect the +// actual build since Gradle can find them just fine. -java { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 +import com.android.tools.profgen.ArtProfileKt +import com.android.tools.profgen.ArtProfileSerializer +import com.android.tools.profgen.DexFile + +project.afterEvaluate { + tasks.each { task -> + if (task.name.startsWith("compile") && task.name.endsWith("ReleaseArtProfile")) { + task.doLast { + outputs.files.each { file -> + if (file.name.endsWith(".profm")) { + println("Sorting ${file} ...") + def version = ArtProfileSerializer.valueOf("METADATA_0_0_2") + def profile = ArtProfileKt.ArtProfile(file) + def keys = new ArrayList(profile.profileData.keySet()) + def sortedData = new LinkedHashMap() + Collections.sort keys, new DexFile.Companion() + keys.each { key -> sortedData[key] = profile.profileData[key] } + new FileOutputStream(file).with { + write(version.magicBytes$profgen) + write(version.versionBytes$profgen) + version.write$profgen(it, sortedData, "") + } + } + } + } + } + } } \ No newline at end of file