New Project with AndroidX and material io

David Cargatser
2 min readOct 1, 2018

--

Let’s set up new project using androidx and material io, but first a few words …

AndroidX

In may 2018 google launched the new Android extension libraries (AndroidX) which represents a new era for the Support Library. Generally speaking broad range of libraries were rearranged to encourage smaller, more focused libraries. For example, android.support.* was redesigned to androidx.* and android.databinding.* to androidx.databinding.*. That means that in future we will see androidx as much as we see support library today.

Material io

Note that android.design.* was changed to com.google.android.material.*

Others libraries

Ok, but how do you now what library has been redesigned and what is the new name? Google made a great mapping and all you need is to find your package.

Create New Project

Create a new project and navigate to build.gradle app module. Let’s first add material io just as explained here. I assume that google() is already inside repositories in your project gradle file

Now add library to dependencies section. the version of library I get from github material-component’s page

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 application@appComponentFactory 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;

public class MainActivity extends AppCompatActivity {

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

Change to androidx.appcompat library from androidx and rebuild

//import android.support.v7.app.AppCompatActivity;
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);
}
}

Nice! All Fixed! All we need now is change default them and we can use any elements from material io to android

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>

</resources>

add text field

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter here">

<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>

and run in a simulator

Conclusion

We set up a new project with new Android extension libraries (AndroidX) that replaces one package from support library. We also added new design library from google to support material io and now can use any elements from there in our project

--

--