Thursday, September 12, 2019

Creating libraries for Android applications

I use 2 languages Vietnamese and English to introduce Creating libraries for Android applications
Mình sử dụng 2 ngôn ngữ tiếng việt và tiếng anh để giới thiệu về Creating libraries for Android applications
Nội dung 
1. Android library projects and Java libraries
2. Custom Android library modules
2.1. Using custom library modules
2.2. Creating custom Android library modules
3. Prerequisite
4. Exercise: Create an Android library module
4.1. Create library module
4.2. Remove generated dependency from build.gradle of the library project
4.3. Create the model class
4.4. Create instances
4.5. Define dependency to the library project
4.6. Use library project to update detailed fragments
4.7. Validate implementation
5. Exercise: Deploy a library project
6. Android library resources
7. vogella training and consulting support

1. Dự án thư viện Android và thư viện Java
2. Các mô-đun thư viện Android tùy chỉnh
2.1. Sử dụng các mô-đun thư viện tùy chỉnh
2.2. Tạo mô-đun thư viện Android tùy chỉnh
3. Điều kiện tiên quyết
4. Bài tập: Tạo mô-đun thư viện Android
4.1. Tạo mô-đun thư viện
4.2. Xóa phụ thuộc được tạo ra từ build.gradle của dự án thư viện
4.3. Tạo lớp mô hình
4.4. Tạo trường hợp
4.5. Xác định sự phụ thuộc vào dự án thư viện
4.6. Sử dụng dự án thư viện để cập nhật các đoạn chi tiết
4.7. Xác thực thực hiện
5. Bài tập: Triển khai một dự án thư viện
6. Tài nguyên thư viện Android
7. hỗ trợ đào tạo và tư vấn vogella

1. Android library projects and Java libraries
1. Dự án thư viện Android và thư viện Java
Android project can use code contained in JAR files (Java libraries)

    If you want to use libraries, these must only use API available in Android. For example, the java.awt and javax.swing packages are not available on Android.

In addition to JAR files, the Android uses a binary distribution format called Android ARchive(AAR). The .aar bundle is the binary distribution of an Android Library Project.

An AAR is similar to a JAR file, but it can contain resources as well as compiled byte-code. A AAR file can be included in the build process of an Android application similar to a JAR file.

It is possible to create libraries modules which can be used as dependencies in Android projects. These modules allow you to store source code and Android resources which can be shared between several other Android projects.

To use a Java library (JAR file) inside your Android project, you can simple copy the JAR file into the folder called libs in your application. *.jar files in this folder are included into the compile classpath via the default build.gradle file

Dự án Android có thể sử dụng mã có trong các tệp JAR (thư viện Java)

Nếu bạn muốn sử dụng các thư viện, chúng chỉ phải sử dụng API có sẵn trong Android. Ví dụ: các gói java.awt và javax.swing không có sẵn trên Android.

Ngoài các tệp JAR, Android sử dụng định dạng phân phối nhị phân được gọi là Android ARchive (AAR). Gói .aar là bản phân phối nhị phân của Dự án Thư viện Android.

Một AAR tương tự như một tệp JAR, nhưng nó có thể chứa các tài nguyên cũng như mã byte được biên dịch. Tệp AAR có thể được bao gồm trong quá trình xây dựng ứng dụng Android tương tự như tệp JAR.

Có thể tạo các mô-đun thư viện có thể được sử dụng làm phụ thuộc trong các dự án Android. Các mô-đun này cho phép bạn lưu trữ mã nguồn và tài nguyên Android có thể được chia sẻ giữa một số dự án Android khác.

Để sử dụng thư viện Java (tệp JAR) trong dự án Android của bạn, bạn có thể sao chép đơn giản tệp JAR vào thư mục có tên libs trong ứng dụng của bạn. Các tệp * .jar trong thư mục này được bao gồm trong đường dẫn biên dịch thông qua tệp build.gradle mặc định
2. Custom Android library modules
2.1. Using custom library modules
An Android library module can contain Java classes, Android components and resources. Only assets are not supported.

