Unity cloud build fails after switching from Unity Ads to Unity Mediation

As title says, I had unity ads which worked perfectly, we decided to switch to unity mediation, I followed all the steps and it works in editor, but builds fail.

20: [2022-12-24T10:07:02Z - Unity] EXCEPTION: BuildFailedException: Build canceled
21: [2022-12-24T10:07:02Z - Unity] BuildFailedException: Build canceled
22: [2022-12-24T10:07:02Z - Unity] ERROR: Error building Player: BuildFailedException: Build canceled
23: [2022-12-24T10:07:02Z - Unity] Error building Player: BuildFailedException: Build canceled
24: [2022-12-24T10:07:03Z - Unity] Finished exporting player successfully.
25: [2022-12-24T10:07:34.260Z] - 7.2.4.2.4.2 - ERROR: Build failed - export directory is empty!
26: RuntimeError (Export directory is empty):
27: [2022-12-24T10:10:13.986Z] - 7.2.4.2.4.2 - WARN: Unity Version is different from previous build.
28: Finished: FAILURE

Finally fixed it. All things you should try:

  1. Follow this great youtube tutorial:
    https://www.youtube.com/watch?v=-q01DJdEQuM

If it doesn’t work then try these:

  1. Delete old advertisment package and switch off ads in services menu
  2. Go to Edit>Project Settings>Player>Publishing settings, turn on: Custom main manifest, Custom main gradle template, Custom gradle properties template.
  3. Go to Assets>External Dependency Manager>Android Resolver>Resolve
  4. If using unity cloud build, set building version of unity to currently closest version to yours from this link: Unity Documentation
  5. Go to Edit>Project Settings>Player>Other settings find minimum api level and set it to 21 or above. (this was my problem)