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 first 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, what 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 that 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.
Every action (present) has a reaction (future) and had a cause (past). There is no action (work) without a cause. Every cause has its cause(s) material or eternal. God (as the Supreme Person) is the cause of all causes, however, He Himself has no cause. He is the "sarv kaaran-kaaranam", He is the cause of His "Brahman" and "Parmatama" revelation (Supreme Personality) too.
Nevertheless, the different religious thoughts differently explain "how?" the material existence comes into being but answering the "why?" is difficult..... always.... in this case too!!
The Bhagvatam mahapuran, although has the answer in third to fifth volumes (scand). We all have unique eternal identities as unconditioned souls (as the salokya-mukta jivas, who resided in the same domain of Parmatama, but we have never ever participated/entered the Bhagvat-dham, higher "vaikuntha", the realm with His eternal pastimes, due to lack of higher eternal knowledge, desire, love and servitude). We have our eternal roots in the God's Kingdom or lower "vaikuntha", which is without the eternal pastimes (eternal domain without Sakhayam, Vatsalya and Madhurya Rasa). Somehow, when there near the "vaikuntha", beside/on shores of the "virja-river" (a.k.a causal ocean, which divides the spritual worlds from material worlds) we had acquired a hidden desire to be the absolute enjoyers like God or Bhagwan. Knowing this desire He, the All-indwelling, manifested His external energy, i.e. material potency into its eight components (the three for subtle and the five for the gross existence), the illusionary energy for the fulfillment of jiva-atamas' (or "tatashatha", marginal potencies') desire to become enjoyer or "bhokta brahman", somewhat similar to the Almighty, "Brahman-tatva", which controls both "jiva-tatva" and "maya-tatva". These three are eternal elements, neither created nor destroyed. The permanent eternal identity of the soul is as a servant of God. However, we are in illusion to temporarily enjoy in ignorance under the influence of His illusionary potency, i.e. maya, which is also eternal servant of God. In B.Gita too He has hinted about it in verse #7.27 as:
इच्छाद्वेषसमुत्थेन द्वन्द्वमोहेन भारत|सर्वभूतानि सम्मोहं सर्गे यान्ति परन्तप||
ichchhā-dveṣha-samutthena dvandva-mohena bhārata sarva-bhūtāni sammohaṁ sarge yānti parantapa.
By word "sarge" meaning at the onset of material universe creation, Krishna is indicating that at the very start of incarnate existence, all creatures are inveigled into the loops of various dualities such as good and better, higher bliss and highest bliss, honour and dishonour, controller and controlled, true-ego and false-ego, which are generated by the desire for what is agreeable and the aversion to what is disagreeable. The word "sarge" meaning at the very beginning of material creation/transformation. Or more precisely, transmigration of jiva-atma in the transformed maya. Thus, our atma or eternal soul, blissful eternal identity without any causal body, became an embodied being, with having a causal, a subtle and a gross body.
The mooting point here is, why, what for He did this (material transformation) pastime, for His own enjoyment?? or for our (the jivas') desire to enjoy like Him!!! Yes. He is the supreme enjoyer, He is always the Supreme Bliss, this truth is eternal in all the worlds, if transcendental or mundane. It can be further revealed by B.Gita verse# 27 of chapter# 7. He did this "Purishavatar-Lila" (pastime of eternal personality expansion in material domain) to fulfill our "ichchha" (desire) to enjoy like Him.
The All-indwelling knew human will indulge in (material) adharma temporarily, but as soon as they will involve in God realisation they can again be acquiring their permanent eternity with even higher conciousness after suffering the temporary miseries in cycles of material dharma-adharma.