The code and resources of the library project are compiled and packaged together with the application.

Therefore a library module can be considered to be a compile-time artifact.

2.2. Creating custom Android library modules
Using library projects helps you to structure your application code. To create a new library module in Android Studio, select File  New Module and select Android Library.
2. Các mô-đun thư viện Android tùy chỉnh
2.1. Sử dụng các mô-đun thư viện tùy chỉnh
Một mô-đun thư viện Android có thể chứa các lớp Java, các thành phần và tài nguyên Android. Chỉ tài sản không được hỗ trợ.

Mã và tài nguyên của dự án thư viện được biên dịch và đóng gói cùng với ứng dụng.

Do đó, một mô-đun thư viện có thể được coi là một tạo tác thời gian biên dịch.

2.2. Tạo mô-đun thư viện Android tùy chỉnh
Sử dụng các dự án thư viện giúp bạn cấu trúc mã ứng dụng của bạn. Để tạo mô-đun thư viện mới trong Android Studio, chọn Tệp Mô-đun mới và chọn Thư viện Android.
3. Prerequisite
The following example assumes that you have created an Android project with the com.example.android.rss feed top level package based on the following tutorial:
3. Điều kiện tiên quyết
Ví dụ sau đây giả định rằng bạn đã tạo một dự án Android với gói cấp cao nhất của com.example.android.rss dựa trên hướng dẫn sau:
4. Exercise: Create an Android library module
Our library project will contain the data model and a method to get the number of instances. The library provides access to (fake) RSS data. An RSS document is an XML file which can be used to publish blog entries and news.

4.1. Create library module
For Android Studio each library is a module. To create a new library module in Android Studio, select File  New Module and select Android Library.
4. Bài tập: Tạo mô-đun thư viện Android
Dự án thư viện của chúng tôi sẽ chứa mô hình dữ liệu và một phương thức để lấy số lượng phiên bản. Thư viện cung cấp quyền truy cập vào dữ liệu RSS (giả). Tài liệu RSS là một tệp XML có thể được sử dụng để xuất bản các mục blog và tin tức.

4.1. Tạo mô-đun thư viện
Đối với Android Studio, mỗi thư viện là một mô-đun. Để tạo mô-đun thư viện mới trong Android Studio, chọn Tệp Mô-đun mới và chọn Thư viện Android.
Creating libraries for Android applications

Use com.example.android.rssfeedlibrary as module name and Rssfeed Library as library name.
Sử dụng com.example.android.rssfeedl Library làm tên mô-đun và Thư viện Rssfeed làm tên thư viện.

If prompted for a template select that no activity should be created. As a result Android Studio shows another module.
Nếu được nhắc cho một mẫu, chọn không có hoạt động nào được tạo. Kết quả là Android Studio hiển thị một mô-đun khác.

4.2. Remove generated dependency from build.gradle of the library project
Open the build.gradle of the library project. Delete the dependencies closure, your library does not need any dependency and the generated dependency can cause problems for the build.
4.2. Xóa phụ thuộc được tạo ra từ build.gradle của dự án thư viện
Mở build.gradle của dự án thư viện. Xóa việc đóng phụ thuộc, thư viện của bạn không cần bất kỳ phụ thuộc nào và phụ thuộc được tạo có thể gây ra sự cố cho bản dựng.
4.3. Create the model class
Create an RssItem class which can store data of an RSS entry.

Generate the getters and setter, the constructor and a toString() method. The result should look like the following class:
4.3. Tạo lớp mô hình
Tạo một lớp RssItem có thể lưu trữ dữ liệu của một mục RSS.

