PHILADELPHIA — Rapper Meek Mill pleaded guilty to a misdemeanor gun charge Tuesday in a deal that resolves a 2007 arrest that has kept him on probation or in prison for most of his adult life.

The negotiated plea comes after an appeals court threw out his conviction last month over doubts about the arresting officer’s credibility. The 32-year-old rapper, born Robert Williams, is now free of a criminal justice system he hopes to reform.

”I know this has been a long road for you and hopefully this will be the end of it,” Judge Leon Tucker told him.

Williams has called the 12-year ordeal “mentally and emotionally challenging,” but said millions of people face the same issues.

”I know you probably got family members in jail, people going through the same thing as me,” Williams told a small crowd as he left the courthouse. “I will continue to do what I do with the reform movement and help the people that helped me.”

He took up the cause after clashing repeatedly with the trial judge who ordered 10 years of probation and sent him back to prison in 2017 for technical violations. He spent five months locked up before the Pennsylvania Superior Court granted him bail and removed her from the case.

District Attorney Larry Krasner’s office supported Williams’ appeal and said it could not call the former officer to testify after the department found he’d stolen money on duty and lied about it. The officer, Reginald Graham, has denied the allegations.

Still, Krasner could not ignore the fact that Williams acknowledged having a gun, though he denied pointing it at police or selling drugs.

Krasner has overseen an office that has backed more than a dozen exonerations but said this is not that type of case. It is one in which Williams was guilty of a gun crime, but was excessively punished, Krasner said.

”Just as Mr. Williams has evolved in the last 10-plus years, the criminal justice system also needs to evolve,” he said.

Kahsim Buey, 23, listened to Williams’ music growing up in North Philadelphia and listened in court Tuesday as he entered his plea. Buey, who spent time in a youth detention center at 14, recently became the first in his family to graduate college and hopes to become a lawyer.

He believes that judges often believe police over defendants.

”Just like the Meek situation. His voice was little at the beginning, but now his voice is big because of the person he is,” Buey said. “I’m very happy for Meek today.”

Buey, a law intern, said he was pulled over by police this month for allegedly running a red light on his bike. The officer searched his drawstring bag for a gun, he said.

”They know our voice is little, so they mess with us,” he said. “That’s why I want to become a lawyer.”

In 2015, a federal jury rejected a lawsuit Williams filed against a Philadelphia police over what he called a racially motivated, 10-hour traffic stop that led him to miss the launch party for his 2012 debut album, “Dreams & Nightmares.”

His follow-up albums include the chart-topping “Dreams Worth More Than Money” and last year’s “Championships,” which includes performances from Jay-Z, his mentor, and former girlfriend Nicki Minaj.

On July 24, just hours before the Pennsylvania Superior Court threw out his conviction, Meek Mill and Jay-Z announced they were launching a new label and starting a $50 million criminal justice reform group.