Axen_Rangs
Axen_Rangs

Reputation: 427

Flutter release apk not working correcty but debug app works fine

I've built simple meme/photo editor app using flutter.It is working perfectly in debug mode but It's not working in release mode.There is a container in my app which is used to show user image loaded using image picker.When I select image in debug app it's showing but when tried it in release version It shows only a blank container and also it alignments are changed.I've tried flutter clean and have added the Internet permission to manifest file.But nothing correct that issue.

Also I've tried app bundle and split apks but Issue is remaining.Is there any method to generate and upload debug apk to playstore without getting any security issues?

This is the part with issues...It is working correctly in debug but not in release mode.

 Center(child: Stack(
           children: <Widget>[
             _image != null ?  Expanded(
                       child: Container(
                       child: Image.file(
                        _image,
                        height: 400,
                        width: 400,
                        fit: BoxFit.cover,),
                              ),
                            )

Upvotes: 4

Views: 19919

Answers (8)

Abdullah Opadeji
Abdullah Opadeji

Reputation: 236

It's probably because your app requires internet connection, but you haven't specified this permission in your AndroidManifest.xml file at: YOUR_PROJECT/android/app/src/main/AndroidManifest.xml

Confirm whether the internet permission is enabled.

Before:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application...

Now:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<application..

I hope this helps.

Upvotes: 6

Ultranmus
Ultranmus

Reputation: 460

I have found a very good solution for this .It also work for packages like autoscale_tabbarview that work perfectly fine on debug mode but causes problem in release mode. Just like the above comment add proguard for all plugins.

  1. Create ProGuard Rules File:

In your Flutter project's 'android/app' directory, create a file named 'proguard-rules.pro' if it doesn't already exist.

#Flutter Wrapper

-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.**  { *; }
-keep class io.flutter.util.**  { *; }
-keep class io.flutter.view.**  { *; }
-keep class io.flutter.**  { *; }
-keep class io.flutter.plugins.**  { *; }
-keep class androidx.lifecycle.** { *; }

-keepattributes *Annotation*
  1. Update build.gradle:

In your 'android/app/build.gradle' file, make sure that the proguardFiles directive includes the custom rules file you created. The proguardFiles line should look like this:

buildTypes {
    release {
        signingConfig signingConfigs.debug
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
  1. Update MainActivity.java:

In your 'MainActivity.java' file, add the below code and for .kt file you can see above comments or can ask chatgpt to convert into .kt file

package com.example.pettera_project;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.FlutterEngine;

public class MainActivity extends FlutterActivity {
    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }
}

And don't forget to change the package name.

Upvotes: 1

Shahed Oali Noor
Shahed Oali Noor

Reputation: 656

remove unnecessary Expanded() from UI code

Upvotes: 2

SAG skills
SAG skills

Reputation: 15

My problem was solved by removing expanded widget,

My debug mode was working perfect but when I release Apk bundle to publish the main screen was not showing images and detail so I just remove Expanded widget by container and it's working for me.

Upvotes: 0

omarezz332
omarezz332

Reputation: 157

I had this exact problem. I solved it with these solutions:

  1. Add android/app/proguard-rules.pro

    #Flutter Wrapper
    -keep class io.flutter.app.** { *; }
    -keep class io.flutter.plugin.**  { *; }
    -keep class io.flutter.util.**  { *; }
    -keep class io.flutter.view.**  { *; }
    -keep class io.flutter.**  { *; }
    -keep class io.flutter.plugins.**  { *; }
    -keep class androidx.lifecycle.** { *; } 
    -keep @interface com.google.gson.annotations.SerializedName
    -keep @interface com.google.gson.annotations.Expose
    -keepattributes *Annotation*
     #https://github.com/flutter/flutter/issues/58479
     #https://medium.com/@swav.kulinski/flutter-and-android-obfuscation-8768ac544421
    
  2. add proguard to buildTypes in app level build.gradle

    buildTypes {
    release {
        profile {
            matchingFallbacks = ['debug', 'release']
        }
    minifyEnabled true
    useProguard true
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    signingConfig signingConfigs.release
    }
    }
    lintOptions {
    disable 'InvalidPackage'
    checkReleaseBuilds false
    }
    
  3. edit the kotlin main activity file:

    import androidx.annotation.NonNull;
    import io.flutter.embedding.android.FlutterActivity
    import io.flutter.embedding.engine.FlutterEngine
    import io.flutter.plugins.GeneratedPluginRegistrant
    
    class MainActivity: FlutterActivity() {
         override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
          GeneratedPluginRegistrant.registerWith(flutterEngine);
      }
    }
    

Upvotes: 4

Bo Kristensen
Bo Kristensen

Reputation: 1648

Hi Axen_Ranges sorry I am late to the party. Today I experienced the same behavior. The fix was to remove the Expanded() from my UI code. It seems there is a problem with some types of layouts.

Earlier I had a Column() with an Expanded(). This works! :)

 body: Column(
    children: <Widget>[
      SizedBox(height: 30.0),
      logoSection,
      logoTextSection,
      Expanded(
        child: Column(children: [
          userImageSection,
          infoTextSection,
        ])
      ),
      pleaseConfirmSection,
      buttonSection,
      ],
    ),
  )

I changed the Column() into a Scrollbar() with a ListView(), still containing the Expanded(), this does not work! :(

body: Scrollbar(
    isAlwaysShown: true,
    controller: _scrollController,
    child: ListView(
      controller: _scrollController,
      primary: false,
      children: <Widget>[
        SizedBox(height: 30.0),
        logoSection,
        logoTextSection,
        Expanded(   // This Expanded() does not work with Scrollbar() or ListView() removing it fixes everything :)
          child: Column(
            children: [
              userImageSection,
              infoTextSection,
            ]
          )
        ),
        pleaseConfirmSection,
        buttonSection,
      ],
    ),
  )

The problem here is that the flutter framework does not tell you that there is a problem. It works no problem in debug mode. But in release mode it only shows an empty container.

Upvotes: 0

Axen_Rangs
Axen_Rangs

Reputation: 427

Problem disappeared when I replaced the expanded with container..Thanks for support

Upvotes: 1

magicleon94
magicleon94

Reputation: 5162

By what you've shared (which is not a lot) I can assume you've missed adding the INTERNET permission on the main manifest.

Usually 3 manifests are created. Open your android/app/src folder, you'll see 3 subfolders:

  • debug
  • profile
  • main

It's easy to be mistaken and put the permission in the wrong Manifest, I've been there some times!

Upvotes: 14

Related Questions