Skip to content

flutter build apk fails on an app using Android Gradle plugin 4.0 #58247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MaikuB opened this issue May 29, 2020 · 128 comments · Fixed by #70808 or #71446
Closed

flutter build apk fails on an app using Android Gradle plugin 4.0 #58247

MaikuB opened this issue May 29, 2020 · 128 comments · Fixed by #70808 or #71446
Assignees
Labels
c: crash Stack traces logged to the console c: regression It was better in the past than it is now customer: crowd Affects or could affect many people, though not necessarily a specific customer. dependency: android Android team may need to help us found in release: 1.21 Found to occur in 1.21 has reproducible steps The issue has been confirmed reproducible and is ready to work on P1 High-priority issues at the top of the work list platform-android Android applications specifically t: gradle "flutter build" and "flutter run" on Android tool Affects the "flutter" command-line tool. See also t: labels.

Comments

@MaikuB
Copy link
Contributor

MaikuB commented May 29, 2020

Canonical Issue

https://issuetracker.google.com/issues/141126614

Steps to Reproduce

  1. Run flutter create counter
  2. Update the app to use Android Gradle plugin 4.0.0
  3. Run flutter build apk

Logs

FAILURE: Build failed with an exception.                                
                                                                        
* What went wrong:                                                      
Execution failed for task ':app:lintVitalRelease'.                      
> Could not resolve all artifacts for configuration ':app:debugRuntimeClasspath'.
   > Failed to transform libs.jar to match attributes {artifactType=processed-jar, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.
      > Execution failed for JetifyTransform: /Users/michaelbui/Projects/counter/build/app/intermediates/flutter/debug/libs.jar.
         > Failed to transform '/Users/michaelbui/Projects/counter/build/app/intermediates/flutter/debug/libs.jar' using Jetifier. Reason: FileNotFoundException, message: /Users/michaelbui/Projects/counter/build/app/intermediates/flutter/debug/libs.jar (No such file or directory). (Run with --stacktrace for more details.)
           Please file a bug at http://issuetracker.google.com/issues/new?component=460323.
                                                                        
* 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
[✓] Flutter (Channel stable, v1.17.2, on Mac OS X 10.15.5 19F96, locale en-AU)
    • Flutter version 1.17.2 at /Users/michaelbui/flutter
    • Framework revision 5f21edf8b6 (20 hours ago), 2020-05-28 12:44:12 -0700
    • Engine revision b851c71829
    • Dart version 2.8.3

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at /Users/michaelbui/Library/Android/sdk
    • Platform android-29, build-tools 29.0.3
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.5)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.5, Build version 11E608c
    • CocoaPods version 1.9.1

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 45.1.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.45.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.10.2

Edit: created an issue against the Jetifier component as suggested by Gradle output https://issuetracker.google.com/u/1/issues/157728192

@Zazo032
Copy link
Contributor

Zazo032 commented May 29, 2020

Building apk also crashes for me. However, building appbundle works.
Running with --release also crashes, but if you run --debug and then --release, it does seem to work (weird one). After succesfully running with --release, you'll be able to build apk.

@jason-simmons
Copy link
Member

The Android Gradle LintPerVariantTask now appears to be adding JARs from all build variants to its inputs:
https://android.googlesource.com/platform/tools/base/+/1f9787310d64a66370627a2aaefa1e616565c17d

The libapp.so libraries containing the app's compiled Dart code are packaged in a JAR, and a different JAR is generated for each variant. I'm not sure how dynamically generated per-variant JARs are supposed to be handled if the lint task expects all variants' JARs to be available.

As a hack workaround you could change the lintOptions in app/build.gradle to disable the lint task:

lintOptions {
    checkReleaseBuilds false
}

@blasten

@newbieflutter
Copy link

Hi,
It also fails on my side I tried first --debug then --release it fails. I tried checkReleaseBuilds false but many plugin fails like shared_preferences, maps etc all fail to work. What is the best solution ?