Tạo getters và setter, hàm tạo và phương thức toString (). Kết quả sẽ trông giống như lớp sau:
public class RssItem {
    private String pubDate;
    private String description;
    private String link;
    private String title;
    public RssItem() {
    }
    public RssItem(String title, String link) {
        this.title = title;
        this.link = link;
    }
    public String getPubDate() {
        return pubDate;
    }
    public void setPubDate(String pubDate) {
        this.pubDate = pubDate;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getLink() {
        return link;
    }
    public void setLink(String link) {
        this.link = link;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    @Override
    public String toString() {
        return "RssItem [title=" + title + "]";
    }
}
4.4. Create instances
Create a new class called RssFeedProvider with a static method to return a list of RssItem objects. This method does currently only return test data.
4.4. Tạo trường hợp
Tạo một lớp mới gọi là RssFeedProvider với một phương thức tĩnh để trả về một danh sách các đối tượng RssItem. Phương pháp này hiện chỉ trả về dữ liệu thử nghiệm.
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class RssFeedProvider {
    public static List<RssItem> parse(String rssFeed) {
        List<RssItem> list = new ArrayList<>();
        Random r = new Random();
        // random number of item but at least 5
        Integer number = r.nextInt(10) + 5;
        for (int i = 0; i < number; i++) {
            // create sample data
            String s = String.valueOf(r.nextInt(1000));
            RssItem item = new RssItem("Summary " + s, "Description " + s);
            list.add(item);
        }
        return list;
    }
}
4.5. Define dependency to the library project
To use the library add it as a dependency in your project select File  Project Structure. Select the app entry. Switch to the Dependencies tab and select Module dependencies via the + sign.
4.5. Xác định sự phụ thuộc vào dự án thư viện
Để sử dụng thư viện, hãy thêm nó làm phụ thuộc trong dự án của bạn, chọn Cấu trúc dự án tệp. Chọn mục ứng dụng. Chuyển sang tab Phụ thuộc và chọn Phụ thuộc mô-đun thông qua dấu +.


4.6. Use library project to update detailed fragments
Update the updateDetail method in your MyListFragment class to use the RssFeedProvider provider. This is only test code.
4.6. Sử dụng dự án thư viện để cập nhật các đoạn chi tiết
Cập nhật phương thức updateDetail trong lớp MyListFragment của bạn để sử dụng nhà cung cấp RssFeedProvider. Đây chỉ là mã kiểm tra.
public class MyListFragment extends Fragment {
..// everything as before
    // triggers update of the details fragment
    public void updateDetail(String uri) {  //
        List<RssItem> list = RssFeedProvider
                .parse(uri);
        String itemListAsString = list.toString();
        listener.onRssItemSelected(itemListAsString);
    }
4.7. Validate implementation
Start your application and ensure that the toString value of the list of RssItems is displayed in the DetailFragment.

The list is currently generated randomly every time you press the button.
5. Exercise: Deploy a library project
Create a new library project called recyclerbaseadapter with the same top level package. Add the following to its build.gradle file.
4.7. Xác thực thực hiện
Bắt đầu ứng dụng của bạn và đảm bảo rằng giá trị toString của danh sách RssItems được hiển thị trong Chi tiết.

Danh sách hiện được tạo ngẫu nhiên mỗi khi bạn nhấn nút.
5. Bài tập: Triển khai một dự án thư viện
Tạo một dự án thư viện mới gọi là recyclerbasead CHƯƠNG với gói cấp cao nhất. Thêm phần sau vào tệp build.gradle của nó.
apply plugin: 'maven'
group = 'com.vogella.libraries'
version = '1.0'
uploadArchives {
    repositories {
        mavenLocal()
    }
}
Create or move a MyBaseAdapter class in this library.

Deploy it by running the gradle uploadArchives task.

You can now define a dependency to this library, by adding mavenLocal() and using:
Tạo hoặc di chuyển một lớp MyBaseAd CHƯƠNG trong thư viện này.

Triển khai nó bằng cách chạy tác vụ uploadArchives gradle.

Bây giờ bạn có thể xác định một phụ thuộc vào thư viện này, bằng cách thêm mavenLocal () và sử dụng:
compile 'com.vogella.libraries:recyclerbaseadapter:1.0`
6. Android library resources
Google guide to install the support libraries

Using the Android support libraries from Google
6. Tài nguyên thư viện Android
Hướng dẫn của Google để cài đặt các thư viện hỗ trợ

Sử dụng các thư viện hỗ trợ Android từ Google

No comments:

Post a Comment