To cope with labor shortages resulting from a declining population, he advocated for more women in the workplace, delaying retirement and using robots to do jobs once filled by humans.

But Japan’s shrinking work force and rapidly aging population put pressure on Mr. Abe and his conservative supporters to accept that the nation’s demographic challenges could not be solved by internal measures alone.

In the absence of immigration, Japan’s population is projected to shrink by about 16 million people — or nearly 13 percent — over the next 25 years, while the proportion of those over the age of 65 is expected to rise from a quarter of the population to more than a third. In caregiving alone, the government estimates that employers will need an additional 377,000 workers by 2025.

The shortage of workers is “an urgent matter,” Mr. Abe said during a parliamentary session late last month. The country, he said, needs “foreign workers as soon as possible.”

Yet the new law, which came under considerable criticism from opposition parties, does not represent an embrace of immigration so much as a deeply ambivalent business calculation. The bill, strongly pushed by industry groups, is vague in some aspects and is designed to limit the kinds of work that foreigners can do, as well as how long they can stay.

“This isn’t about Japan becoming a multicultural society and it’s not about Japan opening its doors to become more globally oriented,” said Gabriele Vogt, a professor of Japanese politics and society at the University of Hamburg who has studied migration. “This is just very plain labor market politics.”