Robert MacPhail, 26, was asked if he knew the 16-year-old accused of abducting, raping and murdering six-year-old Alesha MacPhail.
He answered: “Yes, I sold him cannabis.”
Alesha was killed after she went to stay with her father for a holiday break at her grandparents’ house on the Isle of Bute in July 2018.
Mr MacPhail said he and his new partner had moved in with his parents following the break-up of his relationship with Alesha’s mother.
Mr MacPhail told the jury that the youth accused of her murder would message him through Facebook and they would meet.
The handover of drugs typically took place at the bus shelter across the road from Alesha’s grandparents’ house in Rothesay.
Referring to the night she was killed, he said he had put her to bed watching a Peppa Pig DVD. When she had got up and went into the lounge, he put her back to bed and told her he would see her in the morning.
He said that was the last time he saw her.
Alesha’s grandfather, 49-year-old Calum MacPhail, told the court he woke at about 6am on 2 July last year to find Alesha’s bedroom door wide open.
He said the bed was empty and he couldn’t see her in the room. He said they searched underneath all the beds, in all the wardrobes, but there was no sign of Alesha.
He went out in his car to look for Alesha and came across a police cordon and saw paramedics going up and down a set of steps.
He was directed towards the local police station where he gathered in a room with other family members to be told by an officer: “We have found her but she has passed.”
Giving evidence, Alesha’s grandmother Angela King, 47, called her granddaughter a “beautiful happy girl.”
She told the court that on the night Alesha went missing the key was in the lock of the door. She said: “I’d forgotten to take the key back out of the door.”
The 16-year-old boy on trial denies the rape and murder of Alesha MacPhail. He has lodged a special defence blaming a 19-year-old woman.
The trial, before Lord Matthews, continues.