BEIJING — Six years ago, angry demonstrators filled the streets in dozens of Chinese cities to protest Japan’s claim to islands in the East China Sea, surrounding Tokyo’s embassy, overturning Japanese cars and in some cases even attacking sushi restaurants.

Two years later, President Xi Jinping met with Prime Minister Shinzo Abe on the sidelines of a regional conference in Beijing, and the body language said it all: Mr. Xi could barely muster a smile during an awkward handshake for the cameras.

As Mr. Abe begins the first state visit to China by a Japanese leader in eight years on Thursday, no one is expecting the Asian powers to become instant partners, or even to manage a major reconciliation. But in the age of Trump, both are looking for a little more normality.

Battered by plummeting relations with Washington, and particularly by President Trump’s trade war, Mr. Xi is looking to a friendlier Japan as a hedge. And though Mr. Abe has met more often with Mr. Trump than any other foreign leader has, he is well aware of the president’s fickle treatment of American allies and also wants to cover his bets.

“Both sides need each other,” said Yu Tiejun, a Japan expert at Peking University. “They need to improve relations as a response to the uncertainty brought about by Trump in Asia. This is a good beginning — better than a deterioration.”

A development site in Myanmar, where Japan is the biggest foreign donor. Tokyo and Beijing are competing to be the partner of choice for developing countries in Asia.CreditNyein Chan Naing/European Pressphoto Agency

Neither side is expecting miracles. The countries are strategic rivals, each trying to promote itself as the partner of choice for less powerful Asian nations. And their bloody history, dating back to World War II and before, remains a major obstacle.

Analysts say the optics of Mr. Abe’s three-day visit will be more important than the concrete outcomes, which are likely to be modest. About 500 Japanese businesspeople are expected to accompany Mr. Abe to Beijing, a signal that both sides want the trading relationship — which took a deep dive from 2012 to 2014, after the rupture over the disputed islands — to keep growing.

China is Japan’s largest trading partner. Even as Mr. Trump’s trade conflict with China rages, the Japanese auto giant Toyota plans to increase production in China by 20 percent, expanding plants in two major Chinese cities, according to news reports in Japan.

To show that its ties with China can improve, Japan has agreed to sign an accord in Beijing calling for them to work together on infrastructure projects in developing countries.

But even that agreement will reflect the two powers’ rivalry. Japan has stipulated that it does not want to be involved in so-called Belt and Road projects — China’s ambitious bid to draw countries into its orbit through infrastructure investment — unless international standards of transparency and fiscal sustainability are applied, said a senior Japanese official, who spoke on condition of anonymity according to diplomatic custom.

Some countries involved in Belt and Road projects, like Malaysia and Sri Lanka, have accused China of saddling them with excessive debt. By insisting on standards developed by the Organization for Economic Cooperation and Development, of which Japan is a member but China is not, Japan can signal to developing countries that it is a fairer partner, the Japanese official said.

China and Japan are also strategic competitors in the region. This Japanese submarine, Kuroshio, took part in war games in the South China Sea in September.CreditJapan Maritime Self-Defense Force

Japan believes it has made that distinction clear in Myanmar, for example, where it is the biggest foreign donor. Japan has built functioning infrastructure there, while China was forced to cancel dam construction over environmental concerns and is bogged down in negotiating the finances of other projects.

Strategically, Japan is also seeking to position itself in the region as a counterforce to China. Last month, a Japanese submarine participated in war games in the South China Sea for the first time, then visited Vietnam, an indication that Japan was prepared to stand with other countries against China’s territorial claims in the crucial waterway.

As China rapidly modernizes its military, Japan remains wary of its strategic intent. Six years after the dispute over the contested East China Sea islands — known as the Senkaku in Japan, and the Diaoyu in China — the Chinese Navy has kept up the pressure, sending the same number of coast guard vessels into the waters off the islands as it did in 2017, the Japanese official said.

With such concerns in mind, Japan is enthusiastically participating in an informal, implicitly anti-China alliance with the United States, India and Australia, which has become known as “the quad.” The Trump administration has promoted the grouping, emphasizing that all four countries are democracies and changing the name of the Pacific Command in Hawaii to the Indo-Pacific Command, to signal India’s strategic role and the range of forces that could unite against China.

Indeed, soon after Mr. Abe returns from China he will host Prime Minister Narendra Modi of India, its partner in the “quad.” But the Chinese do not seem overly concerned.

“There is a possibility they could cooperate against China,” Hu Lingyuan, head of the Center for Japanese Studies at Fudan University in Shanghai, said of the Indo-Pacific group. “But it could not go far.”

President Trump has met with Mr. Abe more often than with any other foreign leader, but Mr. Abe is well aware of his fickle treatment of American allies.CreditDoug Mills/The New York Times

Ezra F. Vogel, a China and Japan specialist at Harvard, said that Mr. Abe, a “pragmatic nationalist,” had a better chance of improving ties with China than did Japan’s opposition Democratic Party, which during a brief stint in power presided over the worsening of relations in 2012, despite its stated policy of getting closer to China.

Unlike the United States, Mr. Vogel said, Japan never harbored illusions that China would become like the West, an outlook that has led to more realistic expectations about the extent of a possible rapprochement. Animosity between the two Asian powers is also being reduced by “soft power” factors, like the more than eight million Chinese tourists who visited Japan this year.

Shiro Armstrong, director of the Australia-Japan Research Center at the Australian National University, said that for Japan, “the balancing act is not to give away too much” as its ties with China develop.

“The important thing is the U.S. security relationship is vital for Japan,” he said. “That’s nonnegotiable, and that’s what Japan needs to protect while it improves its relationship with China and other countries.”

Still, some Chinese analysts have suggested that given the volatility of Mr. Trump, who has warmed to Japan’s nemesis North Korea and threatened in the past to pull American troops from the region, Mr. Xi should be able to sow doubts about Japan’s American allies during Mr. Abe’s visit.

“The Trump administration’s economic nationalism and trade protectionism are a leading driver to improved ties between Beijing and Tokyo,” said Zhu Feng, an Asia expert at Nanjing University. “Trump clearly increases Japan’s skepticism toward America’s role and credibility in the region.”

But China should not expect too much from that, at least in the short term, Mr. Vogel said.

“Abe is thinking of a long time frame, of building a framework where China and Japan can work together and live together as neighbors without conflict,” Mr. Vogel said.