flutter异常分析

1、插件shared_preferences找不到gradle,详细日志如下:

* What went wrong:
A problem occurred configuring project ':shared_preferences'.
> Could not resolve all artifacts for configuration ':shared_preferences:classpath'.
   > Could not resolve com.android.tools.build:gradle:3.4.0.
     Required by:
         project :shared_preferences
      > Could not resolve com.android.tools.build:gradle:3.4.0.
         > Could not get resource 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.4.0/gradle-3.4.0.pom'.
            > Could not GET 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.4.0/gradle-3.4.0.pom'.
               > Connect to 127.0.0.1:1087 [/127.0.0.1] failed: Connection refused: connect
      > Could not resolve com.android.tools.build:gradle:3.4.0.
         > Could not get resource 'https://jcenter.bintray.com/com/android/tools/build/gradle/3.4.0/gradle-3.4.0.pom'.
            > Could not GET 'https://jcenter.bintray.com/com/android/tools/build/gradle/3.4.0/gradle-3.4.0.pom'.
               > Connect to 127.0.0.1:1087 [/127.0.0.1] failed: Connection refused: connect
> Failed to notify project evaluation listener.
   > Could not get unknown property 'android' for project ':shared_preferences' of type org.gradle.api.Project.
   > Could not find method implementation() for arguments [project ':shared_preferences_macos'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
   > Could not find method implementation() for arguments [project ':shared_preferences_web'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

分析,这里shared_preferences这个第三方插件,找不到Android gradle插件(注意这个是Android gradle插件版本,不是gradle版本),但是在Android根目录下的build.gradle文件中配置如下:

classpath 'com.android.tools.build:gradle:3.5.0'

可以发现shared_preferences用的Android gradle插件版本与项目中配置的版本不一致,怀疑原因就是这里,那要如何修改呢?
参考这里https://stackoverflow.com/questions/59468295/failure-build-failed-with-an-exception-shared-preference-and-gradle-wrapper-pr
Android Gradle 插件版本说明
翻译过来,在

flutter SDK目录\.pub-cache\hosted\pub.flutter-io.cn\shared_preferences-0.5.6+2\android\build.gradle

注意这里pub.flutter-io.cn目录名称根据你配置的maven仓库,找到相应的一个:
flutter异常分析_第1张图片
用文本编辑器打开build.gradle文件,发现shared_preferencesd的build.gradle如下

classpath 'com.android.tools.build:gradle:3.4.0'

这里我们把Android gradle插件版本改成与项目一致的3.5.0即可。

2、还是build.gradle的异常

Build file 'E:\workspaceTest\flutter_app\android\app\build.gradle' line: 26

* What went wrong:
A problem occurred evaluating project ':app'.
> Could not resolve all artifacts for configuration 'classpath'.
   > Could not find com.android.tools.build:gradle:3.6.1.
     Searched in the following locations:
       - https://maven.aliyun.com/repository/jcenter/com/android/tools/build/gradle/3.6.1/gradle-3.6.1.pom
       - https://maven.aliyun.com/repository/jcenter/com/android/tools/build/gradle/3.6.1/gradle-3.6.1.jar
       - http://maven.aliyun.com/nexus/content/groups/public/com/android/tools/build/gradle/3.6.1/gradle-3.6.1.pom
       - http://maven.aliyun.com/nexus/content/groups/public/com/android/tools/build/gradle/3.6.1/gradle-3.6.1.jar
     Required by:
         unspecified:unspecified:unspecified

分析错误,根据第一行的错误日志找到 项目根路径\android\app\build.gradle的第26行:

 apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

然后这个$flutterRoot定义:

def flutterRoot = localProperties.getProperty('flutter.sdk')

调整即可。

3、android SDK的异常

Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
> Failed to install the following Android SDK packages as some licences have not been accepted.
     build-tools;28.0.3 Android SDK Build-Tools 28.0.3
     platforms;android-28 Android SDK Platform 28
  To build this project, accept the SDK license agreements and install the missing components using the Android Studio SDK Manager.
  Alternatively, to transfer the license agreements from one workstation to another, see http://d.android.com/r/studio-ui/export-licenses.html

说是 Android SDK 的licences 没有被接受,这就很奇怪了,cmd到sdk目录下,运行

sdkmanager.bat --licenses

发现全部授权了。然后在项目的Android目录下打开local.properties文件,发现

sdk.dir=C:\\Users\\Administrator\\AppData\\Local\\Android\\sdk

这个sdk的目录并不是Android studio中配置的目录,而且修改了local.properties无用,因为local.properties文件是flutter自动生成的,每次重新编译都会重新生成。于是找到C:\Users\Administrator\AppData\Local\Android\sdk这个目录的sdk,cmd到sdk目录下,运行

sdkmanager.bat --licenses

全部选择y就行了。

4、网络请求异常
在模拟器中调用接口出现异常:

Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 48784

把localhost换成127.0.0.1,还是出现问题,猜测模拟器不和电脑处在同一网络,cmd中调用ipconfig,查出局域网ip地址为192.168.1.6,替换即可。

5、打包异常

Execution failed for task ':shared_preferences:verifyReleaseResources'.

这方面的错误的原因是: 第三方库的 SDK 与android 下的 build.gradle 不统一
解决这个问题就需要:将报错第三方库的node包下的Android下的 build.gradle文件 SDK 与android 下的 build.gradle 进行统一。
参考:Execution failed for task ‘:xxxxxx:verifyReleaseResources’
比如我这里是shared_preferences这个库有问题,目录下找到该第三方库的 build.gradle 文件,把他的compileSdkVersion改成和项目一致即可。
flutter异常分析_第2张图片
6、正式打包后不能访问网络
在Android的application中加入:

    
    
    

因为flutter并没有帮我们申请权限,所以需要我们自己在Android中申明。

7、Android studio上run安装异常

Gradle build failed to produce an .apk file. It's likely that this file was generated under 

说是找不到安装包,我猜是我在build.gradle修改报名导致的:

  applicationVariants.all { variant ->
      variant.outputs.all { output ->
          def fileName = "wool" +
                  "_${flutterVersionName}" +
                  "_${flutterVersionCode}" +
                  "_${variant.buildType.name}" +
                  new Date().format("yyyyMMddHHmmss") +
                  ".apk"
          outputFileName = fileName
      }
  }

果然把这句话注释就好了。

你可能感兴趣的:(跨平台)