Problem:
You want to add a new library module to your app
Solution:
Using the library plugin, add a library module as a dependency.
Discuss:
You cannot add many features to your app by using a Java library, usually using a jar package. Section 1.5 describes how to use the dependencies block. For example, in order to parse JSON using Googles's Gson library, you can add dependencies to the module's configuration file:
dependencies { compile ' com.google.code.gson:gson:2.6.2 '}
Android Libraries is out of the Java library because they include not only the Android API, but also the resources needed. When the project was built, Gradle packaged Android libraries as an AAR (Android archieve) file, similar to the jar file, but included Android dependencies.
From Gradle's point of view, Android libraries is a sub-project of root. This means they are just like Android apps, only under a subdirectory. The module's name is therefore added to the Settings.gradle file:
Include ': App ', ': Icndb '
In this case, the Android library module, called Icndb, represents the Internet Chuck Norris Database, which is used to provide jokes in JSON format. The API page is as follows:
As an example of an Android library, this site serves as a restful service, the returned JSON data will be parsed, and then the returned jokes will be added to Welcomeactivity's TextView.
In Android Studio, you can create a new library module from the "New module" by selecting "Android Library":
After you create the library name, you can add any activity to the type you want. Complete the wizard, create the library directory and add it to the Settings.gradle file under the root directory.
Each library has its own gradle configuration file. You can specify the minimum and target SDK versions, customize build types,flavors, and modify dependencies as needed. The final difference is that the Gradle configuration uses a different plug-in:
Apply plugin: ' Com.android.library ' android { compilesdkversion buildtoolsversion "23.0.3" packagingoptions { exclude ' meta-inf/notice.txt ' exclude ' meta-inf/license.txt ' Exclude ' LICENSE.txt ' } defaultconfig { minsdkversion targetsdkversion versioncode 1 versionname "1.0" } Buildtypes { Release { minifyenabled false proguardfiles getdefaultproguardfile (' Proguard-android.txt '), }
dependencies { compile ' com.google.code.gson:gson:2.6.2 ' compile ' com.squareup.retrofit2:retrofit:2.0.1 ' compile ' com.squareup.retrofit2:converter-gson:2.0.1 '
}
The configuration file adds the Retrofit2 project as a dependency, using Gsonlibrary to convert the JSON information.
Note the use of packagingotions blocks. This allows you to exclude files of the same name that are used in multiple projects.
If you use these libraries,icndb libraries, it becomes easy to use:
public class Jokefinder {private TextView jokeview; Private Retrofit Retrofit; Private asynctask<string, Void, string> task; Public interface Icndb {@GET ("/jokes/random") call<icndbjoke> Getjoke (@Query ("FirstName") String firs Tname, @Query ("LastName") String LastName, @Query ("LimitTo") String LimitTo); } public Jokefinder () {retrofit = new Retrofit.builder (). BASEURL ("http://api.icndb.com") . Addconverterfactory (Gsonconverterfactory.create ()). build (); } public void Getjoke (TextView TextView, string First, String last) {This.textview = TextView; New Joketask (). Execute (first, last); } private class Joketask extends Asynctask<string, Void, string> {@Override protected String doin Background (String ... params) {icndb icndb = retrofit.create (Icndb.class); call<icndbjoke> Icndbjoke = Icndb.getjoke (Params[0], params[1], "[nerdy]"); String joke = ""; try {joke = Icndbjoke.execute (). Body (). Getjoke (); } catch (IOException e) {e.printstacktrace (); } return joke; } @Override protected void OnPostExecute (String result) {Jokeview.settext (result); } }}
The Jokefinder class accesses the ICNDB network service using the provided initials and end letters, using an asynchronous task that allows the operation to run on a non-UI thread. The Getjoke method contains a TextView parameter so that joketask the completed use is updated.
The Incdbjoke task is just a simple pojo matching JSON response. The format of the response is as follows:
JSON is very small, so for the Icndbjoke class is also very simple, as follows:
public class Icndbjoke { private String type; private Joke value; Public String Getjoke () {return Value.getjoke ();} Public String GetType () {return type;} public void SetType (String type) {this.type = type;} Public Joke GetValue () {return value;} public void SetValue (Joke value) {this.value = value;} private static class Joke { private int ID; Private String joke; Private string[] categories; public int getId () {return ID;} public void setId (int ID) {this.id = ID;} Public String Getjoke () {return joke;} public void Setjoke (String joke) {this.joke = joke;} Public string[] GetCategories () {return categories;} public void Setcategories (string[] categories) { this.categories = categories; }} }
The application uses the library through the Jokefinder class. Use Project dependencies in the module's configuration file, as follows:
Use the project method to compile the app, using the module that contains the subdirectory as the parameter. The result is that Gradle knows to build the Icndb module before building the app, which makes it possible to use classes at compile time.
Welcomactivity calls the Getjoke method inside the Jokefinder, providing a textview reference and the first and last name. As follows:
The results of the operation are as follows:
The build process generates the debug and release versions in the Icndb/build/outputs/arr directory:
AAR files can be published to the warehouse for later use.
Summary below:
1. The Android Library project is a Java project that requires Android-dependent (Android APU class or Resource)
2, Gradle for multi-project use subdirectory, each sub-project added to the root directory of the Settings.gradle file inside
3. In Android Studio, use the "Android Library" in the "New Module" wizard to create an Android library project
4. Library project using Com.android.library plugin
5. The app configuration file uses Project (": library") to rely on the library.
Android Development: "Gradle Recipes for Android" Reading notes (translated) 4.5--using Android Libraries