Bayern will host Barca at the Allianz Arena on July 24, a little over three weeks after Guardiola is due to take over from Jupp Heynckes as Bayern coach.
The deal between the two clubs was finalised during the general assembly of the European Club Association in Doha and announced today.
The match will be named the Uli Hoeness Cup after the Bayern president, with all proceeds donated to charity.
"I am really looking forward to this game," Hoeness said on the Bayern website.
"I thank the board for this gift on my 60th birthday. I am particularly pleased about the fact that I can use the proceeds to support social causes."