Exporting a jar file in Android Studio is not as simple as it is in eclipse, but it's not too complicated. You need to export the jar file using the Gradle script.
We don't want to export jar files with classes like R.class and buildconfig.class, so we need to write Gradle scripts to implement custom JAR file content.
First open the Build.gradle file under the module project, write the Task command under the android{} tab, as shown in my Gradle file:
Apply plugin: ' Com.android.application 'Android {compilesdkversion23buildtoolsversion"23.0.2"Defaultconfig {ApplicationID"Com.example.testapp"minsdkversion14targetsdkversion23Versioncode1Versionname"1.0"} buildtypes {release {minifyenabledfalseproguardfiles Getdefaultproguardfile (' Proguard-android.txt '), ' Proguard-rules.pro '}}}task Buildmyjar (Type:jar, dependsOn: [' Build ']) { //exported jar file nameArchivename = ' Testapp.jar '//which directory to package the jar fromFrom (' Build/intermediates/classes/debug ') //The stored directory of the exported jar file (not specified is stored by default under Build/libs)Destinationdir = File (' Build/libs ') //Remove the classes you don't wantExclude (' Com/example/testapp/buildconfig.class ') Exclude (' Com/example/testapp/buildconfig\$*.class ') Exclude (' **/r.class ') Exclude (' **/r\$*.class ') //classes that need to be packagedInclude (' Com/example/testapp/*.class ')}dependencies {compile filetree (dir:' Libs ', include: [' *.jar ']) Testcompile' junit:junit:4.12 'Compile' com.android.support:appcompat-v7:23.1.1 'Compile' com.android.support:design:23.1.1 'Compile' com.google.android.gms:play-services-appindexing:8.1.0 '}
After writing the Gradle script, click "Sync Now"
Open the Gradle panel on the right side of Android studio and select the project name-->other-->buildmyjar (Task name)
When you double-click and run the script, the jar file is generated in the Build/libs directory. Open the jar file with Jd-gui to see that the class file inside does not contain files such as R.class
Android Studio uses Gradle to customize export jar files