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

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.

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.

Check if Library is used in Android app

I received some legacy code of app (not developed by me, but by some other team, with no documentation), which has almost 20+ dependencies, in build.gradle.
Now, I wanted to clean up unused Libraries/dependencies, by removing them from build.gradle
I searched on Google and came across this project for resource shrinking. But it seems to be used for removal of resources that are unused, at build time, in the packaged app and this also removes resources from libraries you are depending on if they are not actually needed by your application.
Also, I use ProGuard, for obfuscation and shrinking in conjunction with shrinkResources true in build.gradle
My intention is to remove unused Libraries/dependencies from build.gradle itself, without breaking app functionality.
Is there a way or tool which shows which library is safe to remove without breaking the app functionality?
By 20+ dependencies you don't need any tooling and can do a manual check.
I would proceed like this:
Comment out all dependencies and check what fails (see below)
Uncomment the dependency that causes the failure
Repeat
This way you might also notice dependencies that are seldom used or can be replaced with standard libraries or other libraries that you use in the project.
Here are the things that will indicate you that a dependency is required (in the order of slowing down the feedback loop):
compilation errors
unit test errors
integration / system / end-to-end / device test errors (whatever you use and call them)
application functionality at runtime
application performance at runtime
Runtime dependencies can be especially tricky. For example, your code might not depend on a library, but this library provides a runtime implementation for some other library you depend on. Removing such a dependency will only be visible at runtime as missing functionality or performance issues.
Instead of commenting out all dependencies I would go the other way around - comment out one dependency at a time and see what breaks. This way you would also get a grasp of use-cases of all dependencies because the IDE will point you to the place where code broke. If nothing breaks after commenting out a dependency you'll know that it's not used. Another thing you could potentially do is analyze an unobfuscated release .apk where all unused dependencies will be missing but package structure will be preserved.
If you mean that finding unused library or import, you can easily see with "Ctrl + alt + shift + i" and type "unused import"
You can see now all unused imports.
Finding libraries and resources used in an Android app comes up in several contexts.
For the apps published in Google Play, AppBrain maintains reverse lookups, from the library to the more popular apps that use it. For example, apps using a newish 2D game library Godot.
Apktool will decode the APK directly.
The author instead wants to find (unused) resources, starting from the source code and the build process. Gabriele Mariotti above links to the question, whose accepted answer provides detailed information on use of minifyEnabled and shrinkResources in Gradle configuration.
Review Shrinking Android app and ProGuard vs R8.

How to remove unused resources from libraries?

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...

How to have diifferent build configuration for Android Project?

I am having two versions of my android project (release and debug). They both are sharing the same source files. I want the debug version to be intact when we checkin any changes for release build.
It is not working as we cant have 2 different manifest files for it and if we make change in manifest, it will affect both the projects and keep them out of sync.
Is there any way we can have different build configurations for same project?
Please advise.
Thanks
If you don't want to impact the debug app when changing the release source files you'll have to use different source files. Having different build files or configuration will not help.
Gradle apps use at least 3 source folders.
src/main/... is used by all variants
src/debug/... is used by the debug variant
src/release/... is used by the release variant
To do a change that only impact the release variant, just edit code in src/release/.... This can contain a manifest, res, java code, etc...
That said I'm not why you don't want to change the debug version when changing the release version. The whole point of the debug version is to be the same as the release, except debuggable. The different source folders above should only be used for minor things (like enabling/disabling log output for instance). Making both versions different in bigger ways is not recommended.

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