MADRID — With a guilty plea and a huge fine, Cristiano Ronaldo finally put an end to his tax ordeal in Spain.

Nearly four years after an investigation was opened, Ronaldo appeared at a court in Madrid on Tuesday to plead guilty to tax fraud and agree to a fine of nearly 19 million euros ($21.6 million).

The Juventus forward, who was facing charges stemming from his days at Real Madrid, spent about 45 minutes in court to sign the agreement. He was also given a two-year suspended sentence.

Ronaldo will not have to serve time in prison because judges in Spain can suspend sentences for two years or less for first-time offenders.

The Portugal star smiled broadly after leaving court despite the unwanted trip back to Madrid, where he played until last summer.

"It's done," he said briefly as he walked past some of the hundreds of journalists outside the court house.

He later posted a photo on Twitter of himself smiling while apparently taking a selfie inside an airplane. The picture was accompanied by emoticons that included a "thumbs-up."

Ronaldo arrived in court in a black van and was wearing sunglasses, a black sports coat, black pants and white tennis shoes. He stopped to sign an autograph before walking up some stairs leading to the court house. He was accompanied by his lawyers and his partner, Georgina Rodriguez. Police officers escorted him.

The court had dismissed Ronaldo's request to enter the building directly from the parking lot, which would have allowed him to avoid the media. The request was made for security reasons, but the court said it didn't think the measure was needed despite the player's notoriety.

Court officials said Ronaldo didn't make any comments while in the courtroom to sign the agreement.

On his way out of the court house, he again stopped to sign autographs for a few fans who were wedged among the journalists. He also made a thumbs-up sign as people gathered near the court.

Ronaldo made the deal to plead guilty with Spain's state prosecutor and tax authorities last year.

The investigation reportedly began in 2015, and two years later a state prosecutor accused Ronaldo of four counts of tax fraud from 2011-14 worth 14.7 million euros ($16.7 million). Ronaldo was accused of having used shell companies outside Spain to hide income made from image rights.

The accusations didn't involve his salary from Real Madrid, his club from 2009 until joining Italian champion Juventus last year. Ronaldo played for Juventus the night before his court appearance in Madrid, helping the Serie A leader earn a 3-0 win against last-place Chievo Verona.

In 2017, Ronaldo spent about 90 minutes answering questions in court and told a judge he never tried to avoid paying taxes.

Separately, Ronaldo is facing a rape allegation in the United States.

Kathryn Mayorga filed a civil lawsuit in Nevada in September claiming Ronaldo raped her in his Las Vegas hotel room in 2009. Police reopened an investigation into the allegation at her request. Ronaldo has denied any wrongdoing.

Former Real Madrid player Xabi Alonso also was in court in Madrid on Tuesday. He is accused of defrauding tax authorities of about 2 million euros ($2.3 million) from 2010-12.

The retired Spain midfielder denied any wrongdoing after spending nearly three hours in the court house.

"I've done everything correctly, never tried to hide anything," he said. "I'm confident justice will be done."

Alonso had initially been found innocent but the case against him was reopened.

He could be sentenced to five years in prison and ordered to pay a fine of 4 million euros ($4.5 million), in addition to the amount allegedly defrauded. The charges are related to Alonso's income from image rights. He played for Madrid from 2009-14.

Several other soccer figures have been subjected to investigations from tax authorities in Spain in recent years, including Lionel Messi, Jose Mourinho, Javier Mascherano, Marcelo, Luka Modric, Alexis Sanchez, Ricardo Carvalho, Angel Di Maria, Radamel Falcao and Fabio Coentrao.

Messi was found guilty three years ago, along with his father, on three counts of defrauding tax authorities of 4.1 million euros (then $4.6 million) on income made from image rights.