After leading the statewide fight against a bill shutting federal immigration agents out of California jails, Alameda County Sheriff Greg Ahern is turning into an unlikely champion of freeing jailed immigrants suspected of being in the country illegally.

Ahern said his jails in Alameda County have begun releasing about 80 percent of the inmates they otherwise would have held and handed over to U.S. Immigration and Customs Enforcement before the Trust Act took effect this year.

"As the sheriff, my job is to follow the laws as they are written," said Ahern, who issued a directive being copied by sheriffs around the state.

By refusing to hand over all but the most serious criminal convicts to ICE, Ahern said he expects to lose $400,000 each year in federal reimbursements for keeping immigrant detainees. He hopes his new policy encourages East Bay immigrants to report crimes without fearing deportation.

"We hope that we're easing those fears and building the public trust with all of the members of our community," Ahern said.

He also has an alternative business plan to fill the sprawling but undercapacity Santa Rita Jail in Dublin now that the county is losing revenue attached to certain illegal immigrants held on the federal government's request.

Ahern is bartering empty cells to other Northern California sheriffs whose jails are too crowded.

A contract signed last year to ship Monterey County inmates to Dublin already "more than exceeds the loss of revenue on immigration," and more contracts are in the works, Ahern said.

Ahern insists he is just doing his duty in adapting to a new state law, but Bay Area activists who sparred with him for years describe his policy as a significant and welcome turnaround. They also say his policy is even more liberal than the new state law he recently opposed.

A registered Republican who has presided over the California State Sheriffs' Association since last spring, Ahern lobbied against passage of the Trust Act, believing the bill by state Assemblyman Tom Ammiano, D-San Francisco, put sheriffs in conflict with federal law. But even before the Legislature passed and Gov. Jerry Brown signed the bill in October, Ahern said he was already crafting a way to implement it. Now sheriffs in Sonoma County and elsewhere are adopting the language of the four-page general order Ahern issued Jan. 1.

"He's definitely an example. As the leader of the sheriffs association, people take cues from him, especially on immigration," said Cinthya Muñoz, co-chairwoman of Alameda County United in Defense of Immigrant Rights.

Her coalition formed in 2010, when Ahern and sheriffs throughout the state began implementing the federal Secure Communities Program, which forwards the fingerprints of everyone arrested to federal authorities, including many picked up for minor traffic violations.

"At first he was very resistant to feeling like his office's policies were having a negative impact on the community," Muñoz said.

Ahern balked when the Alameda County Board of Supervisors passed a 2012 resolution urging him to follow the lead of other liberal counties, such as San Francisco and Santa Clara, in backing off from the program now credited with deporting nearly 2,300 people from Alameda County in four years. Privately, however, Ahern, whose elected seat has never been contested, began meeting with activists and affected immigrants.

"As we kept meeting, there was definitely a lot more openness," Muñoz said. "He's seeing that immigrants are really a key sector of the community in Alameda County. He's definitely responding to their organized force and power."

Ahern still has unanswered questions about the conflicts between dueling federal and state policies. He also believes federal agents, "instead of working in our facility," will "go out and take into custody people they believe are undocumented."

The agency confirmed this week that agents will keep pursuing released inmates deemed priorities.

"We can use other law enforcement strategies to do that," said ICE spokeswoman Virginia Kice. "If we get information about their whereabouts we may go and seek to locate them."

She added that "we're trying to focus on cases of convicted criminals and public safety threats."

Alameda County Supervisor Richard Valle, who was at odds with Ahern in authoring the 2012 resolution, said "our paths are beginning to align." The sheriff's order "offers a great deal of clarification," Valle said.

"It's a straightforward message we're not going to hold people just because they are here and they're undocumented," Valle said. "It's a great step forward."