Generating JAR file with Android Studio and Gradle

2019-07-20 16:05发布

问题:

I'm working on a project that supposed to become SDK library that other developers will integrate in their Android project. I want to generate and export a Jar library of one of my Android modules from Android studio project. This sdk module contains javadoc comments and my wish is to generate user friendly jar file that contains this javadoc.

How can I export this JAR file using Android Studio?

I found few types of answers:

  • Answers that describes usage of not working gradle scripts
  • Use the classes.jar file - using this answer I couldn't find a way to show the name of the input parameters of each of the methods (when using autocomplete in the project that adds the jar as dependency I'm getting weird methodName(String s1,String s3, String s3) and not methodName(String userName, String password,String country).

Please show me the way...

By the way, I'm using Android Studio 0.5.8.

回答1:

if your lib contains android-resources or dependencies to other moduls that contain android-resources you should publish aar files instead of jar files.

i donot know how to do these in android-studio but from commandline you can do this:

if you have created an android-lib module you can generate the lib using gradle assembleDebug or gradle assembleRelease. it is build in the build/lib folder.

to learn what gradle can do for you you can use gradle --gui

[update]

i dont think that eclipse can consume aar files (but i may be wrong)

if you add

task jar(type: Jar, dependsOn: 'compileReleaseJava') {
   // with gradle2.2+/androidStudio1+/BuildTools1+ add sourcecode to jar
   // from android.sourceSets.main.java.srcDirs 

   // with older gradle/androidStudio/BuildTools add compile-result to jar
   // from android.sourceSets.main.java 

   // for gradle 2.2 and buildTools com.android.tools.build:gradle:1+ / androidStudio1.0
   from "$buildDir/intermediates/classes/release/" 
}

to your gradle file you will get a jar task, that builds a jar for you. so calling gradle jar will build the jar for you



回答2:

You should create a new module project in android studio and change the apply plugin: 'android' to apply plugin: 'android-library' in build.gradle file.