The groups don't need to be the same size.
You can put your data in a list and feed that to kruskal.test. For example:
# Fake data
set.seed(2)
x = list(male = rgamma(40, 2),
female = rgamma(60, 1.5))
kruskal.test(x)
Or, you can provide kruskal.test a data vector and a group vector:
x.data = c(x$male, x$female)
x.group = as.factor(rep(c("male", "female"), sapply(x, length)))
kruskal.test(x.data, x.group)
If you're relatively new to R, the help can sometimes be cryptic, but it's a good idea to get in the habit of checking the help to see if there's a way to cover your use case. In this case, the Details section of the help (?kruskal.test) describes the above options.