I made some calculations and I got a correct answer, although I lack the intuition and I have no idea why it works. Say we want to build a uniformly charged sphere with radius R and total charge Q, bringing charges from infinity and building it. In order to construct the shell, we need to do work against the force acting on every infinitesimal area element. The electric field outside the shell is $E=\frac{kQ}{r^2}$ and inside is zero. Therefore, we get:
$$p=\frac{F}{A}=\frac{1}{2}\epsilon_0 (E_1-E_2)^2 = \frac{1}{2}\epsilon_0\left(\frac{k^2Q^2}{r^4}\right)$$
and integrating we get:
$$W=\int_R^\infty\int_0^{4\pi r^2}\frac{1}{2}\epsilon_0\left(\frac{k^2Q^2}{r^4}\right)\, dadr\, . $$
The result is the work done to construct only the shell. Now, what about the "inside" of the sphere? I did the same, but with $E=\frac{kQr}{R^2}$ and the radius ranging from zero to R and summed the two works (building the shell + building the inside) and I got the right result.
Now, my question is, why? For calculating the work done in order to build the inside of the sphere, I used the electric field inside a uniformly charged sphere, but what we actually do is build infinitely many shells, so I don't see how that works. I will be very glad for some clarification and intuition.
*I know there are easier ways to calculate it.