Ride-hailing behemoth Uber Technologies Inc. said Monday it has given up its expensive battle for China’s riders.

Uber will trade in its local investment in the country in exchange for a minority stake in China’s own ride-hailing service, Didi Chuxing Technology Co., which has also been Uber’s most bitter rival in the country.

Based on the terms of the merger, Uber will become the largest shareholder in Didi, owning 17.7%, while Didi will invest $1 million in Uber. China is a notoriously difficult market for foreign investors to crack, including early pioneers such as EBay, which was ousted by Alibaba, and Google, which suffered relentless cyberattacks.

The merger will end the endless fight between the two companies for market share in mainland China, with both companies spending tens of millions of dollars each moth to sway riders to patronize them.

“Uber and Didi Chuxing are investing billions of dollars in China and both companies have yet to turn a profit there. Getting to profitability is the only way to build a sustainable business that can best serve Chinese riders, drivers and cities over the long term,” said Travis Kalanick, Uber CEO and Co-Founder in a prepared statement.

Didi was valued at $28 billion in its latest fundraising round, and Uber’s China business was valued at around $8 billion. With this deal, Didi will have a valuation of around $36 billion.

Besides saving money, Uber also gains from this deal because after investing about $2 billion in the Chinese market, it gets a $7 billion share in a company that has growth potentials.