Flutter build apk on release mode cannot generate updated version
Asked Answered
A

10

55

I had run this flutter build apk to release my App. Now I had built version 2 of that.

Now, I want to release my version 2 App. So, I run flutter build apk again. I get the released apk but It is still version 1 released apk. I deleted the released apk and tried again but still got version 1 released apk.

I tried this too but It is also giving me Version 1 apk

While building version 2 in debug mode, everything works fine.

Here is the Github Link to that App: https://github.com/nitishk72/Flutter-Github-API

Abott answered 17/4, 2018 at 9:15 Comment(0)
R
84

I think you need

flutter clean

(I'd consider it a bug that this is necessary, but I'm encountering it as well)

and

flutter build apk --release
Rummage answered 17/4, 2018 at 9:16 Comment(4)
Glad to hear . .Leister
@GünterZöchbauer I am still facing the problem. it says that ` Running 'gradlew assembleRelease'... Dart snapshot generator failed with exit code -1073741819 Snapshotting exited with non-zero exit code: -1073741819 ` I feel uncomforttable to give a debuggable version.Guesthouse
I also don't think shipping a debug version is a good idea (if even possible) but I don't know how to solve. Can you please create an issue in GitHub with as much information as possible.Leister
It's not working for me. PLease help. When opened the app gets killed.Narcis
M
100

If your problem is that the flutter build (APK, bundle) isn't making API calls in a real device, this is because you need to add the Internet permission to Android Manifest before creating the release/build.

By default, "internet use" will work fine on the emulator but not on a real device.

To fix this, simply:

Open the file "android/app/src/main/AndroidManifest.xml" and add the proper user-permission:

<manifest> 
...
 <uses-permission android:name="android.permission.INTERNET"/>
...
</manifest>

And then create your build again.

Meaty answered 20/7, 2020 at 16:41 Comment(2)
Thanks! This is a bit confusing, but makes sense since Flutter debug version always needs internet permission in order to use debugging tools. A discussion about this is here: github.com/flutter/flutter/issues/20789Mahau
Thank you so much! I couldn't how should I frame my question on google but here is the answer! You saved my day!Demerol
R
84

I think you need

flutter clean

(I'd consider it a bug that this is necessary, but I'm encountering it as well)

and

flutter build apk --release
Rummage answered 17/4, 2018 at 9:16 Comment(4)
Glad to hear . .Leister
@GünterZöchbauer I am still facing the problem. it says that ` Running 'gradlew assembleRelease'... Dart snapshot generator failed with exit code -1073741819 Snapshotting exited with non-zero exit code: -1073741819 ` I feel uncomforttable to give a debuggable version.Guesthouse
I also don't think shipping a debug version is a good idea (if even possible) but I don't know how to solve. Can you please create an issue in GitHub with as much information as possible.Leister
It's not working for me. PLease help. When opened the app gets killed.Narcis
A
35

I had facing same problem "Application work properly in Debug mode but not in release mode". I don't know why... but finally, I have solved by adding these lines shrinkResources false minifyEnabled false in android/app/build.gradle file.

 buildTypes {
     release {
         signingConfig signingConfigs.config
         shrinkResources false
         minifyEnabled false
     }
 }
Apollus answered 5/3, 2021 at 6:42 Comment(2)
For some reasons, the --no-shrink build parameter stopped working for me, but this helpedThrong
Isn't shrinking file size and obfuscating the code important?Salesgirl
P
10

When you are creating new Flutter project (from Android Studio menu), in file:

/android/app/build.gradle

which is there: enter image description here

you have:

buildTypes {
    release {
        signingConfig signingConfigs.debug    //   <--- HERE YOU ARE USING DEBUG KEY
    }
}

Which means that release build (e.g. triggered from menu Build -> Flutter -> Build APK) will use debug key for signing.

Full build.gradle file (created by Android Studio): enter image description here

Solution

Change from:

signingConfig signingConfigs.debug

to:

signingConfig signingConfigs.release

so (in build.gradle) you should have:

buildTypes {
    release {
        signingConfig signingConfigs.release
    }
}

Error

