Android, Codes and Tips

Just in

12/14/2019

Migrating from Android to Androidx appComponentFactory solution


After working and searching for hours and you can't get the solution you are looking for. You are welcome to the home of Android Developer. If you have being using Android Studio for writing Java apps, Java codes is everything sensitive raging from capital letters, punctuation errors, misspelled of words and plugin updates.

Android Studio comes up with new updates always which depreciated the older once. A code you write today, use it and develop an app may be outdated and depreciated tomorrow. If you copy somebody's code or forked a code from github, there are chances that the code which worked earlier won't work again unless you do serious editing.


Dependencies using groupId com.android.support and androidx.* can not be combined but found IdeMavenCoordinates{myGroupId='com.android.support', myArtifactId='interpolator', myVersion='28.0.0', myPacking='aar', myClassifier='null'} and IdeMavenCoordinates{myGroupId='androidx.asynclayoutinflater', myArtifactId='asynclayoutinflater', myVersion='1.0.0', myPacking='aar', myClassifier='null'} incompatible dependencies less... (Ctrl+F1) 

Inspection info:There are some combinations of libraries, or tools and libraries, that are incompatible, or can lead to bugs. One such incompatibility is compiling with a version of the Android support libraries that is not the latest version (or in particular, a version lower than your targetSdkVersion). Issue id: GradleCompatible

Solution of appFactory Android Studio app due to copying, mixing and joining a android and androidx XML files.

dependencies {
    //...
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    
    // materail io
    implementation 'com.google.android.material:material:1.0.0'
  
    //...    
}
Sync gradle. If you will be lucky as me you will see an error
Manifest merger failed : Attribute [email protected] value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91
 is also present at [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory).
 Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:5:5-19:19 to override.
It happened because we should not use the com.android.support and com.google.android.material dependencies in the app at the same time. I will exploit the circumstances to migrate to androidx. In the mapping table we can see com.android.support:appcompat-v7 maps to androidx.appcompat:appcompat:1.0.0
dependencies {
    //...    
    // to android x
    implementation 'androidx.appcompat:appcompat:1.0.0'
    
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'

    // materail io
    implementation 'com.google.android.material:material:1.0.0'
    //...
}
Sync and rebuild our project. There is no error with sync but rebuild finished with ‘compilation failed’. That’s because we are using import from now unknown library (android.support)
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

Most common replacements are:

    android.support.v7.widget.RecyclerView → androidx.recyclerview.widget.RecyclerView
    androidx.constraintlayout.ConstraintLayout → androidx.constraintlayout.widget.ConstraintLayout
    android.support.constraint.ConstraintLayout → androidx.constraintlayout.widget.ConstraintLayout
    android.support.constraint.Group → androidx.constraintlayout.widget.Group
    android.support.v7.widget.GridLayoutManager → androidx.recyclerview.widget.GridLayoutManager
    android.support.v7.widget.LinearLayoutManager → androidx.recyclerview.widget.LinearLayoutManager

<android.support.design.widget.AppBarLayout--><com.google.android.material.appbar.AppBarLayout
<android.support.v7.widget.Toolbar--> <androidx.appcompat.widget.Toolbar

android.support.design.widget.CoordinatorLayout
android.support.design.widget.AppBarLayout
android.support.v7.widget.Toolbar
android.support.design.widget.TabLayout
android.support.v4.view.ViewPager
to this;

androidx.coordinatorlayout.widget.CoordinatorLayout
com.google.android.material.appbar.AppBarLayout
androidx.appcompat.widget.Toolbar
com.google.android.material.tabs.TabLayout
androidx.viewpager.widget.ViewPager

In build.gradile

add implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.android.volley:volley:1.1.1'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'androidx.cardview:cardview:1.0.0'

Also check the core AndroidX dependencies

android.support.design.widget.BottomSheetDialog
com.google.android.material.bottomsheet.BottomSheetDialog 

android.support.design.widget.TextInputLayout
com.google.android.material.textfield.TextInputLayout 

android.support.design.widget.CoordinatorLayout
androidx.coordinatorlayout.widget.CoordinatorLayout

android.support.design.widget.NavigationView
com.google.android.material.navigation.NavigationView

androidx.core.view.ViewPager
androidx.viewpager.widget.ViewPager

android.support.design.widget.BottomSheetBehavior
com.google.android.material.bottomsheet.BottomSheetBehavior 

android.support.v7.widget.Toolbar
androidx.appcompat.widget.Toolbar 

android.support.design.internal.BottomNavigationItemView
com.google.android.material.bottomnavigation.BottomNavigationItemView

android.support.design.internal.BottomNavigationMenuView
com.google.android.material.bottomnavigation.BottomNavigationMenuView 

androidx.appcompat.widget.CardView
androidx.cardview.widget.CardView 

android.support.design.widget.BottomNavigationView
com.google.android.material.bottomnavigation.BottomNavigationView

androidx.core.view.ViewPager
androidx.viewpager.widget.ViewPager

importandroidx.core.widget.DrawerLayout
androidx.drawerlayout.widget.DrawerLayout 

androidx.appcompat.widget.RecyclerView
androidx.recyclerview.widget.RecyclerView 

androidx.core.view.PagerAdapter
androidx.viewpager.widget.PagerAdapter

importandroidx.core.app.FragmentManager
importandroidx.fragment.app.FragmentManager

No comments:

Post a Comment