Flutter failed to compile apk: Finished with error: Gradle task assembleDebug failed with exit code 1 - flutter

Flutter failed to compile an apk. I am not sure if this has something to do with AndroidX or something.
Launching lib\main.dart on Android SDK built for x86 in debug mode...
Initializing gradle...
Resolving dependencies...
Running Gradle task 'assembleDebug'...
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
Finished with error: Gradle task assembleDebug failed with exit code 1
I ran flutter clean. I also ran flutter run -v. This is what happened: https://1drv.ms/t/s!An3OLJ7n0CjEku1SBrSABWab901z3Q
(I have used an external file locker because the stack trace is too large for the Stack Overflow editor).
This issue is similar to https://github.com/flutter/flutter/issues/30634

try follow steps:1. in gradle-wrapper.properties, downgrade from 5.1.1 to 4.10.2.2. int root/build.gradle, change android gradle version into 'com.android.tools.build:gradle:3.3.2'
Hope it is useful

It is common after migration to androidX.
If you followed recommended route from here:
https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility
Try this:
Check with steps in "not recommended" way in above tutorial if every listed files is same as in your project, especially :
in root/android/gradle/wrapper/gradle-wrapper.properties set distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip (probably you have version 5.1.1 )
in root/android/build.gradle, set
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
}
check if in root/android/app/build.gradle SDK version is set to minimum 28 for:
compileSdkVersion 28 and targetSdkVersion 28
If you still get error then here is what helped me:
delete .gradle and .m2 folders from $HOME directory, as well with in app folders: root/android/.gradle and root/build
flutter doctor
flutter clean
flutter packages pub cache clean
if you changed (as me) app icon then run:
flutter packages pub run flutter_launcher_icons:main
it will render icons for app - without it print same error with gradle..
Invalidate caches in Android Studio "File" > "Invalidate Caches / Restart..."
If nothing from above works open android project as module
(you can do it by opening any file inside android folder for example manifest, then bar above code will shows with link to "Open for Editing in Android Studio", more here https://flutter.dev/docs/development/tools/android-studio )
Then click on gradle tab on the left and compile app - should show now more detailed error message.

Ok so here is your fix, open android sdk settings and uncheck Hide Obsolete Packages and in sdk tools, Check Android SDK Tools (Obsolete) than click ok/apply. It will download old SDK. Than Restart the IDE, Run default flutter program on emulator. This error occurs because flutter isn't compatible with latest Android SDK so you have yo manually install old SDK in Android studio.

Try running "flutter clean" in the project. If it doesn't work, please paste the output after running "flutter run -v".

In the debug mode applications worked well, but when I tried to build a release version, it had this error.
"Flutter doctor -v" was ok.
Made flutter clean.
Checked all AndroidX recommendations, used all latest versions of packages.
Checked android/app/fourpillarsappru1.keystore settings.
Finally the decision was use build in in Android Studio commands instead of command line "flutter build apk".
So, the decision in my case was:
Open build.gradle for editing in Android Studio
Choose Build / Generate Signed Bundle/APK

I had the same problem and solve the error by running flutter doctor --android-licenses and accept all licenses agreement.
I hope it will help

The Solution that works for me and makes the most sense CLEAN CACHE
Enter Command below
--> flutter packages pub cache clean

Downgrading the build gradle version in gradle-wrapper.properties from 5.1.1 to 4.10.2 worked for me.
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
Dependencies in project: build.gradle
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.google.gms:google-services:4.0.1'
}
module: 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 versionPropertiesFile = rootProject.file("../version.properties")
def versionProperties = new Properties()
versionProperties.load(new FileInputStream(versionPropertiesFile))
def flutterVersionCode = versionProperties.getProperty('flutter.versionCode')
def flutterVersionName = versionProperties.getProperty('flutter.versionName')
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
def keystorePropertiesFile = rootProject.file("key.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
android {
compileSdkVersion 28
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
applicationId "com.lok.sewa"
minSdkVersion 16
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
}
}
buildTypes {
release {
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.release
}
}
}
flutter {
source '../..'
}
dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
apply plugin: 'com.google.gms.google-services'

Try checking your key.properties file. There might be error in key path, keyAlias or passwords. The error by key errors is not traceable.
This solution worked for me

It is android problems only so
Open in Android Studio
and pick Flutter->Open module in Android Studio
menu Build->Build Bundle(s) / APKs->Build Bundles.

My code contain keystore info in build.gradle,but we don't have keystore file in our git repository.My colleague uploaded his local keystore file and the problem was solved.

I just simply fixed it by changing the minSdkVersion . Just try minSdkVersion 19 or 21. It will fix.
compileSdkVersion 29
minSdkVersion 19
targetSdkVersion 29

Related

