【问题标题】:Flutter - not able to run in android deviceFlutter - 无法在android设备中运行
【发布时间】:2020-11-29 04:29:17
【问题描述】:

您好,我在没有在 android 设备中调试我的项目的情况下运行时遇到此错误。

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Could not resolve all artifacts for configuration ':app:debugCompileClasspath'.
   > Failed to transform artifact 'arm64_v8a_debug.jar (io.flutter:arm64_v8a_debug:1.0.0-c8e3b9485386425213e2973126d6f57e7ed83c54)' to match attributes 
{artifactType=android-classes, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.
      > Execution failed for IdentityTransform: C:\Users\Admin\.gradle\caches\modules-2\files-2.1\io.flutter\arm64_v8a_debug\1.0.0-c8e3b9485386425213e2973126d6f57e7ed83c54\d84df37647d9c0eb0da1f86ba05744d618192eb7\arm64_v8a_debug-1.0.0-c8e3b9485386425213e2973126d6f57e7ed83c54.jar.
         > Cannot parse result path string:     

* 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.    

* Get more help at https://help.gradle.org

BUILD FAILED in 40s
Running Gradle task 'assembleDebug'...
Running Gradle task 'assembleDebug'... Done                        42.5s
Exception: Gradle task assembleDebug failed with exit code 1

我在代码中使用了连接插件,当我尝试运行 flutter build apk 时出现错误消息

Note: C:\Users\Admin\AppData\Roaming\Pub\Cache\hosted\pub.dartlang.org\connectivity-0.4.9\android\src\main\java\io\flutter\plugins\connectivity\ConnectivityBroadcastReceiver.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

有人可以帮我解决这个问题吗?提前致谢。

更新:我可以在三星 j7 中运行它,但在三星 a7 中出现上述错误

我用来检查连接性的代码

import 'package:connectivity/connectivity.dart';
import 'package:flutter/material.dart';
import 'dart:io';
class NetworkConnection{
  Future<String> checkInternetConnectivity(BuildContext context) async
  {
    var result = await Connectivity().checkConnectivity();
    String connection = "unknown";
    if(result == ConnectivityResult.none)
    {    
      //_showToast(context,"No Connectivity");
      connection =  "not connected";
    }
    else if(result == ConnectivityResult.mobile)
    {
      try {
            final result = await InternetAddress.lookup('google.com');
            if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
              connection =  "connected";
            }
          } on SocketException catch (_) {
            connection =  "not connected";
          }

      //_showToast(context,"Connected to mobile");

    }
    else if(result == ConnectivityResult.wifi)
    {
      try {
            final result = await InternetAddress.lookup('google.com');
            if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
              connection = "connected";
            }
          } on SocketException catch (_) {
            connection =  "not connected";
          }
      //_showToast(context,"Connected to wifi");
    
    }
    return connection;
  }
}

我的颤振医生-v是这样的

[√] Flutter (Channel stable, 1.20.2, on Microsoft Windows [Version 10.0.18362.1016], locale en-IN)
    • Flutter version 1.20.2 at C:\src\flutter
    • Framework revision bbfbf1770c (7 days ago), 2020-08-13 08:33:09 -0700
    • Engine revision 9d5b21729f
    • Dart version 2.9.1

 
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at C:\Users\Admin\AppData\Local\Android\Sdk
    • Platform android-29, build-tools 29.0.3
    • ANDROID_HOME = C:\Users\Admin\AppData\Local\Android\Sdk
    • Java binary at: C:\Program Files\Android\Android Studio1\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[√] Android Studio (version 4.0)
    • Android Studio at C:\Program Files\Android\Android Studio1
    • Flutter plugin version 48.0.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] IntelliJ IDEA Community Edition (version 2019.3)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3.4
    • Flutter plugin version 45.1.2
    • Dart plugin version 193.6911.31

[√] VS Code (version 1.48.0)
    • VS Code at C:\Users\Admin\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.13.2

[√] Connected device (1 available)
    • SM A750F (mobile) • 32008575eabc85fb • android-arm64 • Android 10 (API 29)

这是我的 build.gradle 文件

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 29

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.sfav1"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

【问题讨论】:

  • 你能试试flutter clean吗?
  • 您使用的是最新的软件包版本connectivity: ^0.4.9 吗?如果是并且问题仍然存在,请提供您正在使用此包的代码行。
  • 你的flutter doctor看起来怎么样?
  • 重构点:将 try 块包装到一个方法中,并在两个 "else if" 上调用它。

标签: android flutter


【解决方案1】:

您似乎没有在AndroidManifest.xml 中设置权限Internet

看看这个Issue

【讨论】:

  • 我添加了权限,仍然面临同样的问题。
  • 我将颤振更新到新的稳定版本是我遇到这个问题的原因吗? @frankenapps
【解决方案2】:

尝试将这些设置添加到您的defaultConfigandroid/app/build.gradle

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndk {
    abiFilters 'x86', 'armeabi-v7a','arm64-v8a', 'x86_64'
} 

例如:

defaultConfig {
    applicationId "com.package.name"
    minSdkVersion 16
    targetSdkVersion 28
    versionCode 1
    versionName "0.0.1"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    ndk {
        abiFilters 'x86', 'armeabi-v7a','arm64-v8a', 'x86_64'
    }
}

【讨论】:

  • 我添加了您的代码,但仍然收到此警告 注意:C:\Users\Admin\AppData\Roaming\Pub\Cache\hosted\pub.dartlang.org\connectivity-0.4.9\ android\src\main\java\io\flutter\plugins\connectivity\ConnectivityBroadcastReceiver.java 使用或覆盖已弃用的 API。注意:使用 -Xlint:deprecation 重新编译以获取详细信息。
  • @Anu 你能在你的三星 a7 上运行吗?这只是一个警告,要解决这个问题,您需要在项目的 github 上创建一个问题并等待,或者自己提出一个拉取请求来解决这个问题。但我认为这个警告根本不会导致您的问题。
  • 是的,警告不应该阻止应用程序编译。查看我的回答了解更多详情
  • @Vitor 在将我的颤振版本从 1.20.1 升级到 1.20.2 后,我可以运行了。在 1.20.1 中,我无法进行构建。但现在它的工作。您能否在答案中添加一些细节。对所有人都有帮助!
  • @ByteMe 是的,这应该不是问题,但是当我为目标平台使用拆分 apk 时,我的应用程序崩溃了。虽然我在 1.20.1 中使用了 apk,但即使我连接到互联网也没有检测到网络。这就是为什么我担心这个警告!
【解决方案3】:

您的三星 A7 似乎是 Android 10 (API 29)。但是,flutter 自动生成的build.gradle 并不针对 API 29。

您可以通过更新 android\app\build.gradle 文件中的以下行来更改此设置。

更新文件后运行flutter run

android {
    compileSdkVersion 29  //change compileSdkVersion to 29

    defaultConfig {
        applicationId "com.example.new_app"
        minSdkVersion 21
        targetSdkVersion 29  //this line: change 28 to 29
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

}

至于弃用警告,显然是颤振团队开发的一些插件中的warning is common,但它应该不会影响你的颤振应用程序的编译。

【讨论】:

    【解决方案4】:

    检查您的 minSdkVersiontargetSdkVersion 根据您在项目中使用的 Packages 即在 pubspec.yaml > 文件。

    defaultConfig {
        .
        .
        .      
        minSdkVersion 16
        targetSdkVersion 28
        .
        .
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      • 2020-10-01
      • 2023-02-04
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多