People don't like other people, it's a fact. Especially if they're foreign or different. Most especially if they've got a different religion than you. Hoo boy, watch out for religious folks with a grudge!
So you have the Israelites, who are mostly shepherds. They herd, and being herders, they're not farmers or hunters. We know that herders hated farmers, who hated them right back. Different lifestyle, it makes you weak or stupid or indolent or you smell funny.
Able was a shepherd; his gift to god was a sacrifice of meat, and god liked it. Cain was a farmer; he gave god some fruitw, and god said, "What, are you calling me a fruit? I don't like yourfruity gift. Go away and let me go ack to liking Able."
So Cain threw a hissy fit and killed Able, then went awa to found a foreign country where they do fruity things, like farm.
See what this story really is? It's a canonization of an excuse to go to war.
1) God hates farmers, just like we do! Yay! Look, it says here that God doesn't like their gifts, he likes OUR gifts.
2) Foreigners are evil! See, Cain went off and founded some other country! All foreigners, bad! Hiss! Spit!
3) They started it! They hit us first!
And while I'm at it, I don't like people talking about the story of Onan like it's a condemnation of masturbation or of birth control. See, Onan's brother died, and he did so before he could get his wife properly knocked up with a son. No son means no heir. By the laws presented in Leviticus, it was therefore Onan's duty to get his brother's wife pregnant (If they got married, it would be known as a Levirate marriage) so his (dead) brother would have an heir.
Onan didn't want to do this. With his brother dead and heirless he would be the sole inheritant of his father's property. Or maye he just didn't like his brother all that much. For whatever reason, he didn't want to get his sister-in-law pregnant. That was his crime, failing to fulfill the Levirate law that said he had to do so. Nothing else.
And it's not like it was ambiguous in the Bible. It says straight out that he didn't want to get his brother's wife pregnant, so he didn't, and God killed him for it.