TOKYO — Only a few months ago, Japanese Prime Minister Shinzo Abe appeared to be in danger of losing his job as a series of cronyism scandals dragged his popularity to perilous lows.

To make matters worse, President Donald Trump was ratcheting up trade tensions with Japan, a long-term security ally, exposing the fragility of a personal relationship that Abe had worked hard to establish since Trump was elected in 2016.

Yet Abe is on the cusp of a remarkable political recovery. After stabilizing his position in opinion polls, he has emerged as the clear front-runner in a party leadership vote due next month, according to experts.

Securing another three-year term as head of the dominant center-right party would put Abe on track to become the longest-serving prime minister in Japanese history.

Backroom factional politics within his Liberal Democratic Party (LDP) are “lining up in Abe's favor,” said Kristi Govella, an assistant professor at the School of Pacific and Asian Studies at the University of Hawaii at Mānoa — even as tariff disputes with the U.S. cast a shadow over Japan’s most significant international relationship.

“Given the volatility of recent American foreign policy under the Trump administration, it was inevitable that Abe would experience ups and downs in his relationship with the U.S. president,” she told NBC News. “More than in any personal rapport with Trump, Abe's advantage lies in his strength of expertise and skill on foreign policy issues in comparison to most other Japanese politicians.”

Abe is the second-longest serving leader in the G-7 after German Chancellor Angela Merkel. He wants to maintain pressure on North Korea until the regime takes concrete steps to denuclearize.

Abe has also helped to revive the trade pact known as the Trans-Pacific Partnership, which seemed to be at risk when Trump withdrew the U.S. from the original 12-country deal.

In both security and economics, Govella said, Abe had “a long track record of almost singularly strong foreign policy leadership, and particularly in times of uncertainty in international politics, the Japanese people see this as a valuable asset in a leader.”

Abe has dominated Japanese politics ever since he led the LDP to a landslide victory in the general election in 2012. He had a brief earlier stint as prime minister, from 2006 to 2007, but resigned because of unpopularity and personal health concerns.

Abe’s grip on power began to slip again last year as a result of twin scandals over alleged favoritism.

One involved a huge discount on state-owned land granted to a nationalistic educational group that was closely linked to Abe’s wife, Akie. The first lady had been named as the honorary principal of a proposed new elementary school before the controversy arose.

President Donald Trump and first lady Melania Trump dine with Japanese Prime Minister Shinzo Abe and wife Akie Abe at the Mar-a-Lago resort in Palm Beach, Fla., on April 17. MANDEL NGAN / AFP - Getty Images

The other scandal hinged on suspected preferential treatment for Japan’s first new veterinary school in more than 50 years. It was proposed by an educational institution run by one of the prime minister’s long-time friends.

Abe repeatedly denied wrongdoing and even volunteered to resign if he was found to have been involved in the 85 percent discount on the land sale.

But the trickle of revelations damaged his standing in opinion polls and contributed to a sense of growing arrogance at the heart of government. The approval rating for Abe’s cabinet dropped to 26 percent in a poll by the Mainichi newspaper in July 2017.

Amid rising tensions with North Korea, however, Abe seized the initiative by calling a snap general election late last year.

The fragmented opposition parties were not well prepared and the ruling coalition managed to retain a two-thirds majority in the lower house — providing Abe with a political lifeline.

Abe faced renewed dangers in March when the finance ministry admitted it had tampered with official documents surrounding talks with the elementary school operator, including removing references to Abe and the first lady before the files were presented to lawmakers investigating the sale.

Several finance ministry officials were disciplined, but prosecutors decided against laying formal criminal charges. The finance minister, Taro Aso, kept his job — an important development given that Aso also serves as deputy prime minister and is a powerful factional operator.

Experts say the largest factions have locked in behind Abe, giving him a big advantage in the forthcoming vote of his fellow lawmakers.

“As of now, Prime Minister Abe is the clear front-runner in the upcoming LDP leadership race,” Govella said. “Despite clear signs that the Japanese public continues to be unhappy about the domestic scandals that have plagued Abe in recent months, his support ratings have slowly recovered.”

Yu Uchiyama, from the Department of Advanced Social and International Studies at the University of Tokyo, said while the scandals have damaged Abe, the drip-feed of incremental revelations may have contributed to a sense of public fatigue.

He pointed to a Nikkei newspaper opinion poll showing that the Cabinet’s approval rating increased by 10 points, to 52 percent, from May to June, even though seven in 10 respondents did not think Abe’s explanations were satisfactory.

“It seems to mean that the people don't put much weight on the scandal issue any longer,” Uchiyama added.

While approval ratings are a factor in the leadership machinations, the general public does not get a direct say on the LDP leadership. The party’s lawmakers have an equal share with its grass-roots membership in the first vote. If a runoff is needed, lawmakers dominate the second vote.

The only viable challenger is an Abe nemesis, Shigeru Ishiba, who has vowed to restore trust in politics and to speak up for those who have not felt the benefits of “Abenomics,” the prime minister's economic policy.

Ishiba has previously called for substantive changes to Japan’s “pacifist” constitution.

Shigeru Ishiba speaks during a press conference in Tokyo on Aug. 10. JIJI PRESS / AFP - Getty Images

The constitution, which has not been changed since it was adopted after World War II, prohibits Japan from retaining a full-fledged military. Abe also wants to change this section of the constitution, but has proposed a minimalist amendment to codify the existence of the Japanese Self-Defense Forces.

Ishiba, a former defense minister, was popular among grass-roots LDP members when he ran in 2012. He is still a strong candidate but is unlikely to secure enough votes from lawmakers and even from the broader LDP membership, according to Ryo Sahashi, a professor of international politics at Kanagawa University.

Abe’s advantage was boosted last month when Fumio Kishida, a former foreign minister, pulled out of the race and urged his factional allies to support the incumbent.

The internal affairs minister, Seiko Noda, would become the first female prime minister if elected as LDP leader, but is expected to struggle to gain the 20 nominations needed to run.

Shinjiro Koizumi visits the Yasukuni shrine on the 73rd anniversary of Japan's surrender in World War II in Tokyo on Aug. 15. KAZUHIRO NOGI / AFP - Getty Images

Rising star Shinjiro Koizumi, the son of a former prime minister, is considered not yet ready for the top job.

One potential wildcard is Trump’s threat to impose tariffs on automotive imports — a move that would be more damaging to the Japanese economy than the steel and aluminum duties announced this year.

“If the U.S. were to announce automobile tariffs before the LDP election, it could complicate the leadership campaign, since it would test Abe’s claims to have forged a productive friendship with Trump,” said Tobias Harris, vice president of global consulting firm Teneo Intelligence.

If he’s still in power in November 2019, Abe would become Japan's longest-serving prime minister, totaling nearly eight years in office including his short earlier tenure.

With a lower house election able to be held as late as 2021, he appears to be in a good position to reach the milestone, although a poor showing by his party in the upper house election next year could affect his chances.