Android Studio builds a custom jar package (by Starry Wuge)

Source: Internet
Author: User

Reprint please mark original address: http://blog.csdn.net/lsyz0021/article/details/52162414


It is well known that Android Studio will automatically generate a jar after the app that the library relies on or build, with the path Build/intermediates/bundles/debug or release/ Classes.jar, the resulting jar is available, but not something we can control, so we need to solve this problem in other ways.



First we create a new project and then create a new module as the library, and then let the app rely on it, such as the project structure catalog. Create a Testprint class in mylibrary to allow external apps to invoke.


/** *  LCW on 2016/7/15. * Blog: http://blog.csdn.net/lsyz0021/*/public class Testprint {public void toast (Context conte XT, String msg) {Toast.maketext (context, MSG, toast.length_short). Show (); public void Sout () {System.out.println ("test information, already called the Sout () method");}}

Called in the app's mainactivity

public class Mainactivity extends appcompatactivity {@Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); new Testprint (). Toast (This, "hahaha");}}
This is how I call the dependent mylibrary, and we're going tomylibraryThe Build.gradle configuration in the. jar file can be generated.

In the MyLibrary build.gradle , configure the following:

dependencies {    Compile filetree (dir: ' Libs ', include: [' *.jar '])    testcompile ' junit:junit:4.12 '    compile ' com.android.support:appcompat-v7:23.4.0 '}def sdk_basename = "TESTSDK";d ef sdk_version = "_v1.0";d EF Sdkdestinationpath = "Build";d ef zipfile = file (' Build/intermediates/bundles/release/classes.jar ') Task Deletebuild ( Type:delete) {    Delete Sdkdestinationpath + sdk_basename + sdk_version + ". Jar"}task Makejar (Type:jar) {from    zip Tree (ZipFile) from    filetree (dir: ' Src/main ', includes: [' assets/** '])    baseName = Sdk_basename + sdk_version    destinationdir = File (Sdkdestinationpath)}makejar.dependson (Deletebuild, build)

where sdk_basename = "TESTSDK";sdk_version = "_v1.0";IsdefinitionThe name of the generated jar is Testsdk_v1.0.jar. Follow the action




Then double-click on "Makejar" to generate the jar file, resulting in the following:




You can copy the Testsdk.jar into a new module in the Libs directory and then you can call the new Testprint (). Toast (This, "hahaha") method.

Something this approach also supports packaging the assets directory's resource files into the jar package's



Android Studio builds custom jar packages by starry Wuge

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.