Mr. Ai’s latest run-in with Shanghai officials appears to exemplify that love-hate relationship.

As he tells it, he was approached more than two years ago in Beijing by the mayor of one of Shanghai’s districts — a government unit not unlike an American city ward — and beseeched to build a studio on an abandoned plot of farmland. Initially suspicious — “I told my assistant we’re not going to deal with government anymore,” he said; “there’s no honesty there” — he relented when the mayor flew to Beijing for a personal appeal.

Mr. Ai said he worked closely with the district to rehabilitate an abandoned warehouse on the site, spending about $1 million to create a vast working space fronting on a lake with a sawtoothed roof and sides laced with a concrete grid. Other artists began building their own adjacent studios.

Then last July, as work was wrapping up, there came a city order to tear down the warehouse.

“They said only we received the notice,” he said. “The other artists did not. We said, ‘Why?’ and they said, ‘Well, you should know, because of Ai Weiwei’s activities.’ ”

Which activities offended someone is, of course, not known. But Mr. Ai said he suspected he rankled officials in 2008, when his blogging on the case of Yang Jia, who murdered six Shanghai policemen after being arrested and beaten for riding an unlicensed bicycle, created a national sensation. Mr. Yang was later executed. He said that officials also might resent his documentary this year on Feng Zhenghu, a lawyer and activist who spent more than three months in Tokyo’s Narita Airport after Shanghai officials denied him entry to the country.

Whatever the reason, Mr. Ai said, the district official who first recruited Mr. Ai returned to Beijing this week, apologizing profusely and promising to compensate him for the cost of the renovation if he would leave.

“I said, ‘Why? It took so much effort and energy, and you didn’t give us a clear reason,’ ” he said. “But they cannot really answer these questions. So I realize it’s inevitable. They’ll destroy the building.”

At the planned goodbye party for the studio, in lieu of chips and dip, Mr. Ai planned to serve river crabs — a sly reference to the Mandarin word hexie, which means both river crab and harmonious. Among critics of China’s censorship regime, hexie has become a buzzword for opposition to the government’s call to create a harmonious society, free from dissent.