@sgehrman

This comment was marked as abuse.

@Malik056
Copy link

Same here. Recently updated android studio, android sdk, gradle and flutter, And I no longer able to build release apk

@newbieflutter
Copy link

Hi,
Looks like this is a big issue then. I upgrade all too. Any one found any solution?

@Jwiggiff
Copy link

Jwiggiff commented May 31, 2020

I wouldn't consider this a solution, but I've found a workaround. I was having the same issue and tried building --debug and then --release and it still didn't work. However, if you look at the path in your specific error, it should either end with debug/libs.jar or profile/libs.jar. This indicates which apk you need to build first. In OP's case it is debug/libs.jar so building --debug first and then --release worked. In my case, it was profile/libs.jar so building --profile and then --release worked. I'm pretty sure building the --release shouldn't have to access the debug or profile directories though.

TL;DR
If your error says debug/libs.jar, build --debug then --release.
If your error says profile/libs.jar, build --profile then --release.

@Malik056
Copy link

I downgraded the android gradle and gradle version, from Android studio Project Structure. And Clean build my project. now it is working.

@amelnikov78
Copy link

@Jwiggiff

TL;DR
If your error says debug/libs.jar, build --debug then --release.
If your error says profile/libs.jar, build --profile then --release.

That works, thanks!

@VladyslavBondarenko VladyslavBondarenko added customer: crowd Affects or could affect many people, though not necessarily a specific customer. platform-android Android applications specifically c: crash Stack traces logged to the console t: gradle "flutter build" and "flutter run" on Android tool Affects the "flutter" command-line tool. See also t: labels. dependency: android Android team may need to help us P1 High-priority issues at the top of the work list has reproducible steps The issue has been confirmed reproducible and is ready to work on labels Jun 1, 2020
@newbieflutter
Copy link

@Jwiggiff
Yes I compile and install. But many other things dont work example shared preferences, map etc. But they all work perfectly fine in debug mode so meaning there is a bigger bug some where which I think need resolution.

@hqwlkj
Copy link

hqwlkj commented Jun 2, 2020

@Jwiggiff

TL;DR
If your error says debug/libs.jar, build --debug then --release.
If your error says profile/libs.jar, build --profile then --release.

Excellent, thanks!

@ahmaducg
Copy link

ahmaducg commented Jun 2, 2020

I have flavors in my gradle

If you have flavors, you have to build to project for every single flavor 3 times, once for debug and once for release, and once for profile

@newbieflutter
Copy link

@ahmaducg does your method help I dont get it exactly what to do cause now I have even upgrade my android studiio to version 4. What else should I do now down grade it ?

@Malik056
Copy link

Malik056 commented Jun 2, 2020

@newbieflutter Downgrading will help, If any of the above methods don't work. You just need to downgrade the Android Gradle and Gradle version, In Project Structure of your android app.
Screenshot 2020-06-02 at 8 35 56 PM

@chitgoks
Copy link

chitgoks commented Jun 3, 2020

yep downgrading is the solution for now till this gets fixed.

its a bad idea to manually build for each flavor if you say it works. it shouldnt be that way but can be considered a temporary workaround

@MisterJimson
Copy link

The Android Gradle LintPerVariantTask now appears to be adding JARs from all build variants to its inputs:
https://android.googlesource.com/platform/tools/base/+/1f9787310d64a66370627a2aaefa1e616565c17d

The libapp.so libraries containing the app's compiled Dart code are packaged in a JAR, and a different JAR is generated for each variant. I'm not sure how dynamically generated per-variant JARs are supposed to be handled if the lint task expects all variants' JARs to be available.

As a hack workaround you could change the lintOptions in app/build.gradle to disable the lint task:

lintOptions {
    checkReleaseBuilds false
}

This worked perfectly for me

@ahmaducg
Copy link

ahmaducg commented Jun 3, 2020

