From a69f74f51bf49d0bdbfd83502d0a01ebc1d1752e Mon Sep 17 00:00:00 2001 From: Stypox Date: Fri, 20 Jan 2023 18:39:16 +0100 Subject: [PATCH] Add snippet to ensure baseline.profm file is sorted Thanks to obfusk, see https://issuetracker.google.com/issues/231837768 and #6486 --- app/build.gradle | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/app/build.gradle b/app/build.gradle index 79e07a190..2c0f03e38 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,3 +1,7 @@ +import com.android.tools.profgen.ArtProfileKt +import com.android.tools.profgen.ArtProfileSerializer +import com.android.tools.profgen.DexFile + plugins { id "com.android.application" id "kotlin-android" @@ -308,3 +312,24 @@ static String getGitWorkingBranch() { return "" } } + +project.afterEvaluate { + tasks.compileReleaseArtProfile.doLast { + outputs.files.each { file -> + if (file.toString().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, "") + } + } + } + } +}