Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[Tips] Android Library: How to Create and Import a Library in Your Project

Monday, 23 May 2016
|
Read: Completed in minutes

[Tips] Android Library: How to Create and Import a Library in Your Project

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

Android Library: How to Create and Import a Library in Your Project - Webzone Tech Tips - Zidane


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

Android Library: How to Create and Import a Library in Your Project - Webzone Tech Tips - Zidane

After finished, you can see own projects same here.

Android Library: How to Create and Import a Library in Your Project - Webzone Tech Tips - Zidane

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

Android Library: How to Create and Import a Library in Your Project - Webzone Tech Tips - Zidane

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


Android Library: How to Create and Import a Library in Your Project - Webzone Tech Tips - Zidane

Here is the MyLib location .
Android Library: How to Create and Import a Library in Your Project - Webzone Tech Tips - Zidane


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)





Android Library: How to Create and Import a Library in Your Project - Webzone Tech Tips - Zidane


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 -> 

Android Library: How to Create and Import a Library in Your Project - Webzone Tech Tips - Zidane

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

Android Library: How to Create and Import a Library in Your Project - Webzone Tech Tips - Zidane

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

Android Library: How to Create and Import a Library in Your Project - Webzone Tech Tips - Zidane


-> Here is your lib

Android Library: How to Create and Import a Library in Your Project - Webzone Tech Tips - Zidane

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);
            }
        });
    }
}


Thank you for reading this post. I hope you found it helpful and easy to follow. If you have any feedback or questions about Android Library: How to Create and Import a Library in Your Project , please share them in the comments below. I would love to hear from you and discuss this topic further
✋✋✋✋  Webzone Tech Tips, all things Tech Tips for web development  - I am Zidane, See you next time soon ✋✋✋✋

🙇🏼🙇🏼 Your Feedback Is Valuable and Helpful to Us - Webzone, all things Tech Tips web development Zidane
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com