A brief introduction to Jetpack, an Android interview standardized development library

Composition of Jetpack

Composition of Jetpack

As you can see in the figure, the composition of Jetpack is divided into four parts: Architecture, Foundation, Behavior, UI

Architecture mainly includes:

  • Date Binding
  • Lifecycles
  • LiveData
  • Navigation
  • Paging
  • Room
  • ViewModel
  • WorkManager

DataBinding

DataBinding solves the consistency problem of view calling.

By binding the observable data in the layout, the control is notified to update the data content after the bound observed data content is set.

 

What is Lifecycles?

Lifecycles mainly solves the consistency problem of life cycle management. In the past, life cycle management all relied on manual maintenance. For example, GpsManager needed to activate, unbind, and stop each Activity page used. It is easy to cause problems when there are more Activity pages.

Lifecycles encapsulates all the complex operations of life cycle management in the base class of LifeCycleOwner (such as the base class of view controller Activity) through template method and observer mode, saving the developer the trouble of manual writing. Developers can directly call getLifecycle().addObserver(GpsManager.getInstance()) in the subclass to realize the life cycle awareness of LIfeCycleOwner.

 

What is LiveData?

LiveData follows the standardized development concept of the distribution status of the only trusted source , so that it is difficult to trace and troubleshoot in the rapid development process, and the probability of unpredictable problems is minimized.

The only source of trust is a data component whose life cycle is independent of the view controller. It is usually a singleton or ViewModel. ViewModel can also be regarded as a singleton, a pseudo singleton implemented by the factory pattern.

 

What is Navigation?

Navigation is a library and plugin to simplify Android navigation. Navigation can be used to manage the switching of Fragment, and the interactive process of App can be seen in a visual way.

Advantages of Navigation

  1. The official standard handles the switching of fragments
  2. Correctly handle the forward and backward of Fragment by default
  3. Provide standardized resources for transitions and animations
  4. Can realize and handle deep links
  5. Can bind Toolbar, BottomNavigationView and ActionBar
  6. SafeArgs (Gradle plugin) provides type safety when passing data
  7. ViewModel support

 

Paging

Help developers to load and display small pieces of data at once. Loading part of the data on demand can reduce the use of network bandwidth and system resources.

 

Room

Room persistence library provides an abstraction layer on SQLite to help developers access SQLite databases more friendly and smoothly.

 

ViewModel

The existence of ViewModel is mainly to solve the problems of state management and page communication . When the view controller is rebuilt, the lightweight state can be stored and restored through serialization through the onSaveInstanceState of the view controller base class. But for the heavyweight state, such as the data collection returned by the network request, it can be stored in the ViewModel with a longer life cycle than the view controller, so that it can be directly restored from the ViewModel, which is an inefficient and inefficient serialization method.

 

WorkManager

Even if the application exits or the device restarts, it is easy to schedule the deferrable asynchronous tasks that are expected to run.

 

Foundation mainly includes:

  • AppCompat
  • Android KTX
  • Multidex
  • Test

AppCompat

A series of APIs beginning with AppCompat are provided to be compatible with lower versions of Android development.

 

Android KTX

Android KTX is a Google open source extension designed to make the Kotlin code on Android more concise, thereby improving development efficiency and user experience.

Use case:

1. Convert string to URI

// Kotlin
val uri = Uri.parse(uriString)

// android KTX
val uri = uriString.toUri()

2、SharedPreferences

// kotlin
sharedPreferences.edit()
    .putBoolean("key", value)
    .apply()

// KTX
sharedPreferences.edit {
    putBoolean("key", value)
}

3. View's preDraw monitoring

// kotlin
view.viewTreeObserver.addOnPreDrawListener(
    object : ViewTreeObserver.OnPreDrawListener {
        override fun onPreDraw(): Boolean {
            viewTreeObserver.removeOnPreDrawListener(this)
            actionToBeTriggered()
            return true
        }
    })

// KTX
view.doOnPreDraw {
     actionToBeTriggered()
}

Multidex (launched on Android 5.0)