The Android Gradle LintPerVariantTask now appears to be adding JARs from all build variants to its inputs:
https://android.googlesource.com/platform/tools/base/+/1f9787310d64a66370627a2aaefa1e616565c17d

The libapp.so libraries containing the app's compiled Dart code are packaged in a JAR, and a different JAR is generated for each variant. I'm not sure how dynamically generated per-variant JARs are supposed to be handled if the lint task expects all variants' JARs to be available.

As a hack workaround you could change the lintOptions in app/build.gradle to disable the lint task:

lintOptions {
    checkReleaseBuilds false
}

@blasten

Even if this worked, some plugins will not work because of R8 and minifying

@blasten
Copy link

blasten commented Jun 3, 2020

It appears that task dependencies aren't wired up in Android Gradle plugin 4.0. While this bug is being fixed and changes rolled out, you can either follow the workarounds: #58247 (comment) or #58247 (comment).

Otherwise, continue using the AGP version managed by the Flutter tool as that is the latest version that is known to work well with Flutter.

@MisterJimson
Copy link

Otherwise, continue using the AGP version managed by the Flutter tool as that is the latest version that is known to work well with Flutter.

Can you expand on this? How do we know which AGP version is managed by the Flutter tool?

@blasten
Copy link

blasten commented Jun 3, 2020

The versions that flutter create sets. One way to find that out is deleting <app>/android/BUILD.gradle and <app>/android/gradle/wrapper/gradle-wrapper.properties and then running flutter create . in the <app> directory.

@Abenezerfikadu
Copy link

The Android Gradle LintPerVariantTask now appears to be adding JARs from all build variants to its inputs:
https://android.googlesource.com/platform/tools/base/+/1f9787310d64a66370627a2aaefa1e616565c17d

The libapp.so libraries containing the app's compiled Dart code are packaged in a JAR, and a different JAR is generated for each variant. I'm not sure how dynamically generated per-variant JARs are supposed to be handled if the lint task expects all variants' JARs to be available.

As a hack workaround you could change the lintOptions in app/build.gradle to disable the lint task:

lintOptions {
    checkReleaseBuilds false
}

@blasten

This worked perfectly, thanks

@sigidpam
Copy link

I'm running on debian 9 and the workaround for downgrading to 3.6.3 , then --profile && --release, works for my machine.
thanks heap!

@niktses
Copy link

niktses commented May 6, 2021

This is still not working on flutter 2.0.6.
Many packages dont support both gradle 3.+ and null safety and we can still not build apps on release mode on gradle 4.+.
The only solution is keep everything outdated in order to be able to build release apk with 3.+.
How is this not fixed yet?

@zanderso
Copy link
Member

zanderso commented May 6, 2021

@niktses This issue was filed over a year ago, and is marked closed. If you are still having a problem, please file a new issue with full details and instructions to reproduce it. Thanks!

@niktses
Copy link

niktses commented May 7, 2021

It is marked as closed while it is not fixed and has over a hundred comments. It makes more sense to me to reopen this. And it is a major issue as well. As i see it as of right now no flutter developer can reasonably release their app while having upgraded to flutter 2.0 and gradle 4.0 that has came out a year ago.

@pedromassangocode
Copy link

Hi @niktses
This issue was closed as fixed by #71446. If you are still facing it, please file a new issues with the steps to reproduce the issue.
Thank you

@niktses
Copy link

niktses commented May 7, 2021

This is an issue that has not been fixed for 1.2 year not a new one. This needs to reopen. Me copy pasting the original into a new issue doesn't seem correct.

@zanderso
Copy link
Member

zanderso commented May 7, 2021

@niktses We would like to resolve your issue, but we can't do that without a fairly detailed explanation of what you are doing. See @blasten's comment above in #58247 (comment). The best place to put that information is in a new issue so that it will be visible to the team. Thanks!

@niktses
Copy link

niktses commented May 8, 2021

