How to remove unused resources from libraries? - android

At first I know Remove all unused resources from an android project, but that is only for projects.
I want to remove unused images from a library like the Google Play Services or the Wearable SDK. I already know that I can remove unwanted languages in gradle by using resConfigs, but I don't know how to remove images and layouts which I don't use. Is there any way to avoid that they are added?

I almost missed it to write that there is now a nice solution:
android {
buildTypes {
release {
minifyEnabled true
shrinkResources true
}
}
}
This was annouced on Google+ by Tor Norbye for the build tools version 0.14.0

Probbably all you need to know about that is in this very good article from Cyril Mottier :
http://cyrilmottier.com/2014/08/26/putting-your-apks-on-diet/
Read the part about Lint ("Use Lint extensively"), as it's the tool that allow you to remove unused resources.
Hope it helps...

Related

"Resource shrinker cannot be used for libraries.", Gradle 3.0.1+ [duplicate]

When I try to shrink my Feature APK size, I use shrinkResources true in my feature Build Gradle.
However it complaints Error:Resource shrinker cannot be used for libraries.
This article ask us to use it
https://medium.com/jet-stories/make-your-app-instant-33855ab5d02b
Did I do something wrong or is this really not supported by Instant Apps?
From what I understand from the article (I've just read it quickly, still pending in my reading list), shrinkResources true is only needed to reduce APK size by removing unused resources, but it is not a formal requirement of Instant Apps. I guess the toolchain cannot decide if a resource will be unused in a library, as final APK is not built.
So, if there are no unused resources in your project, should not be a problem and could be removed.
Then, there is one thing that maybe is wrong in your project (without having tried Instant Apps by myself, just read and saw Google IO talks): from what I understand, all the modules of an instant app are APKs, not libraries. So, if you apply the shrinkResources true to the APKs, you should be good to go. If you have libraries in your project that are used to assemble the final APK modules, then remove shrinkResources true there, and just leave it in the modules.
I think this is not and AndroidStudio bug, but a Gradle bug.
I'm trying to build with Jenkins, with an updated version of Gradle (previousely used com.android.tools.build:gradle:2.2.3, upgraded it to com.android.tools.build:gradle:3.0.1) and I still get the error
"> Resource shrinker cannot be used for libraries."
With Reference to Google issue tracker
The fix will be in 3.2 canary 1 and afterwards release.
I just tried it again on 3.2 canary 3 and it was working. Are you sure you updated the plugin version to 3.2.0-alpha03? It's easy to overlook.
If any issue persists, please report at Google issue tracker they will re-open to examine.

shrinkResources true can't be used on Instant Apps Feature?

When I try to shrink my Feature APK size, I use shrinkResources true in my feature Build Gradle.
However it complaints Error:Resource shrinker cannot be used for libraries.
This article ask us to use it
https://medium.com/jet-stories/make-your-app-instant-33855ab5d02b
Did I do something wrong or is this really not supported by Instant Apps?
From what I understand from the article (I've just read it quickly, still pending in my reading list), shrinkResources true is only needed to reduce APK size by removing unused resources, but it is not a formal requirement of Instant Apps. I guess the toolchain cannot decide if a resource will be unused in a library, as final APK is not built.
So, if there are no unused resources in your project, should not be a problem and could be removed.
Then, there is one thing that maybe is wrong in your project (without having tried Instant Apps by myself, just read and saw Google IO talks): from what I understand, all the modules of an instant app are APKs, not libraries. So, if you apply the shrinkResources true to the APKs, you should be good to go. If you have libraries in your project that are used to assemble the final APK modules, then remove shrinkResources true there, and just leave it in the modules.
I think this is not and AndroidStudio bug, but a Gradle bug.
I'm trying to build with Jenkins, with an updated version of Gradle (previousely used com.android.tools.build:gradle:2.2.3, upgraded it to com.android.tools.build:gradle:3.0.1) and I still get the error
"> Resource shrinker cannot be used for libraries."
With Reference to Google issue tracker
The fix will be in 3.2 canary 1 and afterwards release.
I just tried it again on 3.2 canary 3 and it was working. Are you sure you updated the plugin version to 3.2.0-alpha03? It's easy to overlook.
If any issue persists, please report at Google issue tracker they will re-open to examine.

How to enable ProGuard inside ADT

As my Android project getting bigger now I am facing the 64k references limit issue. I read the Configure Apps with Over 64K Methods
article, and it suggests me to reduce the methods count before diving into multidex solution. Now I successfully decreased about 12000 of methods count.
But I still have a problem that is the ProGuard can only take effect when exporing a signed App for release, but not for development. With this fact, I am afraid that I cannot put more code into Eclipse for development, because when I add more methods then I click ADT > Run, I will get the 64k limit error again. I don't want to keep exporting and checking the result during development that is really time wasted.
Will there be any solution for this? Thanks a lot for any advice!
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
This minifyEnable true. will make proguard enabled. but this is in Android studio.
I think you are using Eclipse, which google has deprecated .

Useless resources in APK file

I've got problem with very simple application. APK size is now 3 MB, but it contains a lot of useless for me files (I think that source of this files is Support Library). In my application I don't use any images, but all drawable directories contains a lot of icons, buttons, etc. Is it possible to delete this images by any rule in gradle or other method? I use Android Studio.
Already I added to build.gradle information about languages to include in APK. I had in Hello World 80 languages before it.
Screen of files:
The Gradle build system for Android supports "resource shrinking": the automatic removal of resources that are unused, at build time, in the packaged app. In addition to removing resources in your project that are not actually needed at runtime, this also removes resources from libraries you are depending on if they are not actually needed by your application.
To enable this add the line shrinkResources true in your gradle file.
android {
...
buildTypes {
release {
shrinkResources true
}
}
}
Check the official documentation here,
http://tools.android.com/tech-docs/new-build-system/resource-shrinking

The Apk file exported from Android Studio is too large

I have never used Android Studio before. The size of a simple "Hello world" app generated by default is almost 800k, while it is less than 100k in eclipse. I find that there are too many pictures in the drawable files, they seem come from res/all under the build folder, they still exist even if I delete the folder res manually.
My question is how to shrink the apk size in this situation?
The difference is due to the appcompat library which is compiled in by default to New Project Wizard-generated projects in Android Studio if you target an API level lower than 14. When I ran a test, the size increase was 642k for a debug APK, and 411k for a release APK (with Proguard enabled -- that will strip out unused code but not unused resources).
There's not a lot you can really do about this, short of removing the appcompat library if you're not using any of its features (though you should probably be using its features).
I haven't used Android Studio yet (my understanding is that it's still beta), but, I know that with other IDEs, the size of the resulting executable will change based on if it's a Debug or Release version. Eclipse lets you specify one or the other. Does Android Studio do the same?
Try to use "shrinkResources" in (build.gradle app)
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
https://developer.android.com/topic/performance/reduce-apk-size

Categories

Resources