Flutter - 无法在安卓设备上运行

7

嗨,当我在安卓设备上无调试运行我的项目时,遇到了以下错误。

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;
  }
}

我的flutter doctor -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'
}

1
你可以尝试执行 flutter clean 命令吗? - Sanjay Sharma
1
你看到这个了吗?https://stackoverflow.com/questions/61012389/flutter-app-is-running-on-android-emulator-but-not-on-physical-device-oneplus-7 - kuhnroyal
1
重构建议:将try块封装成一个方法,并在两个“else if”中调用它。 - Watth
1
从现在的情况来看,这似乎不是与连接插件有关的问题。 - ByteMe
1
@Anu:这只是会提高代码结构和可读性。 - Watth
显示剩余9条评论
4个回答

1

看起来你没有在 AndroidManifest.xml 中设置 Internet 权限。

请参考此 问题


我已经添加了权限,但仍然面临相同的问题。 - Anu
我更新了Flutter到新的稳定版本,这是我遇到问题的原因吗?@frankenapps - Anu

1
尝试将以下设置添加到 android/app/build.gradledefaultConfig 中。
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndk {
    abiFilters 'x86', 'armeabi-v7a','arm64-v8a', 'x86_64'
} 

e.g.:

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
1
@Anu 你能在你的三星A7上运行吗?这只是一个警告,要解决这个问题,你需要在项目的Github上创建一个问题并等待,或者自己修复该问题并创建一个拉取请求。但我不认为这个警告会导致你的问题。 - Vitor
1
是的,警告不应该阻止应用程序编译。请查看我的答案以获取更多详细信息。 - ByteMe
@Vitor 我在将我的 Flutter 版本从1.20.1 升级到1.20.2 后可以运行了。在 1.20.1 中我无法构建应用程序,但现在可以了。你能否在回答中添加一些细节?这会对大家有所帮助! - Anu
@ByteMe 是的,这应该不是问题,但当我为目标平台获取分割 APK 时,我的应用程序会崩溃。当我在 1.20.1 中获取 APK 时,即使我连接到互联网,也无法检测到网络。这就是为什么我对这个警告感到担忧! - Anu

1
似乎您的三星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
    }

}

关于弃用警告,显然警告在一些由Flutter团队开发的插件中很常见,但它不应该影响您的Flutter应用程序的编译。

0
请注意您在项目中使用的Packages,并根据此检查您的minSdkVersiontargetSdkVersion,例如在pubspec.yaml文件中。
例如:
defaultConfig {
    .
    .
    .      
    minSdkVersion 16
    targetSdkVersion 28
    .
    .
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接