DreamWorks has closed a deal with Castle Rock that gives a greenlight to “Envy,” a comedy that will shoot in July with Barry Levinson directing and Ben Stiller and Jack Black starring.

The duo play lifelong best friends and neighbors whose relationship takes a nasty turn when one of them (Black) becomes filthy rich by selling an invention. It drives his pal (Stiller) crazy with jealousy.

The project had been developed by Castle Rock at Warner Bros. for several years, after “Seinfeld” co-creator Larry David set it there as a pitch he hatched with screenwriter Steve Adams. WB let it go because it could not agree on a budget that Levinson would need to make the film.

Several studios stepped up when Castle Rock’s Martin Shafer and UTA reps for Levinson, Stiller and Black shopped the package, which has a budget around $40 million, including a salary for Stiller said to be in the range of $10 million against healthy gross participation.

David will exec produce, and the comedy will be produced by Levinson and his Baltimore/Spring Creek partner Paula Weinstein.

Surprise home

The emergence of DreamWorks as the financier of the film is more than a mild surprise, given the highly publicized acrimony Levinson directed at the studio over its handling of “An Everlasting Piece,” the Irish comedy about hairpiece salesman. The film barely registered a blip in the marketplace, and Levinson was outspoken about his contention that the film was marketed poorly.

One of the producers, Jerome O’Conner, even waged a lawsuit against DreamWorks that just this week was dismissed in court.

DreamWorks exec Michael De Luca has a strong relationship with Levinson dating back to “Wag the Dog” at New Line.

Fences were mended and the studio is apparently looking beyond the dispute and concentrating on the commercial appeal of the dark comedy and the pairing of Stiller and Black. DreamWorks exec Adam Goodman will steer the project.

Stiller takes the role after completing the Danny DeVito-directed “Duplex” for Miramax opposite Drew Barrymore.

Stiller is developing “Starsky and Hutch” at Warner Bros. with “Road Trip” helmer Todd Phillips and possibly Owen Wilson as Stiller’s co-star in the comedy.

He’s also expected to reprise his “Meet the Parents” role in the Jay Roach-directed Universal-based sequel “Meet the Fockers.”

Black was last seen in “Orange County” and “Shallow Hal.”