3

I wanted to make some operations while the app was not running, so I decided to use the background fetch package.I followed the android setup step by step This is part of my pubspec.yaml

version: 1.0.0+1

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter


  google_maps_flutter: ^0.5.16
  geolocator: ^5.1.1
  flutter_local_notifications: ^0.7.1+1
  shared_preferences: '0.5.6+3'
  background_fetch: '^0.5.1'

This is part of my AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="firenzepuliziastrade.firenze_pulizia_strade">
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <application
        tools:replace="android:label"
        android:name="io.flutter.app.FlutterApplication"
        android:label="Firenze Pulizia Strade"
        android:icon="@mipmap/ic_launcher">

This is part of my app/build.gradle

android {
    compileSdkVersion rootProject.ext.compileSdkVersion


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

}


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

This is part of my android/build.gradle

buildscript {
    ext.kotlin_version = '1.3.50'
    ext {
        compileSdkVersion   = 28                // or higher
        targetSdkVersion    = 28                // or higher
        appCompatVersion    = "1.1.0"           // or higher
    }

    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            // [required] background_fetch
            url "${project(':background_fetch').projectDir}/libs"
        }
    }
}

This is my gradle.properties

org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true

I tried even the additional step to enable the Headless mechanism even though I thought that it was not necessary since my flutter version is Flutter 1.12.13 and I created this project recently. But all that said I could not make the app do some work while not running, it just worked while being in the background. So I thought that the reason was that I was launching the app in debug mode and I tried to launch it in release mode, but I got the following error:

Launching lib/main.dart on POT LX1 in release mode...
Running Gradle task 'assembleRelease'...

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':path_provider:verifyReleaseResources'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
   > Android resource linking failed
     /home/nik/.gradle/caches/transforms-2/files-2.1/0a271e99b6771ad4a84318244d532fb7/core-1.0.0/res/values/values.xml:57:5-88:25: AAPT: error: resource android:attr/fontVariationSettings not found.

     /home/nik/.gradle/caches/transforms-2/files-2.1/0a271e99b6771ad4a84318244d532fb7/core-1.0.0/res/values/values.xml:57:5-88:25: AAPT: error: resource android:attr/ttcIndex not found.


* 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 7s
The built failed likely due to AndroidX incompatibilities in a plugin. The tool is about to try using Jetfier to solve the incompatibility.
Building plugin background_fetch...
Running Gradle task 'assembleAarRelease'...
Finished with error: ProcessException: Permission denied
  Command: /home/nik/Desktop/Pulizia_Strade/flutter/.pub-cache/hosted/pub.dartlang.org/background_fetch-0.5.6/android/gradlew -I=/home/nik/Desktop/Pulizia_Strade/flutter/packages/flutter_tools/gradle/aar_init_script.gradle -Pflutter-root=/home/nik/Desktop/Pulizia_Strade/flutter -Poutput-dir=/home/nik/Desktop/Pulizia_Strade/firenze_pulizia_strade/build/app -Pis-plugin=true -Ptarget-platform=android-arm,android-arm64,android-x64 assembleAarRelease

I tried to create a new flutter project, specifying androidx, but it did notwork, I got the same error. Can someone help me, I really do not know what to do at this point...

Niccolò Simoni
  • 95
  • 1
  • 2
  • 8

1 Answers1

0

It seems that your app is not compatible with AndroidX. So far you've done some troubleshooting but didn't get to make it work. Try to double check the AndroidX Migration Guide if you haven't missed anything.

So far, you've already enabled the Jetifier by adding the following lines to [project_folder]/android/app/gradle.properties

android.useAndroidX=true
android.enableJetifier=true

Now try to enable multidex by adding this line in the defaultConfig of [project_folder]/app/build.gradle

defaultConfig {
    ...
    multiDexEnabled true
}

Then upgrade gradle dependency in the [project_folder]/android/build.gradle file:

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

Latest version used in flutter as specified here.

Next, make sure that all your packages in the pubspec.yaml are of the latest version.

Lastly, run flutter clean and rebuild your project.

MαπμQμαπkγVπ.0
  • 5,887
  • 1
  • 27
  • 65