If you set up the code as a plain Java module in Gradle, then it's really easy to have Gradle give you a jar file with the contents. That jar file will have only your code, not the other Apache libraries it depends on. I'd recommend distributing it this way; it's a little weird to bundle dependencies inside your library, and it's more normal for users of those libraries to have to include those dependencies on their own (because otherwise there are collisions of those projects are already linking copies of the library, perhaps of different versions). What's more, you avoid potential licensing problems around redistributing other people's code if you were to publish your library.
Take the code that also needs to be compiled to a jar, and move it to a separate plain Java module in Android Studio:
Set up the library, Java package name, and class names in the wizard. (If you don't want it to create a class for you, you can just delete it once the module is created)
In your Android code, set up a dependency on the new module so it can use the code in your new library:
Choose your module from the list in the dialog that comes up:
Hopefully your project should be building normally now. After you do a build, a jar file for your Java library will be placed in the build/libs directory in your module's directory. If you want to build the jar file by hand, you can run its jar build file task from the Gradle window:
Thank you very much! Your answer was really helpful, most in the last screenshot that shows the Gradle tasks. I didn't know that. I could follow your the steps, but I still missing the org.apache.http.Header. How can I import it into my lib?
Go through the same Project Structure > dependencies flow, but choose your library module instead of your project, and add the dependency in the same way. In my answer I say that it puts the jar file in the build/libs dir in its module directory.
I got: java.lang.NoClassDefFoundError: com.loopj.android.http.RequestParams. That's my external library I'm compiling with mine.
It work with java library only, and jar task in android library not exist