How to change Gradle in custom build pipeline?

I’m using Unity 6000.0.19f1 and I need to use Gradle 8.7.
I want to set gradle somehow via script before build process starts.

After googling it made this script.

using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEditor;
using UnityEngine;

public class PreProcessBuild: IPreprocessBuildWithReport
{
    public int callbackOrder => 0;

    public void OnPreprocessBuild(BuildReport report)
    {
        string projectPath = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/'));
        string gradlePath = projectPath + "/External/Android/Building/Gradle/gradle-8.7";

        EditorPrefs.SetBool("GradleUseEmbedded", false);
        EditorPrefs.SetString("GradlePath", gradlePath);
    }
}

Yes, it’s changed the path to gradle in Edit → Preferences, but it does not affect the first build.

Will be appreciated for any help.