William J. Bratton, the city’s 42nd police commissioner, said Tuesday he would step down from the department in mid-September to go into private industry, capping a 45-year career in policing which has been innovative and controversial at a time of constant challenges to law enforcement around the country.

Taking over from Bratton, 68, will be Chief of Department James O’Neill, 58, a respected commander who is credited with devising the NYPD’s new strategy of neighborhood policing, a linchpin in mending the department’s frayed relationships with communities of color.

“We celebrate a transition filled with continuity, filled with shared vision,” Mayor Bill de Blasio said at a City Hall news conference at which he was flanked by Bratton and O’Neill.

De Blasio called Bratton’s contribution to the city and the nation as “literally inestimable and extraordinary” and said O’Neill, a native of Flatbush, Brooklyn, was one of “the best prepared incoming police commissioners this city has ever seen with decades of experience.”

“I try to leave each place with the bus headed in the right direction, with the right people on the bus, before I go, three or four people who are qualified to drive that bus,” said Bratton, who appointed O’Neill as chief of department in 2014.

O’Neill, who has been with the NYPD for 33 years, said the department’s success will be shaped by its ability to work with the communities.

advertisement | advertise on newsday

“With the help of every New Yorker we will continue to facilitate all the good that is done daily by law abiding residents in our neighborhoods,” said O’Neill, whose new job will pay $208,000, officials said.

Bratton is leaving at a time when major crimes in the city are at historic lows, with murders down about 5 percent and shootings down nearly 20 percent from last year. But it also is a time of increased violence against police, which in late 2014 led to the assassinations of Det. Wenjein Liu and Det. Rafael Ramos and has resulted in police killings in Texas, Louisiana and elsewhere. The commissioner was embarrassed when rank-and-file officers turned their backs on de Blasio at Liu’s and Ramos’ funerals. He called the defiant protests inappropriate.

Bratton has also been dogged by criticism that his “Broken Windows” policy of going after quality-of-life offense victimizes minorities. Bratton also had to weather criticism after the alleged police chokehold death of Eric Garner in 2014 on Staten Island and has had to take disciplinary action against some former commanders embroiled in a federal corruption probe involving political contributors, which has spilled over to City Hall. Bratton said the federal probe had nothing to do with his decision to leave the department, noting his good relationship with Manhattan U.S. Attorney Preet Bharara.

Both Bratton and de Blasio wouldn’t say where Bratton will work next. But later Tuesday the global consulting firm Teneo Holdings, a relatively new firm in the field based in New York and London, announced on its website Bratton would join the company in Manhattan. Bratton will start a risk management operation, the company said.

De Blasio and Bratton said most of the current NYPD administration will remain when Bratton leaves Sept. 16. Among those remaining will be First Deputy Commissioner Benjamin Tucker and Counter Terrorism Chief John Miller.

Taking over O’Neill’s job as chief of department, the highest-ranking uniform position, will be Chief of Patrol Carlos Gomez, a Suffolk County resident who immigrated as a child from Cuba in 1968 at age 6. He asked that his hometown not be identified.

“A bit of surprise, you know, it’s a great position, a great honor,” Gomez told Newsday.

Last year, Bratton had made no secret he didn’t intend to stay on even if de Blasio was re-elected. But even as he told reporters last week that he wouldn’t stay beyond 2017, Bratton had already given notice on July 8 to the mayor after a crime briefing, de Blasio said.

advertisement | advertise on newsday

The fact that Bratton had already resigned was one of the most closely guarded secrets in the de Blasio administration and wasn’t even known by many of the commissioner’s deputies until 10 a.m. Tuesday. Monday night Bratton attended a regular dinner at Wolfgang’s Steakhouse on Greenwich Street with federal law enforcement officials, including the two city U.S. attorneys and special agents in charge of various agencies, telling none of them about his impending announcement.

