Flutter - not able to run in android device

Viewed 2104

Hi I am getting this error while run without debugging my project in android device.

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

I used connectivity plugin in my code, and error message says that, when I trying to run 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.

Can someone help me with this? Thanks in advance.

Update : I am able to run it in samsung j7, but I am getting the above error in samsung a7

Code I used to check connectivity

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

My flutter doctor -v looks like this

[√] 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)

This is my build.gradle file

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'
}
4 Answers

It looks like you did not set the Permission Internet in the AndroidManifest.xml.

Look into this Issue.

Try to add these settings to your defaultConfig on android/app/build.gradle

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

It seems your Samsung A7 is an Android 10 (API 29). Howeever, the build.gradle automatically generated by flutter doesn't target API 29.

You can change this by updating the following line in your android\app\build.gradle file.

Run flutter run after updating the file

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
    }

}

As for the deprecation warning, apparently the warning is common in some plugins developed by the flutter team but it shouldn't affect the compiling of your flutter app.

Check out your minSdkVersion and targetSdkVersion as per you have used Packages in your project i.e. in pubspec.yaml file.

i.e.

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