Android Studio uses Support library instead of AndroidX

Record

Recently, I am researching hook technology. When looking for hook points, it is inevitable to read the source code. On different sdk source codes, there will always be more or less changes in the method. Then the device in my hand is 8.1, which is API27. Reduce the app version to 27, so that it can follow the source code of 27. As a result, there was a problem.

The Android studio version I am currently using is 4.1, and after AS3.5, when creating a new project, the default integrated support library is adjusted from v7 to androidx. Here comes the problem, the minimum supported API version of androidx is 28, if it is changed to 27, an error will be reported.

background

Before solving the problem, let's take a look at what androidx is.
The following content is excerpted from my Aunt Guo's blog. For details, please go to What is AndroidX?

When the Android system was first launched, even its designers may not have thought that it would be so successful, so it was impossible to consider its API very well from the beginning. With the continuous iterative update of the Android system version, many new APIs will be added in each version, but the newly added APIs do not exist in the old version system, so there is a problem of backward compatibility.
For example, when the Android system was released to version 3.0, it suddenly realized the importance of tablets. Therefore, in order to make Android better compatible with tablets, the Android team added the Fragment function to the 3.0 system (API 11). But the role of Fragment is not limited to tablets. What should I do if I want to use this function in the old system? So the Android team launched a well-known Android Support Library to provide backward compatibility. For example, the support-v4 library and appcompat-v7 library that everyone is familiar with belong to the Android Support Library. I believe that anyone who has done Android development has used these two libraries.
But many people may not have considered what the name of the support-v4 library means, so let me explain it to you here. 4 here refers to the Android API version number, and the corresponding system version is 1.6. Then support-v4 means that the API provided in this library will be backward compatible to the Android 1.6 system. Its corresponding package name is android.support.v4.app.
Similarly, appcompat-v7 refers to backward compatibility of the API provided in the library to API 7, which is the Android 2.1 system. Its corresponding package name is android.support.v7.appcompat
It can be found that the package names of the libraries provided in the Android Support Library all start with android.support.*.
But slowly with the passage of time, the 1.6 and 2.1 systems have long been eliminated. Now the minimum system version officially supported by Android is 4.0.1, and the corresponding API version number is 15. The support-v4 and appcompat-v7 libraries no longer support such long-standing systems, but their names have been retained, although their actual functions now do not match the original naming reasons.
Obviously, the Android team also realized that this naming is very inappropriate, so they re-divided the architecture of these APIs and launched AndroidX.

In short, androidx is an extended library of the android operating system, and many APIs we usually use for development come from this library.

To solve the problem, downgrade the support library from androidx to android.support.v7

The first step is to change compileSdkVersion and targetSdkVersion back to 27

insert image description here
The error Android resource linking failed is reported. The reason is that the androidx library is used in the project mentioned above, and the minimum support for api28 is. If you change it directly to 28 here, there will be no problem, but I want 27, so I can only continue to solve the problem.
insert image description here

The second step is to change the androidx related dependencies to the support library

insert image description here
Mainly the replacement of the support library

没更改之前
implementation 'androidx.appcompat:appcompat:1.1.0'
更改之后
implementation 'com.android.support:appcompat-v7:28.0.0'

A bunch of errors are reported, or Android resource linking failed, because our project still has calls to resources in the androidx library.
insert image description here
insert image description here

The third step is to remove the call to the resources in the androidx library in the project

There are prompts in the log for the wrong call path, open and remove in turn, for example:
G:\workspace2020\Test2\app\src\main\res\values\themes.xml:3:5
G:\workspace2020\Test2\app\src \main\res\values-night\themes.xml:3:5
Open the file and find that there are red marks, indicating that these resources cannot be found in the support
insert image description here
. Replace the Theme.MaterialComponents.DayNight.DarkActionBar theme with the existence in the Support library Themes, such as Theme.AppCompat.Light.NoActionBar, the remaining popular ones should be deleted.
There is also the call in the code, androidx should be replaced with the Support library,
but at this time I found that the code showed that the Support library could not be found. If I
insert image description here
run it at this time, it still shows linking failed
insert image description here
, so I opened the Project Structure to see the project dependencies. That's right,
insert image description here
but there is an All Moudules on it, and the displayed dependencies are obviously problematic,
insert image description here
so I searched globally, oh well, there are still many places that have not been changed. But the point is the configuration of android.useAndroidX=true
insert image description here

The fourth step removes the configuration of the androidx library in the project

Open this folder, you can see two configurations. The most important thing is not that I found android.useAndroidx=true, but the configuration of android.enableJetifier=true.
If you are a new project and use AndroidX related dependencies, you need to add configuration in the gradle.properties file:

android.useAndroidX=true
android.enableJetifier=true

If you want to use AndroidX, but the previous version does not migrate, you can configure it like this:

android.useAndroidX=true
android.enableJetifier=false

Then we don't want to use Androidx now, so just delete these two lines.
insert image description here
over

Guess you like

Origin blog.csdn.net/changhuzichangchang/article/details/116096606