BEIJING — China said on Thursday that it would temporarily exempt foreign companies from paying tax on their earnings, a bid to keep American businesses from taking their profits out of China following Washington’s overhaul of the United States tax code.

There is, however, a catch: To be eligible, foreign companies must invest those earnings in sectors encouraged by China’s government — including railways, mining, technology and agriculture — according to a statement from the Finance Ministry. The measure is retroactive from Jan. 1 this year, the ministry said.

The move would “promote the growth of foreign investment, improve the quality of foreign investment and encourage overseas investors to continuously expand their investment in China,” the ministry said. It did not elaborate.

Despite its appeal as a manufacturing hub, one where companies from around the world have set up operations to tap into a highly skilled work force and strong infrastructure, China charges high taxes. On top of a standard corporate rate of 25 percent, companies are required to make social security contributions and other payments that push their tax burden higher than it is in many other countries.