BEIJING—China’s trade surplus with the U.S. hit a record last year, as robust American demand for Chinese goods undercut the Trump administration’s tariff offensive aimed at narrowing the countries’ lopsided trade gap.

China recorded $323.32 billion in surplus with the U.S. in 2018, representing a 17% jump from the figure in the previous year, according to Chinese government trade data released Monday.

Abetting the record imbalance were a healthy American economy and a weakening Chinese one, some economists and analysts said, which in turn fed U.S. demand for imports and damped demand in China. The Trump administration’s phased deadlines for tariffs, along with threats of more for this year, also sent Chinese exporters racing to fill orders, and a weakening Chinese currency kept the prices of those goods competitive.

The latest data shows “how the tariffs affected the trading behavior of exporters who accelerated their shipments,” said Liu Yaxin, an analyst at China Merchants Securities . Both exports and imports are likely to slow at least for the first half of the year, Ms. Liu said, as frontloading effects vanish and domestic economic growth slows.

China’s exports to the U.S. rose 11.3% in 2018, while imports from the U.S. inched up 0.7%, data from China’s General Administration of Customs showed.