Build APK stuck at Task :app:checkReleaseAarMetadata

i am new in flutter.
When i run:
flutter apk build -v
it always stuck at
[+29946 ms] > Task :app:checkReleaseAarMetadata
I left it for almost an hour and nothing,
same happen when i run it on release or debug on my own device via usb debugging, but run perfectly on emulator on debug mode
any help would be appreciated, thanks
add the following line to android/build.gradle.try it and update is it fixed!
buildscript {
ext {
androidXCore = "1.7.0" // <-- Add this line
buildToolsVersion = "29.0.3"
minSdkVersion = 21
compileSdkVersion = 30
targetSdkVersion = 29
ndkVersion = "20.1.5948944"
}
Thanks, I found the solution.
In the end tried to clean install Android Studio including sdk, gradle, etc by deleting android studio related stuff on %APPDATA%, %USERPROFILE%, %LOCALAPPDATA%.
After that i build the apk again from vscode console and it work.
I think the error occurred because of missing stuff in gradle or error during sdk installation.

Missing compileSdkVersion for google mobile ads

defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.ringit2"
minSdkVersion 21
targetSdkVersion 30
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Here is what I found on my build.gradle and I can't find compileSdkVersion which I have to change to at least 28 in order for google mobile ads to operate.
After I added google_mobile_ads: ^1.1.0 on pubspec.yaml and imported the package to the main.dart, The app opens as I run the simulator but shuts down right away. Console shows me this message and it is stuck just like this forever.
Launching lib\main.dart on SM G950N in debug mode...
Running Gradle task 'assembleDebug'...
√ Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app.apk...
Please note that my ext.kotlin_version is '1.6.10' because people have recommended to upgrade it
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
Also changed the distributionUrl...
Not sure why google mobile ads package is so hard to use even just to start but what should I change to use it?
I am also using kotlin version '1.6.10' and compilesdk version is inside android>app>build.gradle
The compileSdkVersion will be inside
android>app>build.gradle
inside that you can see compileSdkVersion in line 30
Also check your environment path of flutter

What could be the cause of this error. Execution failed for task ':app:compileFlutterBuildRelease'

I tried generating a build release for flutter and I got these errors. Execution failed for task ':app:compileFlutterBuildRelease'. The code I used for generating build is flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi. The debug works perfectly.
android {
compileSdkVersion 29
buildToolsVersion 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.abc.def"
minSdkVersion 21
targetSdkVersion 29
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
}
}
The code I used for generating build is flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi. The debug works perfectly. // This is the main section that I've updated
// to get the release APK to build
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"
implementation platform('com.google.firebase:firebase-bom:25.12.0')
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-functions:19.0.2'
implementation 'com.google.firebase:firebase-messaging:20.2.0'
implementation 'com.google.firebase:firebase-database:19.3.0'
implementation 'com.google.firebase:firebase-core:17.4.3'
implementation 'com.google.code.gson:gson:2.8.6'
}
apply plugin: 'com.google.gms.google-services'
First, make sure you have followed up correctly all procedures in this documentation, then after you are sure you have followed up correctly then
Try this out:
flutter clean
flutter build apk --debug
flutter build apk --profile
flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi
suggestion:
if this doesn't help then add the full error which you are getting as well as both complete Gradle files
and instead of splitting apk use app bundle, for app bundle use:
flutter build appbundle
if this helps then please notify in comments
run in your terminal
flutter channel stable
flutter upgrade --force
flutter pub cache repair
next,
cd <YOUR APP FOLDER>
flutter clean
AND create a seperate fluter project using flutter create <app-name>
and include the generated ios folder and android folder into your project (obviously you need to delete the current ios and android folders) . If you made changes into these folders consider using version control

Flutter error: Execution failed for task ':app:compileDebugJavaWithJavac'

