When you are dying perhaps your definitions change.
I hold the controversial (and often dismissed) opinion that Google is dying. I assert they are dying in part because they built a company around search advertising before they knew how to build a company, their initial success gave them the confidence and agency to create structural impediments to their ability to do anything but search advertising. And the search advertising market is not only saturated it is getting more competitive as the easy gains to be made there have already been milked for what they are worth.
I support that opinion on the meager evidence of how they more and more cravenly monetize properties that they previously kept free of such things, their repeated failed attempts at building any other business outside of search that brings anything close to the margins they need for their business model to work, and the lack of 'barriers' to other well funded and durable companies from taking away everything that is special from them. (Bing, Uber, Cruise, Facebook, Apple, Etc.) They get less and less per click and they spend more and more to buy more traffic for their search boxes as documented in their financial statements for the last decade.
They have repeatedly come under attack from outside agencies which see their use of, and exploitation of data about people as either anti-competitive or a violation of basic human rights. Those efforts have resulted in fines and legislation that has been cutting off other ways to monetize the traffic of their place in search.
From that misguided perspective, military contracts make total sense. What they have is a tremendous infrastructure that can be wielded by a relatively small number of engineers to do amazing things. For now, that capability is unique. By turning it from a cost center into a profit center (or even a less cost center) it can help them make the profits they need to keep things rolling. When I was working there I got to see what spending 4 billion dollars a year on infrastructure buys you. It rivals what nation states can field, it rivals what the US government can field. Renting that out to a price insensitive buyer for a unique asset? Sounds like saving yourself from dying to me.