BARCELONA, Spain (AP) — Bayern Munich secured the transfer of midfielder Thiago Alcantara from Barcelona on Sunday, reuniting him with coach Pep Guardiola.

Barcelona said that Bayern had agreed to pay the Spanish club 25 million euros for Thiago. The deal also included an agreement to play a friendly match between the two clubs at some point in the next four years.

Bayern said that Thiago would sign a four-year contract and that the cost to the German club would only reach €20 million, with proceeds from the friendly and an agreement by Thiago to forego personal payments making up the difference.

Chairman Karl-Heinz Rummenigge said the 22-year-old Thiago "was the great wish of our new coach, Pep Guardiola."

Rummennigge added that Thiago "is a fantastic player with great prospects who will strengthen Bayern," the Bundesliga and Champions League title holder.

Thiago helped Spain's under-21 team win the European championship last month, scoring a hat trick in the final against Italy.

Born in Italy to Brazilian parents, Thiago came up through Barcelona's training academy and debuted for its first team in 2008.

Guardiola made Thiago a fulltime first-team player in 2011-12 and he played in 45 games, but injuries limited him to 36 appearances last season under coach Tito Vilanova as Barcelona won the Spanish league title. Yet despite his displays of playmaking, dribbling and a good shot from distance, he was never able to become a first-choice starter in Barcelona's midfield held down by Andres Iniesta, Xavi Hernandez and Cesc Fabregas.

Guardiola said this week that Thiago was a "super player" and that he was urging Bayern to sign him.

Thiago's contract with Barcelona had a buyout clause of €90 million, but it included the stipulation that if he played in less than 60 percent of the games in a season the buyout price would be lowered to just €18 million.

Barcelona club president Sandro Rosell said on June 13 that he hoped Thiago would "follow the steps of Xavi (Hernandez), (Andres) Iniesta and the other players who have become references for this club."

"I understand that the player wants to play, but even Xavi had to wait. I hope he has the patience that others have shown," he said.

However, playing time would have been even harder to come by at Barcelona with the arrival of Neymar, and Thiago had been linked to moves to several clubs including Manchester United.

Thiago's transfer to Bayern follows the signing of Germany midfielder Mario Goetze from Borussia Dortmund.

Thiago's brother, Rafina, is also under contract with Barcelona and played for its "B-team" in the second division last season.