android development package upgrade guide to androidx stepped pit recorded [Reserved]

Android developers have been doing lately, but since the information in the hands of older, some of the information is often not on the Import, very headache.

Thanks [on the book Jane Zhang sunny every day, every day] to do finishing.

Here also the record about spare.

After the upgrade Android Studio, in Refactor ---> Migrate to the next AndroidX path can upgrade the entire project to AndroidX.

 
Migrate to AndroidX.png

So the next modification is manually after the automatic upgrade modifications.

The end of the article is a reference to Google's official explanation, you can view more detailed guidelines and instructions inside Migrating to AndroidX .

This article directory:

1. Manually configured gradle.properties
2.app/build.gradle modify
3.java file modification
4. Modify layout file xml
The modified section view unavailable
6. Other issues
7.gradle command help identify the problem
8. Reference article

1. The manual configuration gradle.properties

android.enableJetifier=true
android.useAndroidX=true

2.app/build.gradle modification

Before the amendment android.support After modifying androidx
compile implementation
testCompile testImplementation
debugCompile debugImplementation
releaseCompile releaseImplementation

Of course, this is because some of the major update after update prompt gradle needed.
I did not write the whole, just write the main, Android Studio prompted, follow the prompts to update the line.

android{
        //省略。。。。
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8 } } 

3.java file modification

Here mainly refers to the import of modified, Android Studio project automatically upgraded to androidx help, a lot of control in the androidx path are changed, it is necessary to replace a key global search.

Global Replace: Edit -> Find -> Replace in path

Lists some commonly used:

Before the amendment android.support After modifying androidx
import androidx.appcompat.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.core.view.ViewPager; import androidx.viewpager.widget.ViewPager;
import androidx.core.view.PagerAdapter; import androidx.viewpager.widget.PagerAdapter;
import androidx.core.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.core.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.core.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
import androidx.core.content.LocalBroadcastManager; androidx.localbroadcastmanager.content.LocalBroadcastManager import;
import androidx.appcompat.widget.DefaultItemAnimator; import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.appcompat.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.appcompat.widget.GridLayoutManager; import androidx.recyclerview.widget.GridLayoutManager;
import androidx.appcompat.widget.StaggeredGridLayoutManager; import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.support.design.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
import android.support.design.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
import android.support.design.widget.TabLayout; import com.google.android.material.tabs.TabLayout;
import android.support.design.widget.AppBarLayout; import com.google.android.material.appbar.AppBarLayout

When the entire project because of Azolla file when R is not generated, a global search and replace these common controls would be more convenient.

4. Modify layout file xml

In 3.java file modification listed some control, is also a global search controls corresponding to these modified androidx just fine.

To name a few:

修改前android.support 修改后androidx
<android.support.v4.widget.NestedScrollView/> <androidx.core.widget.NestedScrollView/>
<android.support.v4.widget.Space/> <Space/>
<android.support.v7.widget.AppCompatTextView/> <TextView/>
<androidx.appcompat.widget.CardView/> <androidx.cardview.widget.CardView/>

注意!!!

不管是java还是xml也好,在Android Studio全局搜索的情况下还是有可能搜漏,所以可能还是需要挨个文件查看替换。

5.部分view不可用修改

之前手动拷贝了support包中的TabItem(TabLayout需要使用的一个类)出来使用,在升级到androidx后发现无法使用了。

support中的代码:

    public TabItem(Context context, AttributeSet attrs) { super(context, attrs); final TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs, android.support.design.R.styleable.TabItem); mText = a.getText(android.support.design.R.styleable.TabItem_android_text); mIcon = a.getDrawable(android.support.design.R.styleable.TabItem_android_icon); mCustomLayout = a.getResourceId(android.support.design.R.styleable.TabItem_android_layout, 0); a.recycle(); } 

a.getText()、a.getDrawable()、a.getResourceId()都报错,提示TintTypedArray.getText()或TintTypedArray.getDrawable()或TintTypedArray.getResourceId()只能在同一个library group(groupId=androidx.appcompat)中被调用,

从TintTypedArray的源码可知,是在源码中做了限制的原因:

@RestrictTo(LIBRARY_GROUP)
public class TintTypedArray {} 

6.其他问题

app module relies on some of the library and I upgraded to androidx, attention in the library of build.gradle modified to implementation in the compile time, if there needs to lean on the global use, then we would use the api key a.

such as:

修改前:
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar']) } 修改后: dependencies { api fileTree(dir: 'libs', include: ['*.jar']) } 

7.gradle command help identify the problem

When I compile the project again and again, and always find a java file or xml file went wrong, logcat, message is not an error, this time the most unhappy.

Until I found a big kill: gradle command

Use the following command in the terminal path of the project to build the project:

./gradlew compileDebugJavaWithJavac



Author: Zhang sunny every day, every day,
link: https: //www.jianshu.com/p/b0800f590e6e
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/jiangzuomeng/p/11950007.html