I am trying to view APK contents using Android Studio's APK analyzer tool.
According to https://developer.android.com/studio/build/apk-analyzer, APK would contain dex files from which class information can be viewed.
With my sample application, I am able to see .java classes directly in the APK. Refer APK files.
I want to avoid having the JAVA classes as part of APK to reduce APK size and for security reasons.
Use like this in your build.gradle -
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'
}
}
...
}
Do read google Docs for better understanding. You will get a clear picture.
Follow this -
https://developer.android.com/studio/build/shrink-code
Related
I have created a Wear OS module in our existing Android mobile application. Both application modules remain in the project like following:
Project/app
Project/wear/wear_presentation
Project/otherLibModule/other_lib_module_presentation
and we have a common.gradle that is used by app and wear_presentation:
buildTypes {
release {
minifyEnabled true
debuggable false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
minifyEnabled false
}
}
Before Wear OS module, we had a single proguard file inside app module that handles all minifying work, but now we have 2 application modules that have separate proguard files. That forces me to copy/paste all content from app proguard file into wear_presentation proguard file and this approach seems amateurish.
I want to use a single common proguard file that is used by both app and wear_presentation modules.
Is it possible?
Importing from Libraries is supported already.
https://developer.android.com/studio/build/shrink-code#configuration-files
/proguard.txt - If an AAR library is published with its own ProGuard rules file, and you include that AAR as a compile-time dependency, R8 automatically applies its rules when compiling your project.
Using rules files that are packaged with AAR libraries is useful if certain keep rules are required for the library to function properly—that is, the library developer has performed the troubleshooting steps for you.
However, you should be aware that, because ProGuard rules are additive, certain rules that an AAR library dependency includes cannot be removed and might impact the compilation of other parts of your app. For example, if a library includes a rule to disable code optimizations, that rule disables optimizations for your entire project.
/META-INF/proguard/ - for JAR libraries
It's commonly used by libraries like OkHttp https://github.com/square/okhttp/blob/okhttp_4.10.x/okhttp/src/main/resources/META-INF/proguard/okhttp3.pro
I was reading an article about Proguard in Android Studio their is a line
Proguard comes out of the box in android Studio
Can anyone please explain this to me? If you are interested to read the completed article I'll put the link below.
Article Link
It means that proguard is bundled together with your default android project and you do not need to download it from outside via gradle or maven
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.txt'),
'proguard-rules.pro'
}
}
...
}
Ref:More on proguard usage in android
Since the 3.5 update of Android Studio, I have this warning when building my app :
DSL element 'useProguard' is obsolete and will be removed soon. Use
'android.enableR8' in gradle.properties to switch between R8 and
Proguard..
Removing "useProguard" from build.gradle fixed the problem for me, like:
release {
minifyEnabled true
//useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
Update 2022, or Details; R8 is nowadays:
By default enabled,
And it simply replaces ProGuard,
But supports existing .pro files (and there should be no need to reconfigure).
Also, any way to disable R8, or use ProGuard instead, is deprecated (or even removed).
But debug flavors can set debuggable true, and continue to be debuggable line-by-line.
set the following in your project's gradle.properties file
android.enableR8=true
R8 also has full mode that is not directly compatible with Proguard. In order to try that out you can additionally set the following in your gradle.properties file
android.enableR8.fullMode=true
This turns on more optimizations, that can further reduce app size. However, you might need a few extra keep rules to make it work.
At a glance, when you build you project using Android Gradle plugin 3.4.0 or higher, the plugin no longer uses ProGuard to perform compile-time code optimization. Instead, the plugin works with the R8 compiler by default to handle the Shrink, obfuscate, and optimize your app. However, you can disable certain tasks or customize R8’s behavior through ProGuard rules files.
In fact, R8 works with all of your existing ProGuard rules files, so updating the Android Gradle plugin to use R8 should not require you to change your existing rules.
When you use Android Studio 3.4 or Android Gradle plugin 3.4.0 and higher, R8 is the default compiler that converts your project’s Java bytecode into the DEX format that runs on the Android platform. However, when you create a new project using Android Studio, shrinking, obfuscation, and code optimization is not enabled by default. You may enable them using the below code -
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'
}
}
...
}
For the more adventurous, R8 also has full mode. In order to try that out you can additionally set the following in your gradle.properties file.3
android.enableR8.fullMode=true
This turns on more optimizations, that can further reduce app size. However, you might need a few extra keep rules to make it work. Learn more here - https://youtu.be/uQ_yK8kRCaA
R8 is the default tool available in Android Studio 3.4 onwards. There is no need to explicitly enable R8. Just remove the useProguard true line from the app/build.gradle file.
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
I have a library module in a gradle project that has connected Android tests, but the test APK references too many methods and needs to be multidexed or ProGuard should be enabled.
Is there a way to enable multidex or ProGuard just for the connected Android test's application?
It doesn't make sense to enable ProGuard directly on a library, but if there is a way to enable it only for the androidTest configuration, that would work nicely.
We do have ProGuard enabled for the application module, so it can safely depend on this library module and successfully build the app's APK.
It's been difficult to search for solutions to this question since I can only find information about using the Multidex support library. I understand how to enable it for a typical application.
Is there a way to enable multidex or ProGuard just for the connected Android test's application?
Yes, you can enable ProGuard only for tests using a dedicated test build type. The default one is debug.
In the follow example, the dedicated test build type is named minifiedTest.
android {
defaultConfig {
/* Your configs here. */
// Specify the name of the dedicated test build type.
testBuildType 'minifiedTest'
}
buildTypes {
debug {
// Your debug configurations.
}
release {
// Your release configurations.
}
minifiedTest {
// Use this to get the initial configurations from another build type.
// Some of them will be overridden from the configurations specified in this build type.
// You can avoid to use this or you can get them from your release build type for example.
initWith(debug)
// Enable proguard.
minifyEnabled true
// Specify the proguard file.
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
My assets and drawable are only 2mb java and xml sources is only 1mb but after build project the apk size is 20mb!
I set shrinkResources true
and remove unused resources and generate app with proguard.
Is there a way to reduce the size of apk?
Android Studio has its own apk analyzer which is very useful for cases like yours.
Analyze your apk file and check which files are using this much space.
https://developer.android.com/studio/build/apk-analyzer.html
Also using ProGuard helps to reduce apk size.
Additionally, avoid using unnecessary libraries. For example,
if you need to use Google Analytics, import gradle only analytics library like this:
compile 'com.google.android.gms:play-services-analytics:10.2.4'
do not use like this:
compile 'com.google.android.gms:play-services:10.2.4'
second example uses too much space and redundant classes and files.
at your gradle, normally debug apk will be larger than release APK about 50%. If you care about the debug size, just do the same config like release on debug config
buildTypes {
release {
minifyEnabled true <-- minify your code
shrinkResources true <-- remove any unused resources
zipAlignEnabled true <-- optimization
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' <-- enable proguard is important to shrink byte code
}
}
Second, if you are using google services dependencies, please use individual dependencies.. refer here In android studio 2.2 and above they have added apk analyser tool in Build menu. Use that to analyse APK.
Not only for google services, others library also. some library put android design or appcompat in their library. so you need to exclude those module (if you already have in your dependencies)
According to the "Resource Shrinking" webpage of Andriod documentations (here), you can minimize the app's size via the build.gradle file, by using these lines:
android {
...
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
You can do following things
Remove non required libraries. even use required libraries (like map
or gcm individual instead of full play service library)
Use vector images instead of multiple png.
Use zipAlignEnabled command in build file
Check my blog Different ways to reduce apk size
Main Points are :
android.enableR8=true // enable New R8 Code Shrinker
minifyEnabled true && Add Proguard Rules
Examine Your APK Using Android Studio’s APK Analyzer
Enable Resource Shrinking
Convert Your PNGs, JPEGs, and BMPs Into WebP
Avoid enumerations and use #IntDef annotation
Use aaptOptions { cruncherEnabled = false } to compress images
Use Remove Unused Resources android studio built-in function to remove all the unused resources in the application
Use Code Cleanup android studio built-in function to remove
Note: Go enable it! (*just double and triple check everything works afterwards)