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
Related
I replaced some png images in my project and the app bundle did not build successfully as the png files were too large each 2-3mb. I then changed the images with some jpegs which were a few hundred kb in size per file. After that when I built the app the size of the app went from 9mb to 27 mb. I analyzed the apk and most of the size is due to some lib files
I deleted the build folder to remove any old files but it did not help.I excluded all .so files but the apk is not installing without them. I tried making a bundle but that is also the same size.
What should I do to revert back to the old apk size.
Read Add multi-density vector graphics
Android Studio includes a tool called Vector Asset Studio that helps
you add material icons and import Scalable Vector Graphic (SVG) and
Adobe Photoshop Document (PSD) files into your project as vector
drawable resources. Using vector drawables instead of bitmaps reduces
the size of your APK because the same file can be resized for
different screen densities without loss of image quality.
You should use SVG images instead of JPG/PNG.
To make your app as small as possible, you should enable shrinking in your release build to remove unused code and resources. When enabling shrinking, you also benefit from obfuscation, which shortens the names of your app’s classes and members, and optimization, which applies more aggressive strategies to further reduce the size of your app.
Read Shrink, obfuscate, and optimize your app
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'
}
}
}
I think images are not the issues from the dex files you have created I think you have included big libraries or many libraries?
Try to set minifyEnabled true to shrink resources in gradle file.
Use NDK abiFilters in your app module’s build.gradle like this:
android {
defaultConfig {
//...
ndk {
abiFilters "armeabi-v7a", "x86", "armeabi"
}
}
}
You can also exclude the specific *.so files that you don't want:
packagingOptions {
exclude 'lib/arm64-v8a/lib.so'
exclude 'lib/mips/lib.so'
}
As i need to reduce the size of APK file, I have followed Apk Expansion guide to divide APK in chunks.
The Downloader library defines ways to download the expansion file, but i need to know the way to exclude resource files and aar files from the apk.
I found following, but these are neither removing any resource-drawable files nor any arr files, and the size of apk remains same.
For testing purpose, i have added drawables of around 4 MB and couple of arr files of size 3 MB. I am creating apk from Build->Build APK option. I don't know if following will effect only on signed APK.
sourceSets {
main {
resources {
exclude '**/drawable/*'
}
}
}
android {
packagingOptions {
exclude 'lib/armeabi/a.so'
}
}
If there are specific resources you wish to keep or discard, create an XML file in your project with a <resources> tag and specify each resource to keep in the tools:keep attribute and each resource to discard in the tools:discard attribute. Both attributes accept a comma-separated list of resource names.
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="#layout/l_used*_c,#layout/l_used_a,#layout/l_used_b*"
tools:discard="#layout/unused2" />
Save this file in your project resources, for example, at res/drawable/keep.xml. The build does not package this file into your APK. This way you can customize which resources to keep.
Expansion files may not contain any executable code. This is partly a Google Play policy, but also for security. Because they are written to a directory accessible to both your app and Play, and possibly to an SD card, if you put code their it would open your app to security exploits.
Because of this, you don't want to put AAR files in expansion files, as these normally have code. And many resources might not be appropriate, as these get compiled with your app and so have resource ids etc. Instead you should split out large elements that are not part of the explicit compile. Good candidates are things like:
Open GL textures
large sound files for sound effects
large level data or maps for games
large images
All of these could potentially be in the assets directory of your app and are prime candidates for expansion files.
If you have none of the above, if you are going over 100Mb in size it is likely that you are not Proguarding your code correctly, and including a lot of code your app doesn't use. If this is the case, then learning to use Proguard correctly is probably a bigger improvement than switching to expansion files. SO users may be able to advise you more if you can say where the size in your APK is going? How much on images? How much on executable code? Are you using Android Studio and java, native code, or a technology like Unity? All of these have slightly different approaches to APK size minimization.
There are 2 things that you can do.
Firstly, you can use Lint. Lint will help to highlight and remove all the resources that you are not using in your code including the drawables.
Second you can use Proguard. Using Proguard you can choose which version of APK you want to shrink including the debug(or main, as in your example) version. Just insert the following code.
android {
buildTypes {
debug {
minifyEnabled true
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
} }
I have 3 jar files in my Android project's app/libs folder:
api-dev.jar
api-qa.jar
api-prod.jar
I want to use api-dev.jar when I work on the app in the studio (default), build a version of the app using api-qa.jar which will be tested by the QA team, then release the production app with api-prod.jar.
How should I do?
So far I read that I should add a
configurations {
qaCompile
...
}
element to app/build.gradle and use
android {
buildTypes {
...
qa {
...
}
}
}
to define the builds.
I don't know how to point to the appropriate libs/dependencies, I don't know how to make one the default one either, especially in my case where the default one is not the one for the production release...
Also if the API requires a specific key for dev, qa and prod, how do I set it up?
By the way the features are exactly the same between the different builds, the user experience is exactly the same, it's why I want to use builds, not flavors.
Check this out - Add dependency to specific productFlavor and buildType in gradle
Add you jars to the libs directory
Create a build type and product flavor
Use the name to create a specific compile command
Use that to specify your jar file in the libs directory
I have a file, res/xml/analytics_tracker.xml that stores my google analytics variables. I want to use different variables in my release vs debug builds.
So I have main/xml/analytics_tracker.xml (has maybe 6 xml-elements) & release/xml/analytics_tracker.xml (1 xml-element).
I decompiled my release apk via apktool and looked at the analytics_tracker.xml file. It only had the 1 element from the release xml file.
Shouldn't it merge the two xml files into one?
Normally if you want to have different debug and release versions of files, you would have a debug version in separate debug/.... folder, and release version in main/.... folder, at least this is how it's always in my projects. If it can's find a separate folder for a build type, it takes main/... version.
In your case I guess it found a release/.... version of your analytics_tracker file and used only it.
Here you can find the official doc about Resource Merging with gradle.
You can find the reason of your issue.
The priority order is the following:
BuildType -> Flavor -> main -> Dependencies.
This means that if a resource is declared in both the Build Type and in >main, the one from Build Type will be selected.
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