Beyond the financial crisis, China’s very prosperity threatened Dongguan’s future. The average worker’s income rose fourfold over the past decade. Fewer young people wanted to work on dull and stressful assembly lines, preferring service jobs — like waiting tables and delivering e-commerce packages — that let them interact with people or move around. Some factories moved to lower-cost countries or shut down for good.

Dongguan’s companies and government had to do something. They committed to modernizing.

Before Made in China 2025 became policy, Dongguan kicked off a “replacing humans with machines initiative” and funded it with about $30 million a year. It later channeled more money into other automation initiatives. Companies that could prove they had a worthy research project or were willing to invest in industrial robots, software or advanced machinery could win subsidies and tax breaks. The government picked up 10 percent to 20 percent of the tab. Smartphone, furniture, machinery and even cake companies won support, official documents show.

Mentech, the telecom equipment supplier, once had hundreds of workers winding, packaging and testing magnetic wires that were thinner than hair, all by hand. Even today, the company is desperate for workers. On the side of one factory building it lists the on-the-job benefits it offers: monthly wages with overtime of up to about $1,100, air-conditioned dormitories, free Wi-Fi and even a birthday present.

“Love your employees,” reads a banner, “and they will love you back 100 times.”

But labor costs and a lack of hands were holding it back. During the Lunar New Year holiday, when most of China shuts down and goes home, some 500 Mentech executives, engineers and administrative staff had to work three-hour shifts after their normal workday to keep the factory running, said Zhang Xiaodong, a research and development manager.

Mentech asked Mr. Zhang and others to figure out how to automate the factory. They spent two years working late into the night. Machines needed tweaking. Components needed to be redesigned so that machines could make them. Several projects failed.

“Not every problem has a solution,” Mr. Zhang said. “We know that smart manufacturing is the future. But getting there isn’t easy.”