1730, Scottish, but the sense is"plain bun," and it is debatable whether it is the same word; in the sense of "small, flat, sweet cake" by 1808 (American English); this use is from Dutch koekje "little cake," diminutive of koek "cake," from Middle Dutch koke (see cake (n.)). "Dutch influence is no doubt responsible also for the parallel use of the word in South African English" [Ayto, "Diner's Dictionary"].
Slang application to persons (especially an attractive woman) attested since 1920. Phrase that's the way the cookie crumbles "that's the way things happen" is attested by 1955.