​A 55-year-old man with no criminal record has been arrested for raping a woman on a Brooklyn rooftop 31 years ago after NYPD detectives used genetic genealogy to link him to the crime, police said Friday.

Williams Stephens was arrested at his Brooklyn home and charged with rape after the case came together through the modern detective tool that helps identify nameless victims or — in this case — suspects by first linking DNA recovered at the scene to their relatives, police said.

The attack happened in Bedford-Stuyvesant on the night of Aug. 2, 1995, when the victim, then 36, was approached from behind by a man who stuck a gun in her back before forcing her to the roof of a Pulaski St. building near Marcus Garvey Blvd.

After raping the victim, the attacker robbed her of her jewelry, police said.

NYPD arrests suspect for 1995 rape with help of genetic genealogy

Theodore Parisienne / New York Daily News

William Stephens is pictured in police custody outside the NYPD Special Victims Unit headquarters in the Bronx on Friday. (Theodore Parisienne / New York Daily News)

Detectives collected DNA evidence 31 years ago, but it did not match anyone on record at the time, officials said.

In 2005, the mystery man’s DNA was pre-indicted, a move made to prevent the statute of limitations from expiring, even though his name was not yet known.

The case was more recently assigned to the Special Victims Unit’s Cold Case Squad, which worked with the NYPD’s Investigative Genetic Genealogy Squad.

The DNA was ultimately linked to one of Stephens’ family members, with police focusing on Stephens and developing enough evidence to establish probable cause to charge him, police said.

Stephens arraignment in Brooklyn Criminal Court was pending Friday.