ST. LOUIS (AP) _ David S. Lewis, chairman of beleaguered General Dynamics Corp., said today he will retire from the defense contractor by the end of the year.

Lewis's announcement came one day after Navy Secretary John F. Lehman canceled two contracts and froze all new contracts at two General Dynamics divisions for breaches of ethics. The company was fined $676,000 for giving ''trinkets'' to retired Adm. Hyman G. Rickover.

Lewis said his decision to retire was not a sudden one.

''I have been planning to retire since 1983,'' Lewis said. He said was happy that he had stayed on with the company through a period of ''extreme outside pressure.''

Lewis said he would be succeeded by Stanley C. Pace, who is currently vice chairman of TRW Corp. Lewis said Pace will join General Dynamics as vice chairman on June 1, the day after he retires at TRW, and would step up when Lewis retires, ''no later than Jan. 1.''

Lewis said Pace would begin work as soon as he joined the company on the defense contractor's overhead accounting and contract charging practices. He said he was confident that under Pace's leadership, the company would continue to serve the country.