
The genes were transferred to the beetles, researchers say, from sea squirts (tunicates) , with whom they have, may we say, not much in common, by microorganisms (symbiont bacterial strains). From ScienceDaily:
An international team of researchers led by scientists of Johannes Gutenberg University Mainz (JGU) and the Leibniz Institute for Natural Product Research and Infection Biology in Jena has discovered that bacteria associated to Lagria villosa beetles can produce an antifungal substance very similar to one found in tunicates living in the marine environment. The researchers revealed that this commonality is likely explained by the transfer of genes between unrelated microorganisms.

…
The discovery of a new bioactive substance produced by the dominant strain B. gladioli Lv-StB was particularly interesting for the research team. After putting together 28,000 beetle eggs for chemical analyses, Dr. Kirstin Scherlach and Professor Christian Hertweck in Jena identified an especially interesting symbiont-produced compound which can block fungal growth. The scientists named this new compound lagriamide, after the symbionts’ beetle host, Lagria. “Strikingly, lagriamide closely resembles substances that had been found before in the marine environment and that are presumably produced by microbial symbionts of tunicates,” said Scherlach. How to explain this remarkable similarity in such different habitats and organisms?
By analyzing the pool of genes in the microbial community of the L. villosa beetles, Jason Kwan and his team at the University of Wisconsin in Madison, USA, identified the genes responsible for the production of lagriamide in the genome of the dominant beetle symbiont. They also found an exciting clue: these genes are located in a so called genomic island, i.e., a region that was likely inserted in the chromosome of the symbiont from an external source.
Jumping genes are a known phenomenon in bacteria and other organisms. However, this is one of the few examples in which there is direct evidence that such transfer of genetic material underlies the defensive potential of a symbiont. It is especially exciting that symbiosis and the acquisition of foreign genetic material can be a versatile means of innovation for animal defense across habitats. Paper. (open access) – Laura V. Flórez, Kirstin Scherlach, Ian J. Miller, Andre Rodrigues, Jason C. Kwan, Christian Hertweck, Martin Kaltenpoth. An antifungal polyketide associated with horizontally acquired genes supports symbiont-mediated defense in Lagria villosa beetles. Nature Communications, 2018; 9 (1) DOI: 10.1038/s41467-018-04955-6 More.
As noted re an earlier story about bats, the finding also suggests that Darwinian claims about natural selection producing evolutionary changes should be tested against the possibility that the change was in fact caused by horizontal gene transfer.
See also: Bats “steal” genes from ebola-related virus which serve an as-yet-unknown function in the bat
Researchers: Cross-species transfer has been “an important driver of evolution”
and
Horizontal gene transfer: Sorry, Darwin, it’s not your evolution any more