Get all the latest news on coronavirus and more delivered daily to your inbox. Sign up here.

While under indictment for corruption charges, Prime Minister Benjamin Netanyahu may form a new government, Israel’s Supreme Court ruled Wednesday

The unanimous decision, released just before midnight, ended a 17-month political stalemate and prevented the country from plunging into a fourth consecutive election in just over a year. Netanyahu and his rival-turned-uneasy ally, Benny Gantz, said they expected their controversial power-sharing coalition to be sworn into office next week.

After battling to three inconclusive elections over the past year, Netanyahu and Gantz, a former military chief, announced their “emergency” government last month, saying they would put aside their rivalry to steer the country through the coronavirus pandemic.

CORONAVIRUS: WHAT YOU NEED TO KNOW

Supporters of Prime Minister Benjamin Netanyahu wear a masks amid concerns over the country's coronavirus outbreak during a protest by in front of Israel's Supreme Court in Jerusalem, Sunday, May 3, 2020.  (AP Photo/Tsafrir Abayov)

Supporters of Prime Minister Benjamin Netanyahu wear a masks amid concerns over the country's coronavirus outbreak during a protest by in front of Israel's Supreme Court in Jerusalem, Sunday, May 3, 2020.  (AP Photo/Tsafrir Abayov)

Critics and good-government groups said the deal was illegal and challenged it in the Supreme Court. They argued that the law should bar an official charged with serious crimes from continuing as prime minister. They also objected to the newly created position of “alternate prime minister,” a post that could allow Netanyahu to remain in office throughout his corruption trial and a potential appeals process.

Over two days this week, the court looked at two questions: whether an indicted politician can be given authority to form a new government, and whether the power-sharing deal — which includes new legislation — was legal.

In its decision, the 11-judge panel expressed misgivings about the coalition agreement and Netanyahu’s criminal indictment, but found no grounds to prevent the government from taking office.

“We did not find any legal reason to prevent MK (Member of Knesset) Netanyahu from forming a government,” the court said.

“The legal conclusion we reached does not diminish the severity of the pending charges against MK Netanyahu for violations of moral integrity and the difficulty derived from the tenure of a prime minister accused of criminal activity,” it added.

The judges ruled that while the coalition deal presents significant legal difficulties, the court would not interfere in its contents following changes submitted by Netanyahu and Gantz.

Netanyahu has been indicted on charges of fraud, breach of trust and accepting bribes in a series of scandals involving trading favors with wealthy media moguls. His trial is set to start later this month.

Netanyahu denies any wrongdoing, and since his indictment last fall, he has repeatedly lashed out at the country’s legal system. He and his political allies have taken special aim at the high court, accusing it of overreach and political interference.

As the court was meeting earlier this week, Netanyahu urged it not to get involved in political affairs, lest it risk dragging the country toward new elections. The long-time leader’s opponents consider the court a bastion of democracy under dangerous assault and had expressed hope it would strike down what they see as a deal that undermines the public’s faith in government.

Under the deal, Netanyahu and Gantz would be sworn in together, with Netanyahu serving first as prime minister and Gantz as the designated premier. After 18 months, the two are to swap positions. The new position will enjoy all the trappings of the prime minister, including an official residence and, key for Netanyahu, an exemption from a law that requires public officials who are not prime minister to resign if charged with a crime.

Netanyahu is eager to remain in office throughout his trial, using his position to lash out at the judicial system and rally support among his base. The coalition deal also gives him influence over key judicial appointments, creating a potential conflict of interest during an appeals process if he is convicted. His trial was postponed in March due to restrictions his hand-picked interim justice minister placed on the courts after the coronavirus crisis erupted and is scheduled to commence later this month.

The court said Netanyahu would need to abide by a conflict of interest arrangement while prime minister whenever dealing with law enforcement affairs.

CLICK HERE FOR FULL CORONAVIRUS COVERAGE

An Israeli protester wears a mask amid concerns over the country's coronavirus outbreak looks on a poster of Prime Minister Benjamin Netanyahu during a protest by supporters of Netanyahu in front of Israel's Supreme Court in Jerusalem, Sunday, May 3, 2020. (AP Photo/Tsafrir Abayov)

An Israeli protester wears a mask amid concerns over the country's coronavirus outbreak looks on a poster of Prime Minister Benjamin Netanyahu during a protest by supporters of Netanyahu in front of Israel's Supreme Court in Jerusalem, Sunday, May 3, 2020. (AP Photo/Tsafrir Abayov)

Under the deal, the sides agreed not to take immediate action on key appointments and prioritize legislation focusing on reviving Israel’s economy from the coronavirus crisis.

But it makes an exception for Netanyahu to press ahead with plans to annex large parts of the West Bank, including all of Israel’s dozens of settlements there. Netanyahu is permitted to push the issue through parliament after July 1, even without Gantz’s support.

Netanyahu and his hardline supporters are eager to annex the territory while the friendly administration of President Trump is in office. The Palestinians and most of the international community oppose annexation and say it would end any lingering hopes of a peace deal.

The Associated Press contributed to this report.