📜 ⬆️ ⬇️

AndroidX patch


On Google I / O 2018, a replacement was provided to the existing support-libraries - AndroidX


Initially, support libraries were developed for backward compatibility of new APIs and were closely associated with the operating system. Development of support-libraries was carried out in the internal branches, which periodically merged into the Android Open Source Project (AOSP). This approach limited merge pull requests from the community to small periods of time when the AOSP code and the internal Google code were synchronized. In addition, to work with support libraries, it was necessary to download the entire platform code, which is more than 40GB of source code. For my 250 GB disk, this is quite a lot.


The current functionality of the support libraries is much wider than the original idea. For example, it implemented a component to simplify the development of the AppCompat user interface, a component for working with the Room databases, a component for the background tasks of the WorkManager. Many of these libraries are initially backward compatible and weakly tied to the Android API. The number in the support library number means the minimum API level it supports. For example, support-v7 supports Android API version 7 and higher. However, starting from version 26.0.0, the support libraries support Android API 14 and higher. Separate pain gives the need to simultaneously update all support-libraries. All this indicates that support libraries have become obsolete and need to be rethought.


The development team spent several years allocating support libraries into a separate small project, which you can work with using Android Studio and Gradle. The development was moved to a separate branch, which recently became public. The updated libraries are called AndroidX. Another important difference of new libraries is the ability to independently update. Google promises binary compatibility within a single major version, which will allow using recyclerview version 1.0 and AppCompat version 1.9 in a single project.


In my opinion, this is the right and logical step in the development of support-libraries. I had to strongly customize components from support-libraries several times, which led to the need to create a com.android.support package in my project ... to access package-private classes / methods / fields.


Now I propose to “hack” some library from the AndroidX family with me. As a tutorial, I chose CardView. I'm going to influence the behavior of CardView without making changes to the code that uses it.


We need: A computer running Linux or MacOS (Windows is not supported), Android SDK, optional - Android Studio (I used version 3.1.3)


How is Windows?

To download the source, it is recommended to use the repo utility, which is not available for Windows. Sources can be downloaded using git, for example: git clone --single-branch -b androidx-master-dev https://android.googlesource.com/platform/frameworks/support However, in this case they will not downloaded utilities and compiled dependencies. I did not check how critical it is for assembly


To begin with, I prepared a small example using AndroidX.


Highlights:


Using AndroidX


 def cardViewVer = '1.0.0-beta01' dependencies { implementation "androidx.cardview:cardview:$cardViewVer" } 

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="32dp" android:layout_marginEnd="32dp" android:layout_marginLeft="32dp" android:layout_marginRight="32dp" android:layout_marginStart="32dp" android:layout_marginTop="32dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> </androidx.cardview.widget.CardView> </FrameLayout> 

 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } 

As a result, we obtain the following application:


drawing

Let's get down to AndroidX


First, you need to install the repo utility, created to simplify the work with git in the context of android.


Create a working folder for our manipulations, although you can use the home directory.


 mkdir androidX 

Install the utility


 d androidX mkdir bin curl https://storage.googleapis.com/git-repo-downloads/repo > ./bin/repo chmod a+x ./bin/repo PATH={some_path}/androidX/bin/:$PATH 

Let me explain what we have done now:
In the androidX folder, we created the repo folder, where we downloaded the file from the link https://storage.googleapis.com/git-repo-downloads/repo , made the file executable, and added the bin folder to the PATH during the current terminal session.
In my case, the last command looked like this: PATH = ~ / Work / projects / androidX / bin /: $ PATH


Downloading AndroidX sources:


Create a folder androidX / androidX-source and make it current


 mkdir androidX-source cd androidX-source 

Initialize the local repository. Approximately 16 megabytes of data about existing branches in the repository are downloaded.


 repo init -u https://android.googlesource.com/platform/manifest -b androidx-master-dev 

In the process, the user’s name and e-mail will be pulled from the global config of the gita, in my case, it looked like this:


 Your identity is: Andrew <me@example.com> If you want to change this, please re-run 'repo init' with --config-name      ,      .          . Testing colorized output (for 'repo diff', 'repo status'): black red green yellow blue magenta cyan white bold dim ul reverse Enable color display in this user account (y/N)? 

I answered yes to the last question in the affirmative.
At the end we get the message


 repo has been initialized in /Users/{user}/Work/projects/androidX/androidX-source 

Next, download the source directly (about 3 gigabytes)


 repo sync -j8 -c 

We can open downloaded sources in Android Studio or any other editor. The root folder of the gradle project is located at: androidX/androidX-source/frameworks/support/


There are many modules with different features and several test applications. We can assemble and install them to test performance.


Open the androidx.cardview.widget.RoundRectDrawable class in the cardview module
Add a harmless joke to the onDraw method


 canvas.drawText(“Hacked!”, 100, 100, paint); 

Full patch


For the project, the createArchive gradle task is described, which will assemble the androidX libraries and place them in the local maven repository. Repository address: androidX/androidX-source/out/host/gradle/frameworks/support/build/support_repo


To use it, you must specify the path in the root build file.


maven { url 'androidX/androidX-source/out/host/gradle/frameworks/support/build/support_repo' }


Please note that the assembled version may be newer than in the Google repository. At the time of this writing, I have compiled the androidX library version 1.0.0-rc01. You can view the version of the compiled library in the local maven repository: androidX/androidX-source/out/host/gradle/frameworks/support/build/support_repo/androidx/cardview/cardview/maven-metadata.xml


Updated example


Re-create our application and see the following picture:


drawing

AndroidX successfully patched!


What does this give us:



Related Links:
Documentation for support libraries
Post developer blog
Instructions for Contribution in AndroidX
AndroidX Task Tracker
AndroidX Sources


')

Source: https://habr.com/ru/post/418723/


All Articles