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