韌館-LearnHouse

[Android]AOSP升級android.support到androidx

Android 9之後已經啟動androidx的library,也就是將來support library將不再maintain了

由於google官網只提供Android studio的升級步驟,對於開發AOSP寫Android.mk的卻都沒有著墨

參考了google在AOSP上的其它APP,整理測試後可依下列步驟修改來進行升級到androidx


1. 修改Android.mk, 註1
新增

//使用androidx lib 
LOCAL_USE_AAPT2 := true 

//APP使用了哪個lib就置換成相對應的 
LOCAL_STATIC_JAVA_LIBRARIES := \ 
       androidx.appcompat_appcompat \ (修改前:android-support-v7-appcompat) 
       androidx.cardview_cardview \ (修改前:android-support-v7-cardview)

2. 替換所有的java檔的import, 註2

 import androidx.appcompat.app.AlertDialog; //(修改前: import android.support.v7.app.AlertDialog) 

3. 若遇到thrid-party的jar檔,也須同步更新

4.若遇到下列錯誤訊息需修改proguard.cfg

Warning: android.support.v4.media.SessionToken2ImplLegacy: can't find referenced method 'android.support.v4.media.session.MediaSessionCompat$Token fromBundle(android.os.Bundle)' in program class android.support.v4.media.session.MediaSessionCompat$Token
Warning: there were 6 unresolved references to program class members.
Your input classes appear to be inconsistent.
You may need to recompile the code.
(http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedprogramclassmember)
Error: Please correct the above warnings first.

在最下方加入

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**

註1: 參考官網Artifact Mappings置換,但須注意官網列的是studio的作法,這裡『:』須改成『_』
註2: 參考官網Class Mappings修改所有的import

2020年1 月 posted by admin in 程式&軟體 and have No Comments

Place your comment

Please fill your data and comment below.
名稱:
信箱:
網站:
您的評論: