AndroidX

AndroidX

Um neue Funktionen in älteren Android-Versionen nutzen zu können, hat Google lange Zeit so genannte Support-Bibliotheken angeboten. Diese hatte im Namen immer die Android-Version, ab der diese funktioniert haben. Das galt innerhalb der Bibliothken aber leider nicht für alle APIs, so dass die Version (wie android.support.v4) eher zur Verwirrung führten. Außerdem wurde diese wenige Bibliotheken immer größer.

Um diesen Umstand Rechnung zu tragen, wurde das AndroidX Projekt gestartet, der die Support-Bibliotheken mehr modularisiert und die Versionsnummern nicht mehr in den Packages führt. Bei einer sauberen Neuinstalltion von Android Studio (seit Version 3.4) wird bei Projektanlage automatisch AndroidX als Support-Bibliothek vorausgewählt.

Die Alten Projekte können sehr einfach auf AndroidX umgestellt werden. Dazu gibt es unter Android Studio den Menüpunkt Refactor | Migrate to AndroidX .... Dabei werden die Abhängigkeiten in build.gradle ausgetauscht und in den Java und XML-Dazeien die Importe angepasst.

So wird zum Beispiel aus folgenden Code in build.grade

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
...
}

ein

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
...
}

Die Importe in XML und Java wechseln von

package de.webducer.androidbuch.zeiterfassung.dialogs;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.widget.DatePicker;

import java.util.Calendar;
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/IssueList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingStart="@dimen/ActivityHorizontalPadding"
    android:paddingTop="@dimen/ActivityVerticalPadding"
    android:paddingEnd="@dimen/ActivityHorizontalPadding"
    android:paddingBottom="@dimen/ActivityVerticalPadding"
    tools:context=".InfoActivity">

</android.support.v7.widget.RecyclerView>

zu

package de.webducer.androidbuch.zeiterfassung.dialogs;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import android.widget.DatePicker;

import java.util.Calendar;
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/IssueList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingStart="@dimen/ActivityHorizontalPadding"
    android:paddingTop="@dimen/ActivityVerticalPadding"
    android:paddingEnd="@dimen/ActivityHorizontalPadding"
    android:paddingBottom="@dimen/ActivityVerticalPadding"
    tools:context=".InfoActivity">

</androidx.recyclerview.widget.RecyclerView>