I just updated Android Studio to 3.5.
BTW updated the Gradle version and several libraries.
To my surprise, the application does not fit into a single DEX (uses over 73,500 methods, previously in debug version as far as I remember ~50K)
What changes have I made:
Kotlin 1.3.41 -> 1.3.50
classpath 'com.android.tools.build:gradle:3.5.0' from 3.4.2
classpath 'com.google.gms:google-services:4.3.1' from 4.3.0
implementation 'com.google.android.gms:play-services-ads:18.1.1' from 18.1.0
implementation 'com.google.firebase:firebase-core:17.1.0' from 17.0.1
The new release APK has fewer (60) methods (26760) than the previous one.
Other problem I got:
NDK Resolution Outcome: Project settings: Gradle model version=5.4.1, NDK version is UNKNOWN error
App using AndroidX.
app is compiling & work now OK when set
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
I imported settings from an older version of Android Studio. Maybe here is the problem?
Or there is no problem and I have to reconcile and use multiDEX (for development) and in release apk drop multiDEX?. Will multidex be better than minifyEnabled for debug version?
Changed:
debug {
// minifyEnabled true
// proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
multiDexEnabled true
}
And is working with multiDex when I run emulator with APK >=21.
Prior 21 API will test using release APK only.
BTW APK analyze my debug version show:
classes.dex 54806 methods
classes2.dex 1107 methods
classes3.dex 6725 methods
Total: 62638.
When I disable multiDex.
Cannot fit requested classes in a single dex file (# methods: 73883 > 65536) ;))
Something is not right ;)
set multiDexEnabled true globally or for both build-types - else the release build will exceed the 64k limit of a single DEX file. debug & release build can have different method count, because these are two merged source-sets - and unused classes may also be stripped (multiDexEnabled true also adds it's own library). While minifyEnabled true for debug builds is useless ...and I really don't understand why one would intend to do that. When the non-public class/method names in the APK do not match those class names in the IDE, one might find it difficult to debug that, because breakpoints will not catch; it's alike shooting oneself into the leg.
If a single library update introduces a new dependency, this can add lots of classes & methods.
Related
Since the 3.5 update of Android Studio, I have this warning when building my app :
DSL element 'useProguard' is obsolete and will be removed soon. Use
'android.enableR8' in gradle.properties to switch between R8 and
Proguard..
Removing "useProguard" from build.gradle fixed the problem for me, like:
release {
minifyEnabled true
//useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
Update 2022, or Details; R8 is nowadays:
By default enabled,
And it simply replaces ProGuard,
But supports existing .pro files (and there should be no need to reconfigure).
Also, any way to disable R8, or use ProGuard instead, is deprecated (or even removed).
But debug flavors can set debuggable true, and continue to be debuggable line-by-line.
set the following in your project's gradle.properties file
android.enableR8=true
R8 also has full mode that is not directly compatible with Proguard. In order to try that out you can additionally set the following in your gradle.properties file
android.enableR8.fullMode=true
This turns on more optimizations, that can further reduce app size. However, you might need a few extra keep rules to make it work.
At a glance, when you build you project using Android Gradle plugin 3.4.0 or higher, the plugin no longer uses ProGuard to perform compile-time code optimization. Instead, the plugin works with the R8 compiler by default to handle the Shrink, obfuscate, and optimize your app. However, you can disable certain tasks or customize R8’s behavior through ProGuard rules files.
In fact, R8 works with all of your existing ProGuard rules files, so updating the Android Gradle plugin to use R8 should not require you to change your existing rules.
When you use Android Studio 3.4 or Android Gradle plugin 3.4.0 and higher, R8 is the default compiler that converts your project’s Java bytecode into the DEX format that runs on the Android platform. However, when you create a new project using Android Studio, shrinking, obfuscation, and code optimization is not enabled by default. You may enable them using the below code -
android {
buildTypes {
release {
// Enables code shrinking, obfuscation, and optimization for only
// your project's release build type.
minifyEnabled true
// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources true
// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
proguardFiles getDefaultProguardFile(
'proguard-android-optimize.txt'),
'proguard-rules.pro'
}
}
...
}
For the more adventurous, R8 also has full mode. In order to try that out you can additionally set the following in your gradle.properties file.3
android.enableR8.fullMode=true
This turns on more optimizations, that can further reduce app size. However, you might need a few extra keep rules to make it work. Learn more here - https://youtu.be/uQ_yK8kRCaA
R8 is the default tool available in Android Studio 3.4 onwards. There is no need to explicitly enable R8. Just remove the useProguard true line from the app/build.gradle file.
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
I would like to make my app debuggable.
When I deploy to my nexus 5x I get this error:
Error running AndroidLauncher: Cannot debug application from module android on device lge-nexus_5x-(code here). This application does not have the debuggable attribute enabled in its manifest. If you have manually set it in the manifest, then remove it and let the IDE automatically assign it. If you are using Gradle, make sure that your current variant is debuggable.
If I add the debuggable="true" attribute to the android tag in my manifest it works.
But I'm forgetful, so I'd rather do it the proper way with gradle.
I've tried to add a lot of the things I've found on the internet to both my Android module build.gradle, and my project's root level build.gradle.
All to no avail.
What should work?
Thanks,
Chase
On an tangent, I also tried running my HTML module using the instructions from their site, and it says it's failing because I'm using java 1.7 features but the gradle source is 1.6. I've also tried googling that to no avail. I was adding some lines like this:
sourceCompatibility 1.7
targetCompatibility 1.7
You can use build types (Build Variants) in your android gradle file:
project/android/build.gradle
buildTypes {
debug {
applicationIdSuffix ".debug"
debuggable true
}
release {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-project.txt'
minifyEnabled true
pseudoLocalesEnabled false
debuggable false
signingConfig signingConfigs.AndroidKey
}
}
* There is some proguard stuff and signing config you might not need. Just remove from the release build variant.
You don't need any other configuration in other files. You can see the full source I used in my project here.
If you are using Android Studio you can change between your build variants here:
When you run the project if release is selected it will install the non debugable apk in your device. Otherwise, if debug is selected it will install the debugable apk. Both can be installed at the same time. It will show the app twice in your device.
The problem is that it should be debugable by default. You could remove the debuggable true line and the debug build variant should still be debugable.
If it does not work paste your manifest and your build.gradle files here so we can see what is going on.
You can look all theses files in my project. It is also a libGDX project, but it don't have the HTML module.
The Build Variant docs might be helpful.
I want to compile the debug production apk with minifyEnable true,
But after config it, the test apk has no test method, because all the methods are removed by proguard.
Keeping the method in proguard.flag (with testProguardFile) takes no effect.
How to solve this problem?
Thanks!
ps:
I use gradle 2.2.1, and android gradle plugin 1.1.0
I found some resource about this problem, but without success.
https://code.google.com/p/android/issues/detail?id=159831
Minification can be enabled per build type. Simply omit minifyEnabled for build types where you don't want it (it is disabled by default), and add it to build types where you do:
buildTypes {
release {
minifyEnabled true
...
}
}
See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Types
I switched my IDE from eclipse to android studio and generated apk after some very minor changes (No class or files were added).
The APK generated from eclipse was around of size 3 MB but when I generated APK from android studio, the apk size was around 5.5 MB.
I even tried to clean and generate APK in Release mode but still APK size was same i.e. 5.5 MB.
Is there any specific reason behind this in android studio or am I missing something ?
your problem certainly have nothing to do with Android Studio or Eclipse. It's todo with Gradle and Ant. Those are the built systems used by each respectively.
On a general approach it seems to be that the Ant build was using ProGuard to remove unused resources and classes whilst your build.gradle file is not instructing Gradle to do the same.
Trying adding the minifyEnabled to your release build, like in the example below.
PS: change the name of the proguard file to the one on your project
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
I performed an upgrade to Android Studio 1.0 & Gradle 1.0.0 today, from Android Studio 0.86 & Gradle 0.13. Prior to the upgrade, I was using "runProguard = true" option in my release builds extensively, to reduce my APK size to just under 50 MB, to meet Google Play Store's APK size restrictions.
However, with the new Android Studio 1.0 / Gradle 1.0.0 with "minifyEnabled = true", I am seeing resulting APKs at 55.6 MB, which is significantly larger than before. The debug build APKs are resulting in 55.6 MB as well, leaving me to conclude that the minifyEnabled option isn't actually working.
I've updated my Gradle configuration to work with the new Gradle version and is listed below:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion '21.1.1'
defaultConfig {
minSdkVersion 9
targetSdkVersion 21
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
zipAlignEnabled true
signingConfig signingConfigs.release
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:21.+'
compile 'it.sephiroth.android.library.picasso:picasso:2.3.4.3'
}
As far as I can see, everything seems to be okay with the build.gradle file, but minifyEnabled just doesn't seem to work. What's strange is that there are no compilation errors reported when I generate the release build. Does anyone have any ideas on how to get minifyEnabled to work?
EDIT (12/10/2014):
Alright, it appears that this issue is not related to minifyEnabled at all, but to an older bug with aapt that was previously resolved with Gradle 0.9.1, where aapt would increase the size of PNG files if they were already pre-optimized.
http://tools.android.com/tech-docs/new-build-system
The bug has resurfaced with Gradle 1.0.0. This bug can be reproduced by doing the following:
Optimize PNG files with TinyPNG or other PNG optimization tools.
Use Android Studio 1.0 with Gradle 1.0.0.
Compile Android project with or without aaptOptions newPNG flag.
Open APK file and compare PNG files against original PNG files for size.
The issue has been reported on the AOSP tracker and they seem to be aware of it: https://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=tinyPNG&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=65335
FYI, this post should probably be merged with this previous StackOverflow post, as it is exactly this same old problem: Export signed app without "optimizing" png images