728x90 AdSpace

  • Latest News

    [Tips] How to Build Android Library and add it to another project to use it

    Many question about how to build Android library and how to add it to another projects to used it!
    This is your best question, (I think) => Because I had making it the fastest. :D

    This tutorial I'm use Android Studio 2.0 IDE and Build AAR Android Library. 
    Follow my Code Snippet for more knowledge

    How to Build Android Library and add it to another project to use it


    1. Answer the 1st questions "How to build Android Library?"


    First create your new projects,
    File -> New Projects -> use Add No Activity -> Press Finished
    Because The lib we don't write anything to did, so we can choose "Add No Activity" mode

    How to Build Android Library and add it to another project to use it

    After finished, you can see own projects same here.

    How to Build Android Library and add it to another project to use it

    Then, Create my library -> File -> New Module ->  Android Library -> Choose Next.

    How to Build Android Library and add it to another project to use it

    Set MyLib name (The Package name cannot choose because MyLib and LibDemo is same package (same location)

    How to Build Android Library and add it to another project to use it

    Here is the MyLib location .
    How to Build Android Library and add it to another project to use it


    Ok, Now we have MyLib library already, so we can write code here.
    Example: My code is have two class: one class is Customer and another is CustomerProvider

    -- Customer.java

    package huuvi168.com.mylib;

    // Created by huuvi168@gmail.com on 5/20/2016.
    public class Customer {
    private String name;
    private int age;
    private String sex;

    public Customer(String name, int age, String sex) {
    this.name = name;
    this.age = age;
    this.sex = sex;
    }

    @Override public String toString() {
    return "Name = " + name + "\n";
    }
    }



    -- CustomerProvider.java

    package huuvi168.com.mylib;

    import android.content.Intent;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;

    // Created by huuvi168@gmail.com on 5/20/2016.
    public class CustomerProvider {

    public static List<Customer> getInfo()
    {
    List<Customer> lstCustomer = new ArrayList<Customer>();

    Random r = new Random();

    // random number of item but at least 3
    Integer n = r.nextInt(10) + 3;
    String[] arName = new String[]{
    "Tom",
    "Jerry",
    "Alibaba",
    "LearnTechTips",
    "huuvi168" };

    String[] arSex = new String[]{
    "Male",
    "Female",
    "Other" };



    for (int i=0; i < n; i++)
    {
    int nName = r.nextInt(arName.length);
    int nAge = r.nextInt(80) + 18; // at least 18 ages
    int nSex = r.nextInt(arSex.length);
    Customer cus = new Customer(arName[nName], nAge, arSex[nSex]);

    lstCustomer.add(cus);

    }

    return lstCustomer;
    }
    }



    Tips:
    Sometime you cannot build release mode when config on Build Variants .
    If you CANNOT build release mode, don't worry, Choose Projects mode -> Choose Gradle Properties -> double click on assembleRelease, you will see IDE will download some lib files on Internet (so you should connect with Internet if you do it action)

    How to Build Android Library and add it to another project to use it


    Then access to your outputs folder -> Do you see aar folder? that's your release files
    => If you don't see it, please tell me by comment on the below this topic, I can help you easy do it


    2. Answer the 2nd questions "How to add Android Library to Android projects?"


    You can add AAR package by this way
    At the projects you used it, File -> New Module -> Import JAR/.AAR package -> Next -> 

    How to Build Android Library and add it to another project to use it

     Create Lib name and add your lib-release AAR resource to your proejcts!

    How to Build Android Library and add it to another project to use it

    Add lib to Project
    At Projects want to use lib -> Open Module Settings -> Choose Dependences -> Choose Module Dependency (3)

    Tips:

    - If you choose Librara dependency (1) -> This is your remote file -> This same org.apache.commons.commons-lang:3:3:4

    - If you choose File dependency (2) -> Your files will be add to your projects

    How to Build Android Library and add it to another project to use it


    -> Here is your lib

    How to Build Android Library and add it to another project to use it

    Done -> Now go to used it!
    Create your projects with Button and TextView ->  Use Customer and CurtomerProvider as you can!


    package huuvi168.com.demolib2;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    import java.util.List;

    import huuvi168.com.mylib.Customer;
    import huuvi168.com.mylib.CustomerProvider;
    import huuvi168.com.mylib.RssFeedProvider;
    import huuvi168.com.mylib.RssItem;

    public class MainActivity extends AppCompatActivity {

    Button btnClicked;
    TextView txtMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnClicked = (Button)findViewById(R.id.buttonClicked);
    btnClicked.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    List<Customer> list = CustomerProvider.getInfo();
    String text = String.valueOf(list.toString());

    txtMsg = (TextView)findViewById(R.id.textViewMessage);
    txtMsg.setText(text);
    }
    });
    }
    }




    Have any feedback, leave your comment, we can discuss about it!
    If you see this helpful for you, please share and +1 google plus or facebook,

    We're thanks so much!
    Zidane - Learn Tech Tips (Founder)


    • Blogger Comments
    • Facebook Comments

    0 comments:

    Post a Comment

    Item Reviewed: [Tips] How to Build Android Library and add it to another project to use it Rating: 5 Reviewed By: Unknown
    Scroll to Top