Android 之 AndroidX 庫

按照官方文檔說明 AndroidX 是對 android.support.xxx 包的整理後產物。由於之前的 support 包過於混亂,所以,Google 推出了AndroidX。

由於在後續版本中,會逐步放棄對 support 的升級和維護,所以,我們必須遷移到 AndroidX。

1.在項目的gradle.properties中添加

android.enableD8=true
android.enableR8=true
android.enableR8.libraries=true

2. 

  • 在項目 app下的build.gradle中   

        compileSdkVersion 28  必須 sdk版本大於等於28  

  • 在項目 下的build.gradle中   

      

       gradle版本 最好是3.0以上版本。

  • 在 AndroidStudio 3.2 或更高版本中執行如下操作:Refactor > Migrate to AndroidX 

       但是不一定能完全轉換成功。(也可能造成項目報錯,需提前備份)

      注意: 上面一步 可以通過 手動修改來完成,耗時會比較長。如下

       在項目 build.gradle中  

    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    testCompile 'junit:junit:4.12'

      替換成

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'

3.手動修改編譯錯誤的地方。

通常有:

import android.support.annotation.NonNull;

import android.support.v4.app.ActivityCompat;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentTransaction;

import android.support.v4.content.ContextCompat;

import android.app.AlertDialog;

特別注意 xml中

 <android.support.v4.view.ViewPager
        android:id="@+id/id_page_vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/ll_tab_title" >
    </android.support.v4.view.ViewPager>

改成  :androidx.viewpager.widget.ViewPager

    <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="cn.hand.tech.provider38"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_provider" />
        </provider>

改成:androidx.core.content.FileProvider

如果有 DataBindingUtil 的使用; 即使api修改成androidX後也可能會報錯,這時候需要File---Invalidate Caches/Restart清除緩存 重啓一下AndroidStudio 。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章