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
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
After finished, you can see own projects same here.
Then, Create my library -> File -> New Module -> Android Library -> Choose Next.
Set MyLib name The Package name cannot choose because MyLib and LibDemo is same package (same location)
Explore My Other Channel for More Cool and Valuable Insights
👉 Youtube Learn Tech Tips👉 Tiktok
👉 Facebook:Here is the MyLib location .
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)
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 ->
Create Lib name and add your lib-release AAR resource to your proejcts!
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
-> Here is your lib
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 ✋✋✋✋