I really don't get why you don't reopen it. There is not a single word that i would change if i created a new one.

Run flutter create counter
Update the app to use Android Gradle plugin 4.0.0
Run flutter build apk

You can scroll to the top to see more details.

Gradle expects a ../libs.jar for every combination of flavor - buildType (in new project needs 3 - release - debug - profile, in my project needs 48 so the above 'solutions' of running each one once is out of the question especially with dev-op).

This is not a simple issue as it prevents us from releasing an app as the apk is never generated.

@rundun
Copy link

rundun commented May 10, 2021

Hi @zanderso if you review my issue I logged #81974

You will be able to reproduce the issue of Flutter supporting AGP 4.0.1.

I am still debugging and trying to figure it out, but the gist of it all is that Flutter Tools does not have a reference to the gradle setup, Gradle seems to be dependant on what location in the directory you run it from.

The Command that fails :

/home/gotama/Documents/test_project/android/gradlew -Pverbose=true -Ptarget-platform=android-x86
-Ptarget=/home/gotama/Documents/test_project/lib/main.dart
-Pdart-defines=RkxVVFRFUl9XRUJfQVVUT19ERVRFQ1Q9dHJ1ZQ== -Pdart-obfuscation=false -Ptrack-widget-creation=true
-Ptree-shake-icons=false -Pfilesystem-scheme=org-dartlang-root assembleDebug

In my issue I also show the use of the same Flutter Tools command (flutter run -v) where it can only work manually if run in the android directory, I believe at this point there is something wrong with the way the command is executed via the process manager in flutter tools.

Also adding the -p flag to the command and running it manually from the project root directory works.

I have tried to manipulate the command on dev channel and it shows that flutter tools cant even read the gradlew tasks from the android directory and reads from the project directory which has a .gradle folder but it only has the boiler plate tasks in it.

@atleugim
Copy link

atleugim commented Jun 7, 2021

@Jwiggiff

TL;DR
If your error says debug/libs.jar, build --debug then --release.
If your error says profile/libs.jar, build --profile then --release.

That works, thanks!

This works, I had to do all 3 builds,
first with the --debug flag and then --profile to finally compile in --release mode.

THANKS

@elhe26
Copy link

elhe26 commented Jul 8, 2021

I really don't get why you don't reopen it. There is not a single word that i would change if i created a new one.

Run flutter create counter
Update the app to use Android Gradle plugin 4.0.0
Run flutter build apk

You can scroll to the top to see more details.

Gradle expects a ../libs.jar for every combination of flavor - buildType (in new project needs 3 - release - debug - profile, in my project needs 48 so the above 'solutions' of running each one once is out of the question especially with dev-op).

This is not a simple issue as it prevents us from releasing an app as the apk is never generated.

Same issues. I have 2 flavors and when building, the error is this:

* What went wrong:

Execution failed for task ':app:lintVitalDevelopmentRelease'.

