美文网首页Android
Flutter之AndroidX相关问题

Flutter之AndroidX相关问题

作者: 夏_Leon | 来源:发表于2019-03-05 16:12 被阅读0次

    AndroidX适配

    引入一些比较新的第三方库的时候会出现AndroidX适配问题,官方解决的链接是 https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility
    大意就是如果你的项目没有适配AndroidX,就不用使用那些比较新的库,否则会出问题。若要升到AndroidX,并不是简单一键" Migrating to AndroidX"就行了,还要手动修改老的引用。
    升级了一下发现坑还是有点多,又还原回去,后续补充。

    把第三方库降级适配

    编译的时候报错 error: resource android:attr/dialogCornerRadius not found.

      Output:  /Users/youdongzhen/Documents/repository/trackandroid/flutter/flutter_app/build/app/intermediates/incremental/mergeDebugResources/merged.dir/values-v28/values-v28.xml:7: error: resource android:attr/dialogCornerRadius not found.
      /Users/youdongzhen/Documents/repository/trackandroid/flutter/flutter_app/build/app/intermediates/incremental/mergeDebugResources/merged.dir/values-v28/values-v28.xml:11: error: resource android:attr/dialogCornerRadius not found.
      /Users/youdongzhen/Documents/repository/trackandroid/flutter/flutter_app/build/app/intermediates/incremental/mergeDebugResources/merged.dir/values/values.xml:290: error: resource android:attr/fontVariationSettings not found.
      /Users/youdongzhen/Documents/repository/trackandroid/flutter/flutter_app/build/app/intermediates/incremental/mergeDebugResources/merged.dir/values/values.xml:290: error: resource android:attr/ttcIndex not found.
      error: failed linking references.
    

    网上有很多解决方法,这里有个别人整理的:https://blog.csdn.net/weixin_43465451/article/details/83185112

    然而对我来说还是没用,我的问题出在引入amap_base包的时候报错,因为包中使用了AndroidX的库,而我的项目是并不兼容AndroidX库的,需要修改相应代码。
    先用Android项目方式打开我们的flutter项目:



    打开后就是我们比较熟悉的Android模式,引入的第三方库都是以模块形式存在,找到引发问题的amap_base的gradle文件:



    替换里面的appcompat库
    dependencies {
    //    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
        implementation 'com.android.support:appcompat-v7:27.1.1'
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation 'org.jetbrains.kotlin:kotlin-reflect:1.2.71'
        implementation 'com.amap.api:navi-3dmap:6.5.0_3dmap6.5.0'
        implementation 'com.amap.api:search:6.5.0.1'
        implementation 'com.amap.api:location:4.4.0'
        // 需要getter和setter序列化时, 使用fastjson
        implementation 'com.alibaba:fastjson:1.2.54'
        // 其他情况使用gson, fastjson会有默认构造器问题
        implementation 'com.google.code.gson:gson:2.8.5'
    }
    

    替换完之后在该模块下全局搜索AndroidX关键字,像原来的

    import androidx.core.app.ActivityCompat
    

    修改为

    import android.support.v4.app.ActivityCompat
    

    一一手动把这些库给替换了,然后就可以编译成功了,返回flutter项目里也可以正常运行了。

    相关文章

      网友评论

        本文标题:Flutter之AndroidX相关问题

        本文链接:https://www.haomeiwen.com/subject/uhunuqtx.html