Exclude xml files from compilation android [resource shrinking] - android

I would like to use some xml files as mockups to preview different states of a layout, but I don't want to include it in the apk because thes xml files are only dedicated to preview purposes. I would like to tell gradle which files/directories to ignore for resources.
It would also be useful to easily reduce apk size for low memory devices by using products flavors, in a similar way as this excellent article explains.
Maybe proguard could help?

If you are using the android studio you can add resources by build flavors your want example you can add string resource for debug build only and different string resource for main release build. You just right click add a xml resource choose a resource type and specify the source set.
if you add a layout for debug flavor only and not in main release then everytime you sign an apk the those layout will not be included in apk. Hope it helps :)

We call it "Resource shrinking". All info about it is here.
Look at shrinkResources true. Build system will try to find unused resources files with this flag, and will remove them from the build. You can add it to your debug buildType, and on any build flavor.
android {
...
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

Related

How can I get rid of unnecessary code in .apk file?

I have an empty project (no classes whatsoever, no activities), only a dependency to com.google.android.material:material:1.3.0 for the code to compile (there is a style defined which uses this).
I enabled shrinking option, yet, after generating the signes APK, there is a classes.dex file in the apk with a shitload of code, even though the app has no code. Why and how to I get rid of those, to make sure the apk contains only what is needed, no extra bloatware? Thank you.
This is the expanded apk:
For code shrinking please turn on Proguard and you have the option to customized Proguard rules as you need.
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
}
}
TRY 1
Open .jar file (appodeal.jar) and remove all .dex files.
TRY 2
Use the built-in inspection Java | Declaration redundancy | Unused declaration.
To run it on whole project go to Analyze -> Run inspection by name..., type Unused declaration and select desired scope. Then carefully check output and mark some classes as entry points if needed.
Select Unused declaration node in list and perform Safe delete action on all unused declarations at once.

Android Studio APK Analyzer not show details result

I want to optimize my app size.For that,I want to analyze my app with android studio analyze apk tool.But it only show the file size not details report.
Please see https://medium.com/#arungiri.10/managing-application-size-50810d03b16c for detailed info.
Use below lines to enable app shrinking using R8.
buildTypes {
...
release {
...
useProguard false
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
...
}
}
where proguard-rules.pro must be written by you based on your app. Generally this file contains lines of code to not obfuscate data / model classes.
Once you do this, a APK will be generated in the output/release directory. Use that APK in APK analyzer tool.
As we can see the data present inside a APK below.
If you click on classes.dex, it would show the classes present inside this dex file.
As you can see, the code is proguarded, you will also get option to add respective mapping.txt file which would help in reverse engineering and see the APK package data.
mapping.txt file is auto-generated and is present under outputs/mapping/release/. Use this to reverse engineer and see the actual class names.
Also, to add proguard rules for data classes, it's easy. Just select the class or package you want to keep from proguarding, right click on it and select "Generate Proguard Keep rule" as shown below.
Proguard rules will be auto generated, copy this and put it in above proguard-rules.pro file.
Hope this answer helps.

How to make Proguard obfuscate using Android Studio

ProGuard is not doing a very complete job of obfuscating my Android project. When I decompile my apk at decompileandroid.com I see that it is only changing the name of local temporary variables and nothing else. It is not changing class names, variable names, method names, or anything else.
After reading the manual all of the optional commands seem to be telling it NOT to do something so I am left to think it should obfuscate everything by default.
my build.gradle has the following...
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
Neither of those two files exist is the project. The project is as it was converted from Eclipse by Android Studio.
What am I missing. Do I need to create those two files and put some proguard parameters in them - if so what. I want maximum obfuscation.
Thanks,
Dean
Set minifyEnabled true in order to turn on code shrinking, and
then shrinkResources true to turn on resource shrinking.
Reference for the quote

How to reduce and compress an apk file in android

I have one apk file but its file size is 22.4 MB. It's huge, so I need to reduce or compress the apk file in android. What is the right way to reduce and compress the apk file size?
You can compress an APK file (rar, zip), but it has to be decompressed in order to work.
If the apk file to large to distribute you can:
Use expansion files: http://developer.android.com/google/play/expansion-files.html
Create multiple versions of your apk: http://developer.android.com/google/play/publishing/multiple-apks.html (e.g. no hdpi files for ldpi devices)
Use ProGuard
ProGuard is a free Java class file shrinker, optimizer, obfuscator,
and preverifier. It detects and removes unused classes, fields,
methods, and attributes. It optimizes bytecode and removes unused
instructions. It renames the remaining classes, fields, and methods
using short meaningless names. Finally, it preverifies the processed
code for Java 6 or higher, or for Java Micro Edition.
If you use Eclipse, A default configuration file will automatically be added to your Project. But this default configuration only covers general cases, so you most likely have to edit it for your own needs.
Remove unused resources: https://code.google.com/p/android-unused-resources/
Optimize your images by using tools like 9patch and png optimizers
Remove everything that is only used for debugging purposes (debug classes, or even Log() methods). Also try removing unnecessary .so files
You can ZipAlign the signed apk to compress it.
Usage:
zipalign [-f] [-v] <alignment> infile.apk outfile.apk
Example:
D:\android-sdk\android-sdk\tools>zipalign -f -v 4 "C:\Users\Joisar\Desktop\project_name\appname_signed.apk" "C:\Users\Joisar\Desktop\project_name\appname__zipaligned.apk"
Note:
Kindly checkout the apk whether it is zipaligned or not by following command, if it's not zipaligned, then do it.
zipalign -c -v 4 "C:\Users\Joisar\Desktop\project_name\appname_signed.apk"
Find the problem
You can Analyse APK from Android Studio. This shows you the various files in their directories and their sizes, both absolute and relative to the whole APK:
Check for Unused Resources
This is the easiest: shows you, among others, the unused resources:
Just remove them from your project.
Use Vector Images
Vector Drawables provide one sharp image for all resolutions, greatly reducing the size for your graphics. As usually, you can use these for icons etc, but not for Photos. Non-vectorized images can be reduced with trimage.
proguard minify
Add the following to app/build.gradle to enable proguard to reduce unused classes from your project:
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Use .svg format icon set.
Compress PNGs.
Use only specific libraries of Google Play Services.
Use Proguard
build.gradle
android {
...
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
Shrink Resources
build.gradle
android {
...
buildTypes {
release {
shrinkResources true
minifyEnabled true
...
}
}
}

Android : Automatically remove unused images from project bundle

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

Categories

Resources