God knows that humans will partake in adharma. So why did he create us?
Let's see this the way Tantra talks about it. The God is nothing but Ultimate Reality, also termed as chit or Parasamvit. It is non-relational consciousness. It is the changeless principle of all changes. In it there is no distinction between subject and object, Of I and This. It is supreme self surviving Itself.
So question is, why it needs to change, why can't he stay at his real state forever and why to create all the creatures ?
The answer is simple.
It is svabhava the very nature of Ultimate Reality to manifest. Creativity is of the very essence of Divinity. If Ultimate Reality did not manifest, it would not be self or consciousness, but not-Self, something like a jar.
As Acharya Abhinavagupta puts it,
अस्थास्यदेकरूपेण वपुषा चेन्महेश्वर: ||
महेश्वरत्वं संवित्वम तदत्यक्षद् घटादिवत् ||
"If the Highest Reality did not manifest in infinite variety, but reminded cooped up within its solid, singleness, it would neither be the Highest Power nor Consciousness, but something like a jar"
Strictly speaking no one can answer this question. It is God's sweet will.
One reason is given in Hindu scripture.
In the beginning, this universe was verily the self in the form of a person. He pondered and beheld nothing but himself. He firsr said, ‘I am he’. Therefore he got the name ‘I’. Hence even now when any one is accosted, he first says, ‘It is I’, and then tells the other name that he has. Because he was the first (among the aspirants to the status of Prajapati), and (before) this whole group consumed all evils, therefore he is called Purusa. He who knows thus verily consumes him who wishes to be (Prajapati) in advance of him.
Brhadaranyaka Upanishad I.4.1
He was frightened. Therefore one is (still) frightened when one is alone. He reflected, ‘Since there is nothing besides myself, whhat am I afraid of?’ From that alone his fear departed, for what should he be afraid of? Fear comes from only a second entity.
Brhadaranyaka Upanishad I.4.2
Verily he did not feel happy. Therefore one (still) does not feel happy when alone. He desired a mate. He became of the size of a man and wife embracing each other. He divided this very body into two. From thart husband and wife came into being. ‘Therefore this body (of a man) is one-half of himself, like half of a two-celled seed’, so said Yajnavalkya. Hence this void is verily filled by the wife. He united with her. From that men were born.
Brhadaranyaka Upanishad I.4.3
Prajapati is the first manifestation of the Divine and becomes the whole of the universe. The Upanishad suggests that the universe and all its living beings were manifested because Prajapati or God felt lonely.