I think I remember being taught in a college biology class that cannabinoids may have evolved as an anti-microbial / anti-insecticide defence. Sort of like capsaicin, or aromatics like lavender.
So this doesn't surprise me, but...it was college. My memory might be flawed.