Reason for use: A Dex file in Android can store up to 65536 methods, which is a range of short type. However, as the number of application methods continues to increase, when the Dex file exceeds the number of 65536 methods, an exception will be thrown when packaging, so Multidex is introduced.

  • When packaging, divide an application into multiple dex, for example: classes.dex, classes2.dex, classes3.dex..., when loading, add these dex to the corresponding array of DexPathList, which solves the limitation of the number of methods.
  • After Andorid 5.0, the ART virtual machine naturally supports MultiDex.
  • Before Andorid 5.0, the system only loads one main dex, and the other dex uses MultiDex to load.

Instructions:

1. minSdkVersion is 21 or higher

android {
    defaultConfig {
        ...
        minSdkVersion 21 
        targetSdkVersion 26
        multiDexEnabled true
    }
    ...
}

2. minSdkVersion is 20 or lower

android {
    defaultConfig {
        ...
        minSdkVersion 15 
        targetSdkVersion 26
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.3'
}

Define Application code related (two methods)

// 继承 MultiDexApplication
public class MyApplication extends MultiDexApplication { ... }


// 或者 在Application中添加 MultiDex.install(this);
public class MyApplication extends Application {
  @Override
  protected void attachBaseContext(Context base) {
     super.attachBaseContext(base);
     MultiDex.install(this);
  }
}

 

Test

Android testing framework for unit and runtime interface testing.

 

Behavior mainly includes:

  • DownloadManager
  • Media&Playback
  • Permissions
  • Notifications
  • Sharing
  • Slices

 

DownloadManager

It can handle long-running HTTP downloads, and retry the download after a failure or connection change and system restart.

Media&Playback

Backward compatible API for media playback and routing (including Google Cast).

Permissions

Compatibility API for checking and requesting application permissions.

Notifications

Provides backward compatible notification API, supports Wear and Auto.

Sharing

Provides a sharing operation suitable for the application operation bar.

Slices

Create flexible interface elements that can display application data outside of the application.

Slices is an extension of Google Assistant. Google hopes that users can quickly reach a certain feature in the App. For example, if you tell Google Assistant that you want to go home, then there may only be Didi and Uber options before. , But after the introduction of Slices, a more detailed data list will be displayed, such as the distance to home, how much money, and whether to take a taxi immediately under the Didi item.

 

UI mainly includes:

  • Animation&Transitation
  • Auto,TV&Wear
  • Emoji
  • Fragment
  • Layout
  • Palette

Palette

Palette is similar to a palette tool. According to the incoming bitmap, the main color is extracted, making the picture and color more matched and the interface more coordinated.

The colors that Palette can extract are as follows:

  1. Vibrant
  2. Vibrant dark (vibrant dark)
  3. Vibrant light (Vibrant light)
  4. Muted (soft)
  5. Muted dark (soft dark color)
  6. Muted light (soft bright color)

There are four methods to create a Palette object. You need to pass in the Bitmap object. The first two below are synchronous methods, and the latter two are asynchronous methods.

  1. generate(Bitmap bitmap)
  2. generate(Bitmap bitmap, int numColors)
  3. generateAsync(Bitmap bitmap, PaletteAsyncListener listener)
  4. generateAsync(Bitmap bitmap, int numColors, final PaletteAsyncListener listener)

After getting the Palette object, you can get the Swatch objects corresponding to the 6 colors

  1. Palette.getVibrantSwatch()
  2. Palette.getDarkVibrantSwatch()
  3. Palette.getLightVibrantSwatch()
  4. Palette.getMutedSwatch()
  5. Palette.getDarkMutedSwatch()
  6. Palette.getLightMutedSwatch()

The Swatch object is an internal class of Palette, corresponding to the following methods to obtain the color value

  1. getPopulation(): the number of pixels in the sample
  2. getRgb(): RBG value of the color
  3. getHsl(): HSL value of the color
  4. getBodyTextColor(): The color value of the body text
  5. getTitleTextColor(): the color value of the title text

 

Guess you like

Origin blog.csdn.net/nsacer/article/details/108528123