Flutter 存量Android接入Flutter躺坑指南

有思想,也有忧伤和理想,这才是生活。
人生没有如果,只有后果和结果。

前言

最近给一个老项目接入Flutter,遇到了一些问题,记录下。

存量Android接入Flutter官方文档:Integrate a Flutter module into your Android project

踩坑的本机环境

注意对比下我的环境和你的环境是否一样,有些问题在Flutter的新版本中已经被修复了。

  • Flutter环境安装教程:起步:安装Flutter
➜  app git:(master) ✗ flutter --version
Flutter 1.9.1+hotfix.6 • channel stable • https://github.com/flutter/flutter.git
Framework • revision cc949a8e8b (3 weeks ago) • 2019-09-27 15:04:59 -0700
Engine • revision b863200c37
Tools • Dart 2.5.0

Flutter doctor -v

➜  app git:(master) ✗ flutter doctor -v                  
[] Flutter (Channel stable, v1.9.1+hotfix.4, on Mac OS X 10.15 19A583, locale en-CN)
    • Flutter version 1.9.1+hotfix.4 at /Users/.../flutter
    • Framework revision cc949a8e8b (3 weeks ago), 2019-09-27 15:04:59 -0700
    • Engine revision b863200c37
    • Dart version 2.5.0

 
[] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/notzuonotdied/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
    • All Android licenses accepted.

[] Xcode - develop for iOS and macOS (Xcode 11.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.1, Build version 11A1027
    • CocoaPods version 1.8.3

[] Android Studio (version 3.5)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 40.1.2
    • Dart plugin version 191.8423
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[!] IntelliJ IDEA Ultimate Edition (version 2019.2.3)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
    • For information about installing plugins, see
      https://flutter.dev/intellij-setup/#installing-the-plugins

[] VS Code (version 1.39.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.5.1

[] Connected device (1 available)
    • iPhone 11 Pro Max • 61AA9D21-B994-4FC5-9164-757EABF90190 • ios • com.apple.CoreSimulator.SimRuntime.iOS-13-1 (simulator)

! Doctor found issues in 1 category.

错误

升级JDK1.8

项目中接入了Tinker,且JDK版本为1.7。

接入Flutter需要升级项目JDK版本为1.8。

Java 8 language support, as requested by 'android.enableD8.desugaring= true' in your gradle.properties file, is not supported when 'android.useDexArchive= false'.

需要在项目根目录下的gradle.properties中增加以下的代码:

android.useDexArchive = true
# Java8语法脱糖
android.enableD8.desugaring = true
  • 最新版本不支持java 8了吗 #67,介绍了解决方法。
  • Java 8 language support, as requested by ‘android.enableD8.desugaring= true’ in your gradle.properties file, is not supported when ‘android.useDexArchive= false’.

混淆带来的问题

Warning: io.flutter.embedding.android.FlutterView: can't find referenced method 'android.graphics.Insets getSystemGestureInsets()' in library class android.view.WindowInsets
Warning: io.flutter.embedding.android.FlutterView: can't find referenced class android.graphics.Insets
Warning: io.flutter.embedding.android.FlutterView: can't find referenced class android.graphics.Insets
Warning: io.flutter.embedding.android.FlutterView: can't find referenced class android.graphics.Insets
Warning: io.flutter.embedding.android.FlutterView: can't find referenced class android.graphics.Insets
Warning: io.flutter.embedding.android.FlutterView: can't find referenced class android.graphics.Insets
Warning: io.flutter.embedding.android.FlutterView: can't find referenced class android.graphics.Insets
Warning: io.flutter.view.FlutterView: can't find referenced method 'android.graphics.Insets getSystemGestureInsets()' in library class android.view.WindowInsets
Warning: io.flutter.view.FlutterView: can't find referenced class android.graphics.Insets
Warning: io.flutter.view.FlutterView: can't find referenced class android.graphics.Insets
Warning: io.flutter.view.FlutterView: can't find referenced class android.graphics.Insets
Warning: io.flutter.view.FlutterView: can't find referenced class android.graphics.Insets
Warning: io.flutter.view.FlutterView: can't find referenced class android.graphics.Insets
Warning: io.flutter.view.FlutterView: can't find referenced class android.graphics.Insets

proguard中增加-dontwarn android.**即可解决。

  • Proguard error after upgrating stable to v1.9.1+hotfix.2 #40168
  • After upgrading to 1.9 duplicate class and reference warnings throw error when building apk #40364

minSdkVersion

有些老项目支持到了14,所以需要做下处理。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="包名">

    
    <uses-sdk tools:overrideLibrary="com.example.xxx" />
manifest>

  • uses-sdk:minSdkVersion 15 cannot be smaller than version 16 declared in library
  • Manifest merger failed : uses-sdk:minSdkVersion 14 cannot be smaller than version 16 declared in lib…
  • Manifest merger failed : uses-sdk:minSdkVersion 14 cannot be smaller than version 16 declared in library

EnclosingMember annotations

问题:Android studio warning - InnerClass annotations are missing corresponding EnclosingMember annotations

增加如下内容:

-keepattributes *Annotation*
-keep @**annotation** class * {*;} 

附录

D8相关

  • Android Studio 3.0+ 新Dex编译器D8 Desugar R8
  • Error:Java 8 language support, as requested by ‘android.enableD8.desugaring= true’
  • 最新版本不支持java 8了吗 #67,介绍了解决方法。
  • Java 8 language support, as requested by ‘android.enableD8.desugaring= true’ in your gradle.properties file, is not supported when ‘android.useDexArchive= false’.
  • The option ‘android.useDexArchive’ is deprecated and should not be used anymore. #290

你可能感兴趣的:(Flutter探索之路)