最近一直在做安卓开发,奈何手中的资料比较老,资料上的一些import经常没有,十分让人头疼。
感谢简书上的【张晴天天天天天】做的整理。
在这里也记录一下备用。
升级AndroID Studio后,在 Refactor ---> Migrate to AndroIDX 路径下可以将整个项目升级到AndroIDX。
Migrate to AndroIDX.png所以接下来的修改也是在自动升级后的手动修改。
文末参考文章是Google官方的说明,可以查看里面更详细的指引和说明Migrating to AndroidX。
本文目录:1.手动配置gradle.propertIEs
2.app/build.gradle修改
3.java文件修改
4.布局文件xml修改
5.部分vIEw不可用修改
6.其他问题
7.gradle命令帮助查找问题
8.参考文章
androID.enableJetifIEr=trueandroID.useandroidX=true
2.app/build.gradle修改修改前androID.support | 修改后androIDx |
---|---|
compile | implementation |
testCompile | testImplementation |
deBUGCompile | deBUGImplementation |
releaseCompile | releaseImplementation |
当然,这部分主要是因为gradle升级后提示需要的更新。
没写全,只写了主要的,AndroID Studio有提示,根据提示更新就行。
androID{ //省略。。。。 compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }}
3.java文件修改这里主要指import的修改,AndroID Studio自动帮助项目升级成androIDx时,很多控件的路径在androIDx中都变更了,所以需要全局搜索一键替换。
全局替换: Edit ->Find -> Replace in path
列举了一些常用的:
修改前androID.support | 修改后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; | import androIDx.localbroadcastmanager.content.LocalbroadcastManager; |
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 |
当整个项目因为未生成R文件满江红的时候,全局搜索替换这些常用控件会比较方便。
4.布局文件xml修改在3.java文件修改中罗列了一些控件,同样也是全局搜索把这些对应的控件修改成androIDx的就好。
列举几个:
修改前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所依赖的一些library我也升级到了androIDx,在将library中的build.gradle里的compile修改成implementation的时候注意,如果这里依赖的对象需要全局使用的话,就要使用API关键字了。
比如:
修改前:dependencIEs { compile filetree(dir: 'libs', include: ['*.jar'])}修改后:dependencIEs { API filetree(dir: 'libs', include: ['*.jar'])}
7.gradle命令帮助查找问题在我一次又一次编译项目时,始终找不到是java文件或者xml文件哪里出错了,logcat、message也不报错,这种时候是最愁的。
直到我找到了大杀器:gradle命令
在terminal本项目路径下使用下面这条命令来编译项目:
./gradlew compileDeBUGJavaWithJavac
作者:张晴天天天天天
链接:https://www.jianshu.com/p/b0800f590e6e
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 总结
以上是内存溢出为你收集整理的android开发导包升级到androidx踩坑记录【转载】全部内容,希望文章能够帮你解决android开发导包升级到androidx踩坑记录【转载】所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)