Bratton said he had to leave the dinner when O’Neill called to say de Blasio wanted to talk with him, apparently about the secret succession plan. O’Neill said he received a call from de Blasio about 8 p.m. telling him of his selection as the new commissioner.

“Jimmy will be taking my place at the next dinner with them,” chuckled Bratton, as his wife Rikki Klieman beamed in the audience.

At a Tuesday night National Night Out event in Brownsville, Brooklyn, with Bratton, O’Neill and de Blasio, Brooklyn Borough President Eric Adams, a retired NYPD captain, praised Bratton’s two stints as commissioner, saying he “changed the mindset of policing” in New York and across the nation. He said O’Neill is “a cop’s cop, a people person” who leaves the city’s security in the right place, the right time with the right man to take us in the right direction.”

When Bratton, de Blasio and O’Neill took the stage in Brownsville, looking out on several dozen people, the crowd clapped. One woman booed but only for a moment.

advertisement | advertise on newsday

Bratton succeeded Ray Kelly in 2014 at a time when the department’s relationship with minority communities was badly fractured by its stop and frisk tactics, which a federal judge found to have treated blacks and Hispanics unfairly. Bratton set out to mend fences, drastically pulling back on stop and frisk, which he compared to a medicine no longer needed in a city where crime continued to drop.

Faced with sudden spikes in shootings in 2014 and 2015, Bratton had O’Neill devise strategies to curb the gunfire by pulling cops from desk jobs to patrol troubled precincts. As it became apparent that the Islamic State terror group posed a new threat, Bratton got de Blasio to add more than 1,300 new police officers and deploy many to the new, heavily armed Critical Response Command and Strategic Response Group. Bratton also embraced technology and gave smartphones to all officers, employed ShotSpotter to pinpoint gunfire and made Compstat statistics more widely available to the public.

Bratton also shook up the NYPD structure, reorganizing units and making Chief of Detectives Robert Boyce the overall investigative leader for the department.

Reaction was mixed among the public.

Amarimba Charles, 40, said she was “sort of shocked, in a bad way” to hear Bratton is leaving.

“I’m hoping we do move in the right direction,” she said, adding she was pleased with Bratton’s performance and that she’d embrace O’Neill’s tenure.

“One thing we need is more activities with the police and community,” she said. “There’s a lot of room for improvement.”

The Rev. Steve Kalloo, pastor with the Christian Visions Ministry in Brownsville, a former Trinidad police officer for 15 years, said he was “no fan of Bratton” and was happy the commissioner is stepping down. “Eric Garner should be alive today, I blame Bratton for that.”

“If you want better relationships, you have to pull down the blue wall of silence,” he said. But he hopes O’Neill can be better.

“A police that comes through the ranks will make a better commissioner,” he said.

Andrea Butler, 55, came to national night out with her grandchildren. She was upset to hear Bratton is leaving the department.

“For the short time, he was doing a good job,” she said. But she was hopeful with O’Neill: “You have to give everyone a chance. We’ll see how it goes. I’m going to think positive.”

At Tuesday’s news conference, O’Neill said the department in the past had sometimes “lost focus” and that the administration was committed to changing “our posture and getting on the right course and lowering crime but not at the expense of losing support.”

Bratton leaves after spending 31 months on the job. He had also served as commissioner under Mayor Rudy Giuliani from 1994 through 1996, but left amid reports that the mayor resented Bratton’s high media profile. Bratton and the late Jack Maple pioneered the CompStat crime data system, widely credited with transforming policing. That departure was on Bratton’s mind Tuesday as he quipped that Giuliani had given him the key to the city but “He didn’t tell me he had already changed the locks.”

After his first term as commissioner ended in 1996, Bratton bounced around private industry and landed the police chief job in Los Angeles in 2002, a position he held until 2007. He tried at one point without success to become head of Scotland Yard.

As Bratton and de Blasio left City Hall after Tuesday’s news conference, they were met by demonstrators.