Remove unused resources using Android Studio? - android-studio

I want to remove unused resources from my project to reduce the app size. Is there any way to do it by using Android Studio IDE efficiently?

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.
For example, your application is using Google Play Services to for example access Google Drive functionality, and you are not currently using Google Sign In, then this would remove the various drawable assets for the Sign In buttons.
Note: Resource Shrinking only works in conjunction with code shrinking (such as ProGuard). That's how it can remove unused resources from libraries; normally, all resources in a library are used, and it is only when we remove unused code that it becomes apparent which resources are referenced from the remaining code.
To enable resource shrinking, update your build type as follows:
android {
...
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
And google recently launched Android Studio 2.0 officially, Now they are giving an option in the IDE itself.
Right click on app --> Refactor --> Remove Unused Resources
It will prompt
Check the box prior confirm action so that you can get rid of unused #id declarations too.
In terms of APK optimization consider Selecting a Format fact as well.
Use WebP Images provide better compression than either JPEG or PNG. Lossy WebP images are supported in Android 4.0 (API level 14) and higher, and lossless and transparent WebP images are supported in Android 4.3 (API level 18) and higher.

In android studio. You can use Android Lint. It will show " Strings, Resource, import.." not use
Analyze -> Inspect Code -> Whole Project -> OK

Related

How to use profileable process in Android Studio Profiler?

I have downloaded a performance-samples project from Github (https://github.com/android/performance-samples/tree/main/JankStatsSample) but do not see a (profileable) process, as shown in below image. Mine is only showing (debuggable). I am using Pixel 5 API 31 with latest version of android (Chipmunk). I have followed the steps on Android Devs website below but still no luck. What am I missing?
https://developer.android.com/studio/profile#profileable-apps
You've built the debug version of the app, therefore you can profile it without it being profileable. You can simply select the debuggable process if you want to profile it.
Profileable is useful when you want to build a release (not debug) version of the app and still profile it (to more accurately check the performance of your app). In case the profileable tag is added to the AndroidManifest.xml, all you need to do is build the release version of the app and it will show as profileable.

Importing LIBGDX library gdx-backend-android.jar into Android Studio Project

After a hiatus of a couple of years I'm picking up Android development again.
I installed the newest Android Studio(4.1.1) with the latest Android SDK version (Android 11, API 30). After that I created a new project with gdx-setup.
If I add the old java source to my newly generated project I get this error:
error: package com.badlogic.gdx.backends.android does not exist
I'm not sure how to add this jar into the new project. In the Gradle configuration I see mentions of the backend, but it's not available.
I also downloaded the 'gdx-backend-android.jar' from the nightly build and put the jar in the Android library folder, all to no avail.
Does anyone actually know how to correctly add this dependency into my project?
I added the jar but still have an error, don't mind the other errors, I first need to fix the GDX import.
The project dependencies are managed by Gradle, so there is no need for you to directly touch any .jar files at all.
The most likely issue you're facing is that you are trying to use Android-specific classes from the core module, which is platform agnostic.
In a typical libGDX project, you do almost all your game code in the core module so it can easily be compiled for any platform. The code you showed above would be in the android module, but your LiveWallpaperStarter class would be part of core.
Some might say there's no reason to use core at all if you're making a Live Wallpaper, since it can't run on any other platforms besides Android. But there is some advantage in keeping the rendering in core so you can test in a desktop game window, because you can more rapidly compile and run on the desktop. This library has some tools that make it easy to wrap your rendering code in a class that lets you simulate a live wallpaper on desktop, for testing.

Can enabling android proguard prevent people decompile my code?

I have an android app, and it seems someone decomiple my code and uploaded to the play store. In my app, I have something that makes a call to my website through a php file like the following
http//www.test.com/get_service.php
Its hardcoded in the app as a string. That guy is making the same call using my get_service.php because whenever I change something in the get_service.php, it would reflected on their app.
I just noticed that apps like https://play.google.com/store/apps/details?id=com.njlabs.showjava&hl=en
is able to decompile my android app.
So I tried enabling the Proguard on my app, and test it on my own phone. I haven't release the app yet, but it seems the app is still able to decompile my code. Is there a way to hide my string? so that even if someone decompile my code, he won't be able to see the get_service.php file.
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'
}
}
...
}

