Android Studio uses Gradle to customize export jar files

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.