CommandInvokationFailure: Failed to sign APK Package

CommandInvokationFailure: Failed to sign APK package.
C:\Users\Hp\AppData\Local\Android\Sdk\build-tools\27.0.1\apksigner.bat sign --ks “SOME PERSONAL INFO THAT CANT BE SHARED…”

Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
-Djava.ext.dirs=C:\Users\Hp\AppData\Local\Android\Sdk\build-tools\27.0.1\lib is not supported.  Use -classpath instead.
exit code: 1
UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg)
UnityEditor.Android.PostProcessor.Tasks.TasksCommon.Exec (System.String command, System.String args, System.String workingdir, System.String errorMsg)
UnityEditor.Android.PostProcessor.Tasks.BuildAPK.SignPackage (UnityEditor.Android.PostProcessor.PostProcessorContext context)
UnityEditor.Android.PostProcessor.Tasks.BuildAPK.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context)
UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context)

So, I’ve been trying to fix this issue for 2 days straight. I see that this issue had been around for a while when I checked the internet for solutions but none seems to work out. I tried downgrading my JDK to version 8 and I downgraded my SDK(tool’s folder only) version to r25.2.5.
Here is the weird thing. I wasn’t able to build in debug mode and test the game on my mobile phone before I downgraded my JDK and SDK. I was getting a different error when I tried to build in debug mode. After the downgrading of my SDK and JDK was done I was able to build my game on my mobile device and play it in unsigned(debug) mode. But when I tried to sign the APK and release it on google play I started getting errors above…

Anyone with solutions? I would appreciate it a lot.

So here is what got me thinking:

Error: Could not create the Java Virtual Machine.

I can run the game on my mobile device in unsigned(debug) mode and play it without any errors and when I try to sign the game I get some problems with JVM? That doesn’t make any sense. If there were any problems with JVM I shouldn’t be able to play the game in debug mode either…

I managed to fix the issue by using Gradle. I don’t know how using gradle would be related to signing an apk but it seems to be related after all.