Pierfrancesco Soffritti
Pierfrancesco Soffritti

Reputation: 1738

Proguard minifyEnabled true for debug build, not working on pre-Lollipop

The methods count of my app is higher than the 64k limit, so I'm using Proguard to reduce it.

Everything works fine for release builds.
Debug builds, on the other hand, are successful only for Lollipop+ devices. When launching on pre-Lollipop devices I always have the infamous error:

com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

This is what I have in the app level gradle file:

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

I've tried removing minifyEnabled true in the debug build, and then the build fails also on Lollipop+, therefore proguard is working! but not on pre-Lollipop.

For what i know minifyEnabled should not be platform dependent, so maybe the build process skips it (for I don't know what reason) on pre-Lollipop devices.

Anyone knows a solution to this issue?

ps. I'm aware of the multidex possibility, but I'm leaving it as my last resort.

EDIT:

This is the full code of the app level gradle file:

apply plugin: 'com.android.application'
apply plugin: 'android-apt'
apply plugin: 'me.tatarka.retrolambda'

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'me.tatarka:gradle-retrolambda:3.2.5'
        classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.5.5'
    }
}

android {
    compileSdkVersion 24
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId "com.pierfrancescosoffritti.shuffly"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 30
        versionName "0.13"
    }
    buildTypes {
        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

def dbflow_version = "3.1.1"

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    apt 'com.jakewharton:butterknife-compiler:8.2.1'
    apt "com.github.Raizlabs.DBFlow:dbflow-processor:${dbflow_version}"
    compile "com.github.Raizlabs.DBFlow:dbflow-core:${dbflow_version}"
    compile "com.github.Raizlabs.DBFlow:dbflow:${dbflow_version}"

    compile 'com.android.support:support-v4:24.1.1'
    compile 'com.android.support:design:24.1.1'
    compile 'com.android.support:recyclerview-v7:24.1.1'
    compile 'com.android.support:cardview-v7:24.1.1'
    compile 'com.android.support:palette-v7:24.1.1'
    compile 'com.jakewharton:butterknife:8.2.1'
    compile 'com.github.PierfrancescoSoffritti:AndroidUtils:0.5'
    compile 'com.github.PierfrancescoSoffritti:SlidingDrawer:0.10'
    compile 'com.github.PierfrancescoSoffritti:WebBasedOAuth:0.7'
    compile 'com.github.PierfrancescoSoffritti:ShrinkingImageLayout:0.4'
    compile 'com.github.PierfrancescoSoffritti:ExpandableLayout:0.1'
    compile 'com.google.apis:google-api-services-youtube:v3-rev177-1.22.0'
    compile 'com.google.api-client:google-api-client-android:1.20.0'
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'io.reactivex:rxjava:1.1.8'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.github.Commit451:ElasticDragDismissLayout:1.0.4'
    compile 'com.google.firebase:firebase-core:9.2.1'
    compile 'com.google.firebase:firebase-crash:9.2.1'
    compile 'com.google.firebase:firebase-ads:9.2.1'
    compile 'com.artemzin.rxjava:proguard-rules:1.1.8.0'
}

apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.getkeepsafe.dexcount'

project level gradle file:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath 'com.google.gms:google-services:3.0.0'
        classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.5.5'
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

proguard rules:

# retrolambda
-dontwarn java.lang.invoke.*

# picasso
-dontwarn com.squareup.okhttp.**

# adMob
-keep public class com.google.android.gms.ads.** {
   public *;
}
-keep public class com.google.ads.** {
   public *;
}

# GoogleApiClient
# Needed to keep generic types and @Key annotations accessed via reflection
-keepattributes Signature,RuntimeVisibleAnnotations,AnnotationDefault
-keepclassmembers class * {
  @com.google.api.client.util.Key <fields>;
}
# Needed by google-http-client-android when linking against an older platform version
-dontwarn com.google.api.client.extensions.android.**
# Needed by google-api-client-android when linking against an older platform version
-dontwarn com.google.api.client.googleapis.extensions.android.**
# Needed by google-play-services when linking against an older platform version
-dontwarn com.google.android.gms.**
# com.google.client.util.IOUtils references java.nio.file.Files when on Java 7+
-dontnote java.nio.file.Files, java.nio.file.Path
# Suppress notes on LicensingServices
-dontnote **.ILicensingService
# Suppress warnings on sun.misc.Unsafe
-dontnote sun.misc.Unsafe
-dontwarn sun.misc.Unsafe

# DBFlow
-keep class * extends com.raizlabs.android.dbflow.config.DatabaseHolder { *; }

Upvotes: 13

Views: 10130

Answers (3)

Amit Desale
Amit Desale

Reputation: 1301

Replace your build Types code with this code in app level gradle file. when you are debugging or running code on your device before final release then select built variant debug and when you are going to built signed apk for release then select built variant release

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

Upvotes: 1

Jay Shah
Jay Shah

Reputation: 742

Enable multidex enabled true and allow support to your application as a multidex application.Few steps to be followed are:

 defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 3
        versionName "1.0.1"
        **multiDexEnabled true**
    }

add this dependency to dependency list

compile 'com.android.support:multidex:1.0.0'

extend your application level class with MultiDexApplication

write this method in your application class

protected void attachBaseContext(Context base)
    {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

keep minifyEnabled false no matter debug or release build

update

dexOptions {
        jumboMode = true
        javaMaxHeapSize "4g" //specify the heap size for the dex process
        preDexLibraries = false
    }

Upvotes: -1

Fabio
Fabio

Reputation: 2834

If you want to try to reduce the method count, or at least you want to understand the difference from release to debug, I suggest you try looking at the dex count available in AS 2.2 preview 5, they had a youtube video (maybe from google IO) that made it pretty easy to count your methods.

This is only for counting methods, if you still use the same buildToolsVersion "23.0.3" you should get the exact same apk file in both versions of AS (apart from the fact that AS 2.2 ships with its own version of JDK, which is not supposed to get in your way).

Disclaimer: I have never used that tool apart from playing with it, so I wouldn't know what to recommend after you actually find a culprit there.

EDIT: here's the video https://youtu.be/csaXml4xtN8?t=331 for the "Apk Analyzer", that lives inside the "Build" menu. Don't try reviews earlier than 2.2 preview 3 or later, they previewd some things that were not released until later.

EDIT 2: also why are you only using shrinkResources on release? That's the line that is suppposed to eliminate unneded methods: "minifyEnabled" vs "shrinkResources" - what's the difference? and how to get the saved space?

Upvotes: 1

Related Questions