The 58-year-old actor, who for two decades portrayed psychiatrist Dr. Frasier Crane on the hit NBC sitcoms "Cheers" and "Frasier," most recently starred in the Starz cable channel drama series "Boss" as a fictional Chicago Mayor Tom Kane, in the mold of Richard J. Daley.
Grammer's role in the "Transformers" science-fiction action film sequel was first reported by Deadline Hollywood. It identified his role as "a counterintelligence guy" named Harold Attinger.
Peter Cullen, who voiced Optimus Prime in the earlier films, will reprise his role as the leader of the Autobots.
Production starts in July.
The film, written by Ehren Kruger, "takes place exactly four years after the war in Chicago," director Michael Bay says on his website. "The story makes a very natural transition, and reason as to why we have a whole new cast.
"This 'Transformers' will feel very different than the last three," Bay writes. "We are embarking on a new trilogy."