HONG KONG — Ride-hailing services in China have attracted billions of dollars in investments. They have pulled in money from some of the world’s largest tech companies, like Apple and Alibaba. They have become a crucial road test for one of America’s best-known start-ups, Uber.

On Thursday, they finally became legal.

In a new law announced by several regulators, China’s government gave the thumbs-up for ride-hailing companies like Uber and its Chinese rival, Didi Chuxing, to operate in the country. The law clears up regulatory uncertainty and lays out a new framework under which ride-hailing companies can operate in the country.

Uber and Didi have upended China’s taxi industry by allowing drivers to take passengers in private vehicles with the help of hugely popular apps. Despite investments in technology, marketing and subsidies, both companies had operated in a legal gray zone, with drivers occasionally being detained by the police.

Strict government rules have repeatedly tripped up local and foreign internet companies in China. Yet in the fast-moving Chinese tech industry, an “invest first, get approval later” model has prevailed. For Uber and Didi, it appears that ethos has paid off.