I try run on android and return this error.
I Aredy tried:
gradle.properties:
android.useAndroidX=true
android.enableJetifier=true
build.gradle:
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
my pubspec.yaml:
flutter_facebook_login: ^3.0.0
firebase_database: ^3.1.0
firebase_auth: ^0.15.0+1
cloud_firestore: ^0.12.11
url_launcher: ^5.2.7
google_maps_flutter: ^0.5.21+12
image_picker: ^0.6.2+1
firebase_storage: ^3.0.8
intl: ^0.16.0
What went wrong: Execution failed for task ':app:compileDebugJavaWithJavac'.
Compilation failed; see the compiler error output for details.
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 35s 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 cloud_firestore... The
plugin cloud_firestore could not be built due to the issue above.
SOLUTION:
When you created the new project, did you select "androidX"? And are those versions the latest versions? (meaning, androidX compatible/requiring). if not sure, create a new project and make sure to select androidX, and change the sdk settings to 23/29 as above.
Edit: Solution was a combination of creating a new project with AndroidX selected and add the proper SDK versions to the gradle file. Possibly with using correct dependency versions.
To migrate to AndroidX, you need to set the SDK versions to 21 or higher. I recommend 23 because that removes some other issues as well.
compileSdkVersion 29
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example"
minSdkVersion 23
targetSdkVersion 29
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
// testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
I recently encountered this error using Flutter 2.2.2, None of the above solutions worked for me.
First try basic commands like:
cd android && ./gradlew clean
flutter clean
If it doesn't work:
set distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip in android/gradle/wrapper/gradle-wrapper.properties
if your gradle version is <= 6.7 (refer gradle issue)
Try basic commands and build again!
Solution
flutter clean
delete android/.gradle and run
revert pubspec.lock and flutter pub get
flutter run
Change compileSdkVersion value to flutter.compileSdkVersion
also change minSdkVersion,targetSdkVersion values as shown below
android {
compileSdkVersion flutter.compileSdkVersion
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.octs"
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
This worked for me.
None of answers above helped me. I use IntelliJ Idea as an IDE instead of Android Studio (because I develop other modules in other langs, not only Flutter app). I was able to run ./gradlew :app:compileDebugJavaWithJavac from command line, but flutter run failed. After long time I've realized that Flutter uses Android Studio to let it build android app. So if you use IntelliJ Idea, do check what Java you use not only in Idea (I use Java 11), but what Java you use in Android Studio! (mine AS used Java 8). After changing to Java 11, problem was solved.
I solved my problem by changing the version (reduced the gradle version), which caused me another problem, but related to one API and Kotlin plugin. Since I cannot delete Kotlin I took out the API and it came to life again! thanks for your help here!
Try this package flutter_stripe: ^3.2.0

Flutter Build failed with an exception for Geolocation plugin

Whenever I'm trying to include flutter geolocation(https://github.com/loup-v/geolocation) plugin I'm getting following error.
Initializing gradle...
Resolving dependencies...
Running 'gradlew assembleDebug'...
registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection)
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
* Get more help at https://help.gradle.org
BUILD FAILED in 3s
Finished with error: Gradle build failed: 1
My pubspec.yml dependancies list is
dependencies:
flutter:
sdk: flutter
google_sign_in: "^3.0.2"
firebase_auth: "^0.5.5"
shared_preferences: "^0.4.1"
cloud_firestore: "^0.7.2"
firebase_storage: "^0.2.5"
image_picker: "^0.4.1"
progress_hud: "^0.2.2"
cached_network_image: "^0.4.1"
intl: "^0.15.6"
path: "^1.5.1"
geolocation: "^0.2.1"
My app>build.gradle code
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.")
}
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
compileSdkVersion 27
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.bhramaan.bhramaan"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.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 {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
apply plugin: 'com.google.gms.google-services'
What's going wrong?
There are three things I can suggest to help. The first is to do a full rebuild by first running flutter clean and then running again. Occasionally things get a little bit messed up between flutter & android, and that helps.
The next thing I would do is check that you're getting the same version of all the Play services. For some reason gradlew sometimes messes that up. Run gradlew app:dependencies from the android (or ./gradlew if you're on mac/linux). If you're getting different versions, add them all as dependencies of the same version.
For example, in my android/app/build.gradle, I had to set
implementation 'com.google.android.gms:play-services-base:15.0.2'
implementation 'com.google.android.gms:play-services-vision:15.0.2'
even though Gradle should have sorted that out. In particular look out for com.google.android.gms:play-services-location as it's a dependency of the geolocation package. Same goes for the com.android.support libraries.
If that still doesn't work, try running the gradle build directly, and in particular looking at the stack trace (gradlew app:build --stacktrace from the android folder) to see if it tells you anything. You could then post that, or take a look at the generic Android questions for a solution. It could be that you need to enabler multi-dex, although I don't think so.
I believe this is an issue with geolocation gradle version of firebase and your flutter version being incompatible. Try adding multiDexEnabled true to your build gradle. If that does not solve it open your flutters android project in android studio and go to the geolocation build gradle and change dependencies version to classpath 'com.android.tools.build:gradle:3.1.2' & at the bottom update the play services to api "com.google.android.gms:play-services-location:15.+".
Finally, I've fixed this by following the steps as mentioned by #rmtmckenzie.
In my android/app/build.gradle I added following piece of code &
configurations.all {
resolutionStrategy {
force 'com.google.android.gms:play-services-location:15.0.1'
}
}
and enabled multiDexEnabled true
defaultConfig {
multiDexEnabled true
}
With this setting now, my whole app working fine.
In your app's top level build.gradle update classpath. For me
classpath 'com.android.tools.build:gradle:3.5.3' is working fine.