Can I generate an unsigned APK using dexguard?

My question is quite simple but it seems I cannot find the answer on the web and since dexGuard does not have a trial period I cannot test it myself.
Can I generate an unsigend apk obfuscated/encrypted with dexguard in Android Studio. I've been told that is not possible because the signature is used during the obfuscation/encryption process but I feel like I'm missing something.
I would like to know if I can obtain the same result I obtain with "assemble release" gradle task with proguard enabled but using dexguard instead for a better protection.
Thank you for your help
Dexguard is a better version than Proguard so it has all the functionalities that has its little brother. From the Dexguard docs:
The procedure for building Android applications and libraries remains
the same. You can invoke gradle with the usual targets, such as
assemble, build, install, and connectedInstrumentTest. For instance,
to build the release version of your application and install it on a
connected device:
gradle installRelease
To build the release version of a library:
gradle assembleRelease
Debug builds use debug settings, without
optimization or obfuscation. Release builds use release settings, with
full optimization and obfuscation. Applications can optionally be
signed. The entries in application archives are always zip-aligned for
efficiency.
Additionally you can use the Standalone version which makes DexGuard run from the command line. By using this you can post-process an existing Android application (.apk file).
I only use Dexguard to obfuscate a Library in Standalone mode but I am pretty sure it will work.

How to add Storage reference?

This should be pretty straightforward, but can't work this out for myself I'm afraid!
The following line of code triggers the error:
using Microsoft.Xna.Framework.Storage;
Error: The type or namespace name 'Storage' does not exist in the
namespace 'Microsoft.Xna.Framework' (are you missing an assembly
reference?)
But as far as I can tell, I have added the reference: (Solution explorer -> right click References -> Add reference -> .NET tab -> scroll down to Microsoft.Xna.Framework.Storage -> select it -> click OK).
Microsoft.Xna.Framework.Storage shows up when I expand "references" in the solution explorer.
What am I missing?
EDIT
Okay on second look this might be to do with how I'm adding the references. When I search in the object browser Microsoft.Xna.Framework.Storage doesn't show up (but when I search, for example, for audio, Microsoft.Xna.Framework.Audio does show up). Is there anyway to check that when I click "OK" on the add reference pop-up that it is actually working?
I see two likely possibilities.
You have a mismatch of XNA 4.0 and XNA 3.1 (or previous) references in your project. If the Storage reference you added is from 3.1, you'll get this message. You can try removing it, and then looking specifically for the 4.0 version during add.
The device you are building for may not support the Storage mechanism. In particular, Windows Phone apparently uses a different storage mechanism (System.IO.IsolatedStorage http://msdn.microsoft.com/en-us/library/ff604992.aspx).
Hope one of these helps.
This question was asked a long time ago, but if anyone else is still looking for an answer, here is how i fixed it.
If you installed the Microsoft.Xna.Framework references by using the installer from Microsoft, then it installs in C:\Windows\Microsoft.NET\assembly. I found all of the Xna libraries in GAC_32 and Microsoft.Xna.Framework.Storage in GAC_MSIL.
You can also use the GAC to find libraries that aren't in Visual Studio's reference list. For .NET Framework versions before 4.0, the libraries are in C:\Windows\assembly, and the libraries for versions after 4.0 are in C:\Windows\Microsoft.NET\assembly.
It's actually been removed from Monogame by Microsoft.
Running: Windows 10, with integrated graphics card (Intel G45/G43), Visual Studio 19, Monogame 3.7.1
Project Structure: Solution > Game.Shared, Game.Android, Game.OpenGL (followed this guide on creating a Cross-Platform project, method number 3).
I changed the Target Framework in my OpenGL project from .NET 4.5 to .NET 4.7.x (the newest version), and I was able to build and deploy the app to an Android device. Do this by clicking the project and selecting Properties.
I simply commented out the line. App will successfully deploy to an Android device this way. The game will then deploy, but I can't get any content to load.
Have yet to see if I can deploy to an iOS device, I will update this post when I figure out if I can.
No nuGet's installed.

Resources