> Could not resolve all artifacts for configuration ':app:productionReleaseRuntimeClasspath'.

   > Failed to transform libs.jar to match attributes {artifactType=processed-jar, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.

      > Execution failed for JetifyTransform: /Users/builder/clone/build/app/intermediates/flutter/productionRelease/libs.jar.

         > Transform's input file does not exist: /Users/builder/clone/build/app/intermediates/flutter/productionRelease/libs.jar. (See https://issuetracker.google.com/issues/158753935)

This issue is critical since we only need to perform 1 build instead of 2 or 3 (in the case of @niktses, 48).

Edit:

I'm using the "hack" of calling --debug and --profile and only calling it for the current flavor.

Edit 2:

I got it working using this combo:

android/app/build.gradle

...

project.ext.envConfigFiles = [
        developmentdebug: ".env.development",
        developmentprofile: ".env.development",
        developmentrelease: ".env.development",
        productiondebug: ".env.production",
        productionprofile: ".env.production",
        productionrelease: ".env.production",
]

...

    // TODO: Temp measure - Add 'checkReleaseBuilds false' 
    lintOptions {
        disable 'InvalidPackage'
        checkReleaseBuilds false
    }
...

android/build.gradle

     ...
     dependencies {
        classpath 'com.android.tools.build:gradle:4.1.2' // <-- using gradle +4.0 instead of downgrade
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
         ...
    }
...

*android/gradle/wrapper/gradle-wrapper.properties

...
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip

CI/CD (config per flavor)

# Build (Debug & profile)

flutter build apk --debug --flavor <flavor>
flutter build apk --profile --flavor <flavor>

This config worked without any issue (yet). @niktses you could try using this if works for you. I haven't tried without using debug or profile.

Edit 3:

We can use the above configuration without this part:

CI/CD (config per flavor)

# Build (Debug & profile)

flutter build apk --debug --flavor <flavor>
flutter build apk --profile --flavor <flavor>

@niktses, you can use the above config omitting the previous part.

@pedromassango
Copy link
Member

If you don't want to mess with your CI/CD configs then just upgrade your Gradle version:

User Android Gradle 7.0.0-alpha14 in your android/build.gradle:

dependencies {
    classpath 'com.android.tools.build:gradle:7.0.0-alpha14'
}

And upgrade your Gradle version to 7.0-rc-1 in android/gradle/wrapper/gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-rc-1-all.zip

@comatory
Copy link

comatory commented Jul 15, 2021

If you don't want to mess with your CI/CD configs then just upgrade your Gradle version:

User Android Gradle 7.0.0-alpha14 in your android/build.gradle:

dependencies {
    classpath 'com.android.tools.build:gradle:7.0.0-alpha14'
}

And upgrade your Gradle version to 7.0-rc-1 in android/gradle/wrapper/gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-rc-1-all.zip

@pedromassango This did work for me, the approaches from above did not work for me at all. I'm on Flutter 2.2.2.
I don't feel comfortable using alpha and rc versions though and I'm not sure why this issue is closed since this happens still.
My project did not start with flavors, I added them later.

flutter doctor output is:

[✓] Flutter (Channel stable, 2.2.2, on macOS 11.4 20F71 darwin-x64, locale en-CZ)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.2)
[✓] IntelliJ IDEA Community Edition (version 2020.2.1)
[✓] VS Code (version 1.54.3)
[✓] Connected device (2 available)

• No issues found!

@pedromassango
Copy link
Member

If you don't want to mess with your CI/CD configs then just upgrade your Gradle version:

User Android Gradle 7.0.0-alpha14 in your android/build.gradle:

dependencies {
    classpath 'com.android.tools.build:gradle:7.0.0-alpha14'
}

And upgrade your Gradle version to 7.0-rc-1 in android/gradle/wrapper/gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-rc-1-all.zip

@pedromassango This did work for me, the approaches from above did not work for me at all. I'm on Flutter 2.2.2.
I don't feel comfortable using alpha and rc versions though and I'm not sure why this issue is closed since this happens still.
My project did not start with flavors, I added them later.

flutter doctor output is:

[✓] Flutter (Channel stable, 2.2.2, on macOS 11.4 20F71 darwin-x64, locale en-CZ)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.2)
[✓] IntelliJ IDEA Community Edition (version 2020.2.1)
[✓] VS Code (version 1.54.3)
[✓] Connected device (2 available)

• No issues found!

It is closed because this is not a Flutter issue but an Android project issue.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
c: crash Stack traces logged to the console c: regression It was better in the past than it is now customer: crowd Affects or could affect many people, though not necessarily a specific customer. dependency: android Android team may need to help us found in release: 1.21 Found to occur in 1.21 has reproducible steps The issue has been confirmed reproducible and is ready to work on P1 High-priority issues at the top of the work list platform-android Android applications specifically t: gradle "flutter build" and "flutter run" on Android tool Affects the "flutter" command-line tool. See also t: labels.
Projects
None yet