OAKLAND, Calif. — In one of the most far-ranging attempts to halt the spread of the coronavirus, Apple and Google said they were building software into smartphones that would tell people if they were recently in contact with someone who was infected with it.

The technology giants said they were teaming up to release the tool within several months, building it into the operating systems of the billions of iPhones and Android devices around the world. That would enable the smartphones to constantly log other devices they come near, enabling what is known as “contact tracing” of the disease. People would opt in to use the tool and voluntarily report if they became infected.

The unlikely partnership between Google and Apple, fierce rivals who rarely pass up an opportunity to criticize each other, underscores the seriousness of the health crisis and the power of the two companies whose software runs almost every smartphone in the world. Apple and Google said their joint effort came together in just the last two weeks.

Their work could prove to be significant in slowing the spread of the coronavirus. Public-health authorities have said that improved tracking of infected people and their contacts could slow the pandemic, especially at the start of an outbreak, and such measures have been effective in places like South Korea that also conducted mass virus testing.