AndroidX - Android Extension Library

Android X or Extension New Name and Packaging for the Andrioid Support Library .

What is Support Library ? 

The Android Support Library package is a set of code libraries that provide backward-compatible versions of Android framework APIs as well as features that are only available through the library APIs. Each Support Library is backward-compatible to a specific Android API level.


What is AndroidX?


AndroidX is the open-source project that the Android team uses to develop, test, package, version and release libraries within Jetpack.
Like the Support Library, AndroidX ships separately from the Android OS and provides backwards-compatibility across Android releases .

Need of AndroidX


  • The Organic growth of the library has become confusing .
  • There are Components and packages named "v7" when the minimal SDK level we support is 14 .
  • Weired packing name diffcult to change and confusing hard to change .

Writing Android apps means depending on two kinds of classes ( android.* vs androidx.* namespaces )


  1. Classes like PackageManager, which are bundled with the operating system and can have different APIs and behavior for different Android versions
  2. Classes like AppCompatActivity or ViewModel, which are unbundled from the operating system and ship in your apk. These libraries are written to provide a single API surface with behavior that's as consistent as possible across Android versions.

 Bundled libraries :  bundled with OS can have different API and behavior.
 UnBundled libraries : (androidx.) unbundled from OS and ships in your apk.


AndroidX uses Semantic-version


Previously, support libraryuse SDK version but AndroidX use the Semantic-version. It’s going to re-version from 28.0.0 → 1.0.0.

Generally, you can expect the following mapping from old to new packages:

Support Library Components have been moved under androidx and their package name simplified .

OLD
New
 android.support.**  androidx.@
 android.databinding.**             androidx.databinding.@
 android.design.**  com.google.android.material.@     

The Architecture Components libraries have also been moved under androidx and their package names simplified .

OLD
New
android.arch.**  androidx.@
android.arch.persistence.room.**       androidx.room.@                          
android.arch.persistence.**  androidx.sqlite.@

Complete Mapping : https://developer.android.com/jetpack/androidx/migrate#class_mappings

How to migrate current project to androidx 

In Android Studio 3.2 (September 2018), there is direct option to migrate existing project to AndroidX

Android Studio > Refactor Menu > Migrate to AndroidX. It will analysis and will open Refractor window in bottom. Accept changes to be done.




Below two flags gets added to gradle.properties file .

android.enableJetifier=true
android.useAndroidX=true 

Below are the Changes Before and After Migrating Sample App To AndroidX .

build.gradle 

Before :

dependencies {
 implementation fileTree(dir: 'libs', include: ['*.jar'])
 implementation 'com.android.support:appcompat-v7:28.0.0'
 implementation 'com.android.support.constraint:constraint-layout:1.1.3'
 testImplementation 'junit:junit:4.12'
 androidTestImplementation 'com.android.support.test:runner:1.0.2'
 androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

After   :

dependencies {
 implementation fileTree(dir: 'libs', include: ['*.jar'])
 implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
 implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
 testImplementation 'junit:junit:4.12'
 androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
 androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}

XML Layout 

Before :

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity">

<TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Hello World!"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintRight_toRightOf="parent"
 app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>


After   :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity">

<TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Hello World!"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintRight_toRightOf="parent"
 app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>


Imports  

Before :

package com.tutorialsbuzz.androidsupport;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

After   :

package com.tutorialsbuzz.androidsupport;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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


No comments:

Post a comment