By Steven Levy, guest contributor
FORTUNE -- Plans for Google.cn were well under way by May 7, 2005, when an unexpected e-mail arrived in the in-box of Eric Schmidt. It was from a computer scientist and executive at Microsoft named Kai-Fu Lee. "I have heard that Google is starting an effort in China," he wrote. "I thought I'd let you know that if Google has great ambitions for China, I would be interested in having a discussion with you." Kai-Fu Lee was a celebrated computer scientist -- he'd worked for Apple previously -- who had become a phenomenon in China. Lee, who had grown up in Taiwan and gotten his Ph.D. at Carnegie Mellon, was the embodiment of the "sea turtle" -- an Asian-born engineer whose success in America was a prelude to a homecoming that allowed him to contribute to China's drive to the pinnacle of the world economy. Lee was perhaps the most famous of all sea turtles. Hundreds of thousands of people went to his website and wrote to him for advice, as if he were a combination of Warren Buffett, Bill Gates, and Abigail Van Buren. Google immediately recognized how Kai-Fu Lee could accelerate its plans to make a mark in China. "I all but insist that we pull out all the stops and pursue him like wolves," senior vice president Jonathan Rosenberg wrote to his fellow executives. So Lee flew to meet with Google founders Sergey Brin and Larry Page in Mountain View, Calif., on May 27, 2005. The session was a lovefest. Lee was startled when Sergey, who had arrived by skateboard, asked him, "Do you mind if I stretch?" and then did body motions on the floor while asking questions. As they left, Lee overheard one say to the other, "People like Kai-Fu don't grow on trees." When Lee returned to Seattle, he was greeted by a huge box of Google swag, including a basketball, a chair, and a coin-operated gumball machine with a Google logo.Lee resigned from Microsoft (MSFT) on July 18 and officially accepted Google's (GOOG) offer the next day. It was worth over $13 million, including a $2.5 million signing bonus. On his Chinese-language website, Lee said that Google had given him a "shock" by its fresh approach to technology and postulated that in China, his new employer's youth, freedom, transparency, and honesty would produce a miracle. "I have the right to make my choice," he wrote. "I choose Google. I choose China." Microsoft rushed to the courthouse and charged Lee with violating a noncompete agreement that was part of his employment contract. But on Sept. 13, Judge Steven Gonzalez ruled that while Lee was prohibited from sharing proprietary information with or helping Google in competitive areas such as search and speech technologies, he could participate in planning and recruiting for Google's effort in China. Ultimately, the two companies would settle, and the restrictions on Lee's activities would be lifted in 2006.
Google.cn went live on Jan. 27, 2006. A few months later Google China moved into its new offices. It occupied several floors of a gleaming building that appeared as if it were made out of giant white Lego blocks and glass. It was one of several similar structures in the Tsinghua Science Park in the Hardan District of north Beijing, China's Silicon Valley. Occupying several floors of the high-rise, Google's headquarters was outfitted with the usual frills: physio balls, foosball tables, a fully equipped gym, a massage room, and (in a nod to local recreational activities) a karaoke room, and a Dance Dance Revolution videogame, as well as a huge cafeteria with free meals. Finding applicants wasn't a challenge. As soon as the news broke that Lee would be heading Google China, résumés began arriving by the hundreds. Lee went on a recruiting trip that had aspects of a rock-and-roll tour, with students actually bootlegging counterfeit tickets. Google's head of engineering, Alan Eustace, accompanied Lee on one trip and couldn't get over how people mobbed him. It was like some weird Asian form of Beatlemania. "He'd give a talk at a university, and it would be like a basketball game -- 2,000 people in the audience," he says. "He would be surrounded by literally hundreds of students. People would get close to him, just to touch him."Signs of a distressed relationship almost from the start
Google had hoped that its decision to create a search engine in the .cn domain -- one that followed government rules of censorship -- would lead to a level playing field. But even as Google rolled out its .cn web address, there were indications that its compromise would not satisfy the Chinese government. Unexplained outages still occurred. (Meanwhile, Google's competitor Baidu seemed to hum along unscathed.) And not long after Google got its operating license, in December 2005, the Chinese declared that the license was no longer valid, charging that it wasn't clear whether Google's activities made it an Internet service or a news portal. (Foreigners could not operate the latter.) Google then began a year-and-a-half-long negotiation to restore the license.
Google finally got its license in June 2007. The dispute had been resolved in secret. And to a large degree the level of service stabilized. Another boost that year was that Google was granted a valuable concession: simply typing "g.cn" would take Chinese users to the Google.cn site. But by then many Chinese had written off Google as an unwelcome outsider with less reliable service.
Because Google had a firm policy against storing personal data inside China -- to avoid the problems of having the government demand that Google turn over the data -- it did not offer a number of its key services for local Chinese users. No Gmail. No Blogger. No Picasa. Other services had to be drastically altered. YouTube was blocked entirely.
As Chinese employees came onboard, it took a while for some of them to adjust to the Google style. For instance, many were uncomfortable with Google's worldwide policy that employees initiate and pursue independent projects during 20% of their work time. Engineers had to be told by a visiting Mountain View executive that they did not need permission to do a 20% project. Yet the No. 1 concern of Google's engineers was their access, or lack of it, to Google's production code. Google was a collaborative company that wanted its engineers around the world to innovate on its existing products and create exciting new ones. It empowered them to do so by giving them access to its production code base. Without such access, engineers were limited in what they could do.
But unlike Google's employees in other locations, the China workers did not have such access. The restrictions limited what the engineers could do -- and sent a message that they were second-class employees. "At one time I had the feeling that if we didn't give them access, there would be a riot," says Google China manager Ben Luk. Suspicion lingered that the engineering executives behind the policy -- some of whom had deep concerns about the company's China policy -- had intentionally engineered rigid restrictions as a form of corporate civil disobedience against their employer's cooperation with censors.
Government relations fiascos
Google's success in China depended in part on having a government relations point person who could navigate the tricky shoals of preserving Google's values without offending Chinese officials. Google's first GR head was a former vice president of Sina, who was experienced in the ways of Chinese bureaucracies. But perhaps because she did not speak English, she failed to appreciate issues from the Google perspective. She complained to at least one colleague that Google wasn't flexible enough with the government and did not work hard enough to please it.
Her tenure came to an end when Google discovered that she had taken it upon herself to give iPods to Chinese officials. She had charged them to Google, and another executive had approved the charge. In Chinese business culture such gifts are routine, but the act unambiguously violated Google policy, not least because it was an explicit violation of the U.S. Foreign Corrupt Practices Act. Google fired both her and the executive who had approved the expense. When she was called to Kai-Fu Lee's office for dismissal, she was dumbfounded. In Mountain View, this breach was another sign of how difficult the China situation was. Eustace, the Mountain View executive overseeing China, later recalled the incident as "the worst moment in our company" and blamed himself for not making sure that Google's representative to the Chinese government knew how dimly the company would view such an act.
After the employee's departure, Google chose a three-person government relations team, all female, led by Julie Zhu, an energetic woman in her thirties. She was hired straight from a government ministry, instead of the commercial sector with its backscratching culture. Zhu was better able to communicate with Mountain View. But she had her hands full fending off Chinese government directives. A demand would come from a government ministry to take down 10 items; Google would typically take down seven and hope that the compromise resolved the matter. Sometimes after a few days or weeks Google would quietly restore links it had censored. Every five months Google's policy-review committee in China would meet to make sure it was filtering the minimum it could possibly get away with. It was, as Google China engineering director Jun Liu put it, "trench warfare," but he believed that Google's continuing problems were proof that it was indeed moving the democracy needle in China.
For all the progress, some Google executives were beginning to think that its great China compromise wasn't working. A turning point came in 2008, the year China hosted the Olympics. In the run-up to its turn in the international spotlight, China apparently decided to increase its restrictions. It demanded that in addition to censoring the .cn results, Google purge objectionable links from the Chinese-language version of Google.com. That, of course, was unacceptable to Google -- it would mean that it was acting as an agent of repression for Chinese-speaking people all over the world, including in the U.S. Other search engines, including Microsoft's, agreed to such demands. But Google stalled, hoping that after the Olympics the Chinese would back off. They did not. The demands for censorship became broader and more frequent.
The last straw
In June a new problem arose involving Google Suggest, a search feature that instantly offered fully developed search queries when users typed just a few characters or words into the search box. This innovation, ultimately offered globally, was developed first in China after Google's search team realized that, because of difficulty in typing, Chinese users generally entered shorter queries into the search box. But Chinese officials discovered that in an alarming (to them) number of instances, the suggestions offered by Google were related to sexual matters. They informed Google of their unhappiness by summoning Kai-Fu Lee and other Google China executives to a local hotel, where representatives of three ministries were waiting with a laptop and a projector. Once everyone was seated, the show began. The Chinese went to Google.cn and typed in a vulgar term for breasts. Google Suggest offered links that displayed raw nudity, and more. The official typed in the word meaning "son," and one of the Google Suggest terms was "love affair between son and mother." The links to this term yielded explicit pornography. The woman serving tea in the conference room almost fainted at the spectacle. The Google people tried to explain that apparently someone had spammed keywords to artificially boost the popularity of sex sites in Google Suggest. The officials were not impressed. "You've been warned twice before, and this is the third time. So we're going to punish you." By that time Lee had already decided to leave Google.
Just before Christmas 2009, Google's information security manager Heather Adkins learned that she would fall short on her annual "don't get hacked" internal goal. Google's monitoring system had detected a break-in of Google's computer system, and some of the company's most precious intellectual property had been stolen. The hack was geographically tied to China -- and both the sophistication of the attack and the nature of its targets pointed to the government itself as an instigator of or a party to the attack.
"The more we learned as we looked into it, the more we realized this wasn't just a classic hack, but folks who were after something. This was hacking with a purpose," says chief legal officer David Drummond. As Google's security specialists kept looking, they found even more horrendous consequences. The hackers had dug into the Gmail accounts of Chinese dissidents and human rights activists. All their contacts, their plans, their most private information had fallen into the hands of intruders. It was hard to imagine that the Chinese government was not poring over them.
Within days Google set up the most elaborate war room in its history, as an entire Google facility was filled with a mix of security engineers working on the forensics of the incursions and policy lawyers trying to figure out what to do next. Meanwhile Google's executives began a series of meetings to determine the next step. The question they discussed was the same one that had been argued five years earlier: What's the right thing to do in China? Google had originally hoped that the Chinese would appreciate its compromise and tacitly tolerate Google's quiet pressure to relax the filtering. Instead it was the opposite. And now Google was under attack.Brin took the incident personally. Insiders observed he was much less perturbed by the theft of Google's intellectual property than the fact that his company had unwittingly been a tool used to identify and silence critics of a repressive government. Brin wanted the incident to be the catalyst to the action that he and others had been urging since 2008: Google should stop censoring. He was passionate in his insistence. He had support from some executives who had soured on China over the past 10 months -- but not all. Notably, Schmidt was not convinced. But Brin was adamant: Google was under attack by the forces of evil, and if his fellow executives did not see things his way, they were supporting evil. (I'd heard from a knowledgeable but not firsthand source that Brin threatened to quit if Google did not change its policy. Brin, through a spokesperson, didn't recall saying that, and said that the company was so much in his blood and DNA, it was unlikely that he expressed that intention. He did acknowledge that during the many hours of debate, he presented his case with utmost passion.)
Cut ties and the aftermath
Brin's point of view eventually prevailed. On Jan. 10, 2010, Google's top executives reached a decision. Page had joined Brin in deciding to end Google's experiment in censorship; the outvoted Schmidt accepted the decision. (Observers would later say that the setback had long-lasting implications for Schmidt's relationship with the founders, but from the very start of his time at Google, Schmidt had understood that his word on crucial company matters was not final.) In any case, the company decided it would no longer carry out censorship for the Chinese government.
The news spread through Mountain View like an earthquake. Meetings all over the campus came to a dead stop as people looked at their laptops and read how Google was no longer doing the dirty work of the Chinese dictatorship. "I think a whole generation of Googlers will remember exactly where they were when that blog item appeared," says one product manager, Rick Klau.
For Google's employees in China, the day was also unforgettable. Not one of them had been alerted ahead of time. Drummond posted his announcement at 6 a.m. Beijing time, and many of the Googlers in Beijing and Shanghai first heard about it when frantic colleagues awakened them. Employees filed into the office in a state of shock. That afternoon Google told all employees to leave and gave them tickets to see Avatar. The next day everyone gathered in the café for a teleconference with Brin and other executives, who tried to explain Google's actions. It was a tough sell. Julie Zhu, Google's new government relations person, delivered an emotional objection to her employers, overseas generals who seemed to have abandoned the soldiers in the theater of war. You should not have given up, she argued. You should have kept fighting.
Kai-Fu Lee now says that if you look at China's behavior over a long horizon -- 20 or 30 years -- it's clear that the trend is toward more openness. The incidents that led to Google's retreat were "a perturbation" in this movement, mainly because Chinese leaders had reached their limits. "The next generation will come up in less than two years," he says. "They're younger, more progressive, many American-trained, and many have worked in businesses and run banks -- they're going to be more open."
Excerpted from In the Plex by Wired senior writer Steven Levy. Copyright © 2011 by Steven Levy. Reprinted by permission of Simon & Schuster Inc.
Fortune Podcast: Missteps in China - Assistant managing editor Leigh Gallagher speaks with author Steven Levy about the chapter of his new book, In the Plex, that details how Google got it wrong in China.