The leaders of Japan and China will hold their first official meeting next week, almost two years since each took office, after striking a deal Friday to build “strategic, mutually beneficial relations.”

The plan for direct dialogue between Japanese Prime Minister Shinzo Abe and Chinese President Xi Jinping is seen as an opening to possibly easing tensions from disputes that include rival territorial claims over a group of islands.

It also reflected a willingness on both sides to seize the moment during a regional summit that will include President Obama.

Japanese officials had been furiously seeking talks on the sidelines of the Asia-Pacific Economic Cooperation, or APEC, conference in Beijing next week, but Xi had set conditions that were difficult for Abe to accept.

On Friday, the two sides found common ground.

A statement issued by China and Japan said the leaders had agreed to “squarely look to history and overcome political difficulties which affect bilateral relations, with a spirit to look to the future.”

The leaders’ meeting will take place Monday and Tuesday, but the timing of the talks is not yet clear.

Relations between China and Japan have been at a nadir, mainly because of a territorial dispute involving a group of islands in the East China Sea known as the Senkaku Islands in Japan and the Diaoyu Islands in China.

Relations have been further strained by visits by Abe and members of his government to the Yasukuni Shrine, where 14 people convicted of Class A war crimes are memorialized along with 2 million other Japanese who died in World War II.

China and South Korea view Yasukuni as a symbol of Japan’s military aggression during the first half of the 20th century. Japanese politicians say the shrine is simply their equivalent of Arlington National Cemetery.

Beijing had insisted — as a condition for talks with Abe — that Tokyo acknowledge that the islands’ status is in dispute and agree to halt high-level visits to Yasukuni.

Under their deal, the leaders agreed to “recognize that the two countries hold a different view over the tense situation in the East China Sea” and to build a crisis management mechanism — a “hotline” — to avoid accidental clashes over the islands, Japanese and Chinese officials said.

For weeks, experts in both countries have been speculating about the probability and depth of any exchange between the two leaders at APEC. Would it be a brief handshake, a short chat or a full-blown meeting?

Japan in recent months has played the part of the suitor, lobbying for a substantive meeting to patch up a fraught relationship that has harmed both sides economically. Abe’s aides had been saying that even a handshake with Xi would be a “good handshake.”

As recently as Monday, China’s state-run Xinhua News Agency reported that Xi would “undoubtedly receive the Japanese leader with etiquette and hospitality,” but it played down the prospect of a more substantive encounter.

The backroom dealings behind Friday’s agreement remain unknown. But analysts in Japan said that Abe would most likely agree privately not to visit Yasukuni — he has not attended events at the shrine this year — even if he cannot say so publicly for fear of angering his nationalistic supporters.

Japan also had wanted to avoid any reference to disagreements over the islands, apparently fearing that doing so would be tantamount to acknowledging China’s territorial claims.

Nationalist sentiment has been raging in both countries, and neither Xi nor Abe wants to appear soft to his domestic audience.

“Everyone on the Chinese side, from the defense chief to foreign minister to even Xi, is wary of the nationalistic backlash that would come from embracing Abe,” said Michael Green, who was a top Asia adviser in the administration of President George W. Bush.

Chinese leaders have proved their willingness to snub foreign counterparts before.

In 2012, during an Asia-Europe meeting in Laos, then-Chinese Premier Wen Jiabao passed by Japanese Prime Minister Yoshihiko Noda without greeting him. That came several months after another flare-up over the Senkaku/Diaoyutai islands.

Last year, Japan’s Yomiuri newspaper reported that Xi and Abe shook hands on the sidelines of the APEC summit in Bali. But Chinese Foreign Ministry spokeswoman Hua Chunying refused to confirm it and called “relevant reports meaningless.”

Xi drew notice across Asia in September when he opened a conference commemorating the anniversary of the birth of Confucius by citing an oft-quoted line from the philosopher: “What a pleasure to have friends from afar.”

Wan reported from Beijing. Liu Liu and Gu Jinglu in Beijing contributed to this report.