JERUSALEM (AP) — Israeli Prime Minister Benjamin Netanyahu and his chief rival announced Monday that they have forged a deal to form a coalition government, ending months of political paralysis and averting what would have been a fourth consecutive election in just over a year.

Netanyahu and former military chief Benny Gantz, leader of the Blue and White party, signed the power-sharing agreement after weeks of negotiations for what they termed a “national emergency” government meant to steer the country through the coronavirus outbreak.

Although Netanyahu repeatedly came up short in three elections over the past year, the coalition agreement returns the long-serving leader to the premiership, defying critics who predicted his downfall and restoring his reputation as a political wizard. It means that Netanyahu will almost certainly remain in office for the duration of his upcoming trial on corruption charges.

The deal calls for a three-year period, with Netanyahu serving as prime minister for the first half, and Gantz taking the job for the second half.

Gantz's party will take control of a number of senior government ministries, including defense.

The agreement delivers Netanyahu a significant boost as he fights to hold on to power while fending off corruption charges. His party will gain influence over judicial appointments. The deal also requires the approval of both parties on key appointments including the attorney general and the state prosecutor — granting Netanyahu veto power over the officials who hold sway over his legal fate.

“I promised the state of Israel a national emergency government that will work to save the lives and livelihoods of the citizens of Israel,” Netanyahu tweeted.

After March 2 elections left both men short of a required parliamentary majority, Netanyahu and Gantz agreed to try to form a unity government because of the burgeoning coronavirus crisis. The talks stalled several times, reportedly over Netanyahu’s personal legal problems, sparking concern that they would plunge the country into new elections.

The deal required major compromise by both men. During three bitter campaigns over the past year, Gantz and his Blue and White party vowed never to serve in a government under Netanyahu so long as he faces a slew of corruption charges. After 11 years as prime minister, Netanyahu agreed to step aside and allow Gantz to take the job, if the coalition manages to survive long enough.

“We prevented a fourth election,” Gantz tweeted. “We will protect democracy. We will battle corona and we will worry about all the citizens of Israel.”

While the government was ostensibly formed to deal with the coronavirus pandemic, which has killed over 170 Israelis and ravaged the economy, negotiations revolved largely around Netanyahu's corruption trial, set to start next month. Main sticking points included a demand by the prime minister to have more say on judicial appointments, which could play a role if his case eventually reaches the Supreme Court.

Political analyst Avraham Diskin, who was involved in crafting some of the coalition deal, said guarantees by both sides were critical. Netanyahu wanted a guarantee he would not be forced to resign after Gantz takes over as prime minister. Israeli law requires all public officials, with the exception of the prime minister, to step down if charged with a crime.

For Blue and White, that meant assurances that Netanyahu wouldn’t topple the government before Gantz becomes prime minister, Diskin said.

“There was a total lack of trust,” he said. “I pray that the government will hold up and won't miss the opportunity with fights over nonsense.”

In a joint statement, the parties said they would form a six-month “emergency” government focused on the coronavirus crisis. In the meantime, they will work on a “policy outline” for a long-term unity government.

The deal could clear the way for Netanyahu's campaign promise to annex large parts of the West Bank after July 1. Gantz has raised concerns about the plan, which is bitterly opposed by the Palestinians and much of the international community, saying it could only take place with broad international support.

Palestinian official Saeb Erekat said the new government would have two options: “to either open the doors for a meaningful peace process or to further jeaopardize any hope for peace.”

Last month’s election, just like the campaigns last September and April, ended with no clear winner. But with a slight majority of lawmakers endorsing him, Gantz was chosen first by the country's figurehead president to try to build a coalition government.

The glue holding together Gantz's different backers was their shared animosity toward Netanyahu. Gantz began to move forward with legislation that would have disqualified the indicted Netanyahu from serving as prime minister in the future.

But with the virus crisis worsening, and his own shaky alliance fraying, Gantz made an about-face late last month and accepted an offer from Netanyahu to pursue a joint government to deal with the pandemic. The move drew heavy criticism from Gantz’s supporters and tore apart his Blue and White alliance.

Negotiations continued even after Gantz’ allotted time to build a coalition ended last week. The Knesset, or parliament, had been given until May 7 to select a candidate for prime minister. Otherwise, it would automatically have been dissolved and triggered new elections.

On Sunday night, several thousand demonstrators, including Gantz’s former political partner Yair Lapid, gathered in Tel Aviv to protest the emerging deal. Protesters accused Netanyahu of using the coronavirus crisis to shield himself from prosecution and accused Gantz of abandoning his central campaign promises.

“You don’t fight corruption from within. If you’re inside, you’re part of it,” Lapid said.

Netanyahu is desperate to remain in office throughout the trial, using his position to lash out at his enemies and rally public opposition to his prosecutors.

Netanyahu is awaiting trial on charges of accepting bribes, breach of trust and fraud. He has denied any wrongdoing and portrays himself as a victim of a media and judicial witch hunt. Citing the coronavirus crisis, Netanyahu’s hand-picked justice minister has already delayed the trial by two months by shuttering most of the court system.

Israel has identified more than 13,000 cases of the coronavirus, with 172 deaths. While this week the country began relaxing some of its health restrictions, hundreds of thousands are out of work and the economy has come to a standstill.