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
Related
I have read the article. The author think every Android app should use code shrinking.
I use the following code to shrink and obfuscate code, proguard-rules.pro is original and blank, and proguard-android-optimize.txt is original. it's default made by Android Studio.
You know that some project can work well in Android Studio but failed after publish to Google Play, you can see the article.
When an app run in Android Studio, I think ProGuard doesn't work and it doesn't shrink and obfuscate code, so the app works well in Android Studio.
When I generate .aab file for publish in Android Studio, the ProGuard will shrink and obfuscate code, but it maybe cause runtime error due to incorrectly shrink and obfuscate operation.
How can I test if ProGuard works correctly before I publish an app to Google Play ?
buildTypes {
release {
minifyEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
}
}
Add content:
To Ryan M: Thanks!
It seems that I can test whether ProGuard works correctly in Android Studio by the article.
You can see Code A and Image A.
Is that right?
Code A
debugMini {
initWith debug
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
matchingFallbacks = ['debug']
}
Image A
Install and run the minified release version of your app (see here or here for info on installing AAB files) that you upload to Google Play, not the debug version.
If you're just hitting "Run" in Studio, you're installing the debug version that doesn't (by default) have Proguard or other minification run on it. If you instead use the minified release version before uploading it to Google Play, you'll get the same behavior you will after uploading: Google Play isn't running any "extra" Proguard tasks on it after you upload.
You can also use the Alpha/Beta testing tracks in Play to test the full Play experience without publishing to a wider audience or fiddling with bundletool.
There are several ways to confirm you code is being properly minimized. One is to check the youappmodule/build/outputs/mapping/release/mapping.txt file. This will include lines like
com.example.somepackage.SomeClass -> a.b.c.d:
If you see that, you know which classes are being properly obfuscated. You can also find out which classes have been removed by making sure there is no such entry for that class.
Another good way is to inspect the output APK in Android Studio. In Android Studio 4.1 you can do this by going to Build > Analyze APK and then selecting your APK that should have had Proguard run with it. You can then inspect the classes.dex file and check its contents. You can see which classes have been obfuscated and removed by directly traversing the file structure.
I think the easiest way to test it is to build your app in the release version and install it on your device.
This is the simplest way.
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.
My aim is to be able to distribute the android library application only as binary(WITHOUT SOURCE) to other developers.
So that they can make their own apps with it.
I am developing two android applications. The first one is library application should act as a kind of SDK.
The second one application should use the library application and make the original app.
I am using android studio to develop the applications. I have completed the implementations of the library application and generated the .aar.
Now I am using my own SDK(.aar) to build the second application. I can use the .aar in my second application.
The problem is that, android studio can decompile my SDK(.aar). But I don't want to share the source code.
Note: My librarySDK doesn't contain any UI elements such as Activity, Resources.
Is there any way to create the SDK(.aar) without source code?
yup for that you need to use proguard when you build SDK/Library aar file
Change minifyEnabled to true in your SDK/Library gradle
buildTypes {
release {
minifyEnabled true //enable proguard
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
Enabling ProGuard in Eclipse for Android
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
I am developing an android project that is pretty big and have many images in drawable folder. Many of these images are not being used in the project (because they have been replaced) and they consume valuable space. Is there any way that I could automatically find those images and delete them, instead of searching through each image in the project?
Some tools I have used are
android unused resources
and
android lint
One of the features does say
Unused resources
We open sources a tool that removes all unused resources automatically from your project based on android lint output.
Even unused strings and other 'inline' resources.
https://github.com/KeepSafe/android-resource-remover
Now we can do it automatically with gradle
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
More details are here
https://sites.google.com/a/android.com/tools/tech-docs/new-build-system/resource-shrinking