When you have error Could not get unknown property 'release' for SigningConfig - just add section release in build.gradle, so:

  1. Create new .jks file (via Generate Signed Bundle / APK...

  2. Put credentials in build.gradle (:app):

    enter code here

Providential answered 4/8, 2020 at 22:13 Comment(2)
It doesn`t work to me, got the error: Could not get unknown property 'release' for SigningConfig container of type org.gradle.api.internal.FactoryNamedDomainObjectContainer.Drayage
@MarcoA.Braghim the error is so so clear. You do not have property 'release' in SigningConfig container. In android { } you have to create it, so android { signingConfigs { release { } } } and ther put your keyAlias, storeFile, keyPassword and storePassword.Providential
M
9
  1. you can change in local.properties android project

        sdk.dir=D:/SDK               //your android SDK location
        flutter.sdk=D:\\Flutter\\flutter  //your flutter SDK location
        flutter.versionName=1.0.0
        flutter.versionCode=2
        flutter.buildMode=release
    
  2. changes in your android/app/build.gradle file

         buildTypes {
                release {
                    // if everything ok then not add 
                    //minifyEnabled true 
                    //another you can remove minifyEnabled true
                    proguardFiles getDefaultProguardFile('proguard- 
                     android.txt'), 'proguard-rules.pro'
                    signingConfig signingConfigs.release
                }
            }
    
  3. you can also changes in android/app/build.gradle defaultConfig method body.

      multiDexEnabled true
    
  4. if you want to migrate to androidx then do it setup.

    dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' implementation 'androidx.multidex:multidex:2.0.1'

    }

    1. and clean before create build folder by following this cmd.

       - flutter clean
      
       - flutter build apk --release
      
       - flutter install
      

6.Release Apk location.

        in `android studio termial` or inside `project-root` you can navigate to this.

        cd build\app\outputs\apk\release
Masticatory answered 25/3, 2019 at 5:16 Comment(5)
iOS dev here. Did Android using Flutter. Tried flutter clean and then flutter apk --release but it always created a debug build. Followed above steps and added signingConfig.release and got the Release build with flutter apk -- release also refer flutter.dev/docs/deployment/android which is essentially above stuff but in detail on how to create the key file and where to store and link it, etc.Sanderson
it will create debugRelease. ( why because inside app folder inside android code , you will find a build.gradle where you will see release tag which is bind default to debug.keystore.Masticatory
oh. Then how do I create a Release build?Sanderson
you can create release build from Android-studio. there is a option present.Masticatory
Thanks Tushar. I was getting some weird error on Android Studio. So I created the release build from command line (flutter apk --release).Sanderson
E
9

If you're using an API Call#

Make sure you have the Internet Permission on you android Manifest

<manifest> 
...
 <uses-permission android:name="android.permission.INTERNET"/>
...
</manifest>

If you're using Shared Preferences plugin

In android\build.gradle set your dependencies to use the classpath 'com.android.tools.build:gradle:3.5.1'

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

Like this:

dependencies {
        classpath 'com.android.tools.build:gradle:3.5.1'
        ...
    }
Editorialize answered 26/5, 2021 at 16:0 Comment(0)
F
6

1).

add following lines in android\app\src\mainAndroid manifest

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

and then add these lines in android\app\build.gradle

   buildTypes {
   release {
        shrinkResources false
        minifyEnabled false
   }}

2).

Building apk using the below commands.

flutter build apk --no-shrink

or

flutter build apk --split-per-abi --no-shrink

Both Solutions worked for me but personally I recommend second one

Friederike answered 29/3, 2021 at 9:46 Comment(0)
K
4

I hope this can help someone. I ran into a problem that my release version does not works and there are no any error messages in logcat. The problem turned out to be that I had FutureBuilder in which i start Future<void>. By running flutter run it works properly (snapshot.hasData was true), but in release version it was always false. Of course, documentation of the FutureBuilder says that hasData should be false in this case. But in fact in debug mode it has different behaviour.

P.S. Another possible problems: Now my app still not working, but this time i saw on logcat next error:

Unhandled Exception: MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)

Downgrading gradle plugin version from 7.* to 3.5.4 solves this issue.

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

Kiss answered 1/2, 2021 at 12:0 Comment(0)
B
2

I decide to open the MyProject/android folder in android studio and build the apk from android studio. It finally worked properly.

Birdsall answered 14/1, 2021 at 11:11 Comment(0)
S
0
flutter build apk // this generate apk 

flutter build appbundle // this generate aab

And remember that you need to execute flutter clean before execute flutter build apk

Sternum answered 8/3 at 13:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.