SAN FRANCISCO—Apple Inc. and Google will build software together that would alert people if they were in contact with someone infected with the coronavirus, an unprecedented collaboration between two Silicon Valley giants and rivals.

The project, which is certain to raise privacy concerns, offers the most concrete technological solution to date for governmental authorities searching for ways to at least partially lift the lockdown orders that have swept the nation. The companies are by far the world’s biggest smartphone software...