WASHINGTON — President Obama feigned opposition to gay marriage for most of his political career, compromising his true beliefs out of concern it could hurt him with voters, Obama’s longtime political adviser disclosed in a new book.

David Axelrod, who served as a top White House adviser after helping Obama get elected, said Obama begrudgingly followed his advice that he would face strong opposition from African American religious leaders and others if he let it be known he supported gay marriage. He said Obama “modified his position” to say he supported civil unions – but not same-sex marriage.

“Having prided himself on forthrightness, though, Obama never felt comfortable with his compromise and, no doubt, compromised position,” Axelrod wrote in the memoir “Believer: My Forty Years in Politics,” released Tuesday.

Axelrod’s disclosure affirmed what was widely suspected for years: that Obama’s May 2012 announcement that he supported gay marriage came long after the president had personally come to that conclusion. The year earlier, Obama and the White House had started saying his position was “evolving,” leading many to believe he was holding off on a public embrace of gay marriage.

“If Obama’s views were ‘evolving’ publicly, they were fully evolved behind closed doors,” Axelrod wrote.

White House spokesman Josh Earnest said he wouldn’t dispute Axelrod’s account, noting that it was informed by Axelrod’s “front-row seat to history.”