Ads are not showing up in release mode

3,503

Solution 1

Create a file called proguard-rules.pro in your android/app directory.

Add this line of code to it:

 -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 com.google.firebase.** { *; }
 -keep class com.shatsy.** { *; }

The most important being: -keep class com.google.firebase.** { *; } and -keep class com.shatsy.** { *; }

Now in your app level build.gradle file, add this to your buildType:

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

So that your buildType folder looks something like this:

buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.debug
        }
    }

Then run flutter build apk --buildTypeName

Example:

flutter build apk --release

A quicker solution is to add minifyEnabled false to your release buildType in you app level build.gradle

EXPLANATION: Proguard is probably blocking your app from using the firebase_ads library. That's probably why your app runs in debug mode, but not after building the apk.

Try it, and see if it works.

Solution 2

It takes a few hours to show ads on the release version. So if it is working on debug apk then you don't need to add any bit of code, just update your account details on AdMob and wait for account confirmation and you will see ads on release after few hours.

Solution 3

I had problems with the same and that was my error, I did not add the internet permission. Add the android.permission.INTERNET permission if your application code needs Internet access. The standard template does not include this tag but allows Internet access during development to enable communication between Flutter tools and a running app.

Step 1: Go to android\app\src\main\AndroidManifest.xml

Step 2: Copy this line below:

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

Step 3: Put it in AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.java2blog.helloworldapp">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".HelloWorldActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

Step 4: Rebuild the the .apk and try on mobile it should work.

Share:
3,503
Aman Verma
Author by

Aman Verma

Junior at CSE, Flutter dev, learning android

Updated on November 29, 2022

Comments

  • Aman Verma
    Aman Verma over 1 year

    Ads not showing up in release mode

    i want to try the solution mentioned here but i dont know how to

    when the app is signed in debug it shows ads , but not in release mode

    • Wilson Wilson
      Wilson Wilson almost 4 years
      What package are you using for ads? firebase_admob, admob_flutter, or both?
    • Aman Verma
      Aman Verma almost 4 years
      both , firebase_admob for interstial ad , and admob_flutter for banner
    • Wilson Wilson
      Wilson Wilson almost 4 years
      I've edited my answer. Added (-keep class com.shatsy.** { *; })
    • Ray Coder
      Ray Coder over 3 years
      Should we use firebase_admob and admob_flutter? Or can we just use admob_flutter? and why we should use firebase_admob? I mean in what case? @WilsonWilson
  • Aman Verma
    Aman Verma almost 4 years
    should i let it remain in debug mode as you have left
  • Wilson Wilson
    Wilson Wilson almost 4 years
    It depends on what you're currently using. If you're not using debug, remove it
  • Wilson Wilson
    Wilson Wilson almost 4 years
    Try adding minifyEnabled false to your app level build.gradle. And did you add -keep class com.shatsy.** { *; } ?
  • Aman Verma
    Aman Verma almost 4 years
    It has been 2 days+ after creating ad unit . and real ads are being showed when i sign the apk in debug mode as written in above reply , and where to add account details
  • Abdullah Khan
    Abdullah Khan over 3 years
    I add this line "-keep class com.shatsy.** { *; }" but still ads are not showing in release mode. Ads are working fine in debug mode. Can you also explain the purpose of this line?
  • Arnon Zilca
    Arnon Zilca over 2 years
    setting minifyEnabled false just for the sake of testing failed for me unless I also added shrinkResources false