Chinese President Xi Jinping, right, shakes hands with Japanese Prime Minister Shinzo Abe during a meeting on the sidelines of the Eastern Economic Forum in Vladivostok, Russia, on Sept. 12. (Xie Huanchi/Xinhua/AP)

Since trade tensions between the United States and China began to soar this year, China’s markets have lost 20 percent, its currency has wobbled and exports have decelerated.

But China is gaining something else: friends.

Under pressure from President Trump’s tariff war, China has embarked on a charm offensive on the diplomatic circuit, smoothing over old disputes and courting partners who could help Beijing weather the storm with Washington. Germany, which perennially harangued Beijing over market access restrictions, recently let Chinese investors hold bigger shares in joint ventures in a significant concession. South Korea, the target of withering Chinese boycotts last year over its deployment of a U.S. missile defense system, is seeing Chinese tourism revenue and automobile sales return.

This week, China’s relations with its heavyweight neighbor, Japan, reached its highest level in years. After meeting at a summit in Russia, Chinese President Xi Jinping and Japanese Prime Minister Shinzo Abe announced that China will welcome Abe on his first state visit to Beijing next month after he was frozen out for years over territorial disputes and the Japanese leader’s visits to a controversial shrine for wartime dead. The two men smiled for a photo together, a stark turnaround from four years ago, when they could barely face each other for a memorably grim snap.

During their meeting, Xi told Abe that the two countries should “firmly defend multilateralism, the free trade system and the rules of the World Trade Organisation to push forward an open global economy,” according to China’s state-run Xinhua News Agency. Later, a Japanese foreign ministry official told the Mainichi Shimbun that Japan could play a “mediating” role in the ongoing China-U.S. dispute.

Although Trump has yet to confront Japan as he has China, Mexico, Canada and the European Union, the United States' most critical ally in the Pacific could be in his sights next. The White House has been weighing tariffs of up to 25 percent on autos and car parts, and the president said this month that he plans to tell Japanese leaders “how much they have to pay” — at the risk of straining the relationship.

The U.S. “strategic rivalry” with China has had other secondary effects. The People’s Liberation Army this month participated in Russia’s biggest military drills since the Soviet era, while Xi and Russian President Vladimir Putin sipped vodka at a summit in their latest display of brotherhood. The Global Times, a hawkish Chinese state-run newspaper, quickly pointed a finger at Washington.

“While China-Russia relations are well-developed, they both have a twisted relationship with a distant major power,” it said in an editorial. “Other countries should rethink why they can’t become good friends with Beijing and Moscow.”

To be sure, it’s not clear that China’s whirlwind diplomacy will lead to lasting alliances. Its disputes with neighbors such as South Korea and Japan often involve territory and are steeped in complicated, emotive history. Its frictions with the E.U. over technology sales and market access will probably outlive the temporary camaraderie of resisting Trump. It may now even be wooing India, but few foresee the two Asian rivals settling into an easy, enduring friendship.

But as Trump pulls America into self-imposed isolation, Xi isn’t wasting time. He gave a major speech at the World Economic Forum in Davos last year, the first appearance by a Chinese leader, where he presented himself as a champion of free trade and attributed China’s economic miracle to market reform and opening up. Next week, China’s No. 2, Premier Li Keqiang, is set to repeat the message in an address to the “Summer Davos” crowd in Tianjin.

Li will probably hit notes of “shared prosperity” and “shared values,” but in truth, the message is really about the White House and its trade policies: in other words, a shared threat.

Historically, that’s typically been an effective reason for building alliances.