Skip to content

rozeridilar/AndroidTips

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 

Repository files navigation

Android tips & tricks ⚡️

ProGuard -- shrinking, optimization, obfuscation, and preverification of Java bytecode.

Proguard optimizes the bytecode, removes unused codes, and obfuscates the classes, fields, methods with shorter names. Optimization operates with java bytecode. Since android runs on Dalvik bytecode which is converted from java bytecode, some optimizations usually doesn't work well ( So you should be careful ).

The obfuscated code makes your APK difficult to reverse engineer, which is the main reason why most of the developers choose proguard.

Proguard can sometimes break your code up since it renames almost everything. Be sure to thoroughly test your app before release, especially after changing proguard config.

Prevent Obfuscation In Some Classes

Every app has some kind of data classes, models, or some important classes which they cannot be obfuscated. In such circumstances, we cannot let proguard to rename or remove any fields of those classes. It's a safe bet to add a @Keep annotation on the whole class or methods, or a wildcard rule on proguard-rules.pro.

1 - @Keep Annotation:

Denotes that the annotated element should not be renamed when the code is minified at build time. This is typically used on methods and classes that are accessed only via reflection so a compiler may think that the code is unused.

@Keep
  public void foo() {
      ...
  }
 

Note that: This annotation is available only when using the Annotations Support Library.

2 - Using ProGuard, keep class fields with wildcard:

-keepclassmembers class com.my.package.** {
    public protected ;
    private *** string*;
}

Tips & Tricks in Proguard

  • Proguard does not obfuscate the class names and public methods called in AndroidManifest.xml by default. So you don't have to add 'keep'in those classes.
  • When we use data classes (models) for serialization or retrieving data from JSON, we should keep our model classes
 -keep class com.xyz.mypackage.models.** { *; } 
  • Android Analyser can help you a lot while deciding which rules to add in proguard. Yet, to prevent unexpected crashes, be sure to test your release app in depth at the end.
  • To create released apk, change your directory in terminal then add the following snippet and run.
    ./gradlew assembleRelease
  • Finally If you are building library and want to change your release lib name, add the following line to your build-gradle(module: app) in android {} part.
    project.archivesBaseName = "SomeExample";
    

    Some useful Q&As:

    https://stackoverflow.com/questions/35321742/android-proguard-most-aggressive-optimizations

    https://stackoverflow.com/questions/33958972/how-to-obfuscate-everything-but-public-method-names-and-attributes-with-proguard

    https://stackoverflow.com/questions/30526173/obfuscate-private-fields-using-proguard

  • "Defines whether widgets contained in this layout are baseline-aligned or not."

    By setting android:baselineAligned="false" , app prevents the layout from aligning its children's baselines, that means app doesn't worry about where the baseline of other elements in the layout, which increases the UI performance.

    Note: By default, baselineAligned is set to true.

    Publishing Single Library on Jitpack

    JitPack is a novel package repository for JVM and Android projects. It builds Git projects on demand and provides you with ready-to-use artifacts (jar, aar).

    1. Create a new android project.
    2. Add your library by File -> New -> Import a module.
    3. Add the JitPack maven repository to the list of repositories:
    repositories {
    jcenter() maven { url "https://jitpack.io" }
    }

    Note: when using multiple repositories in build.gradle it is recommended to add JitPack at the end. Gradle will go through all repositories in order until it finds a dependency.

    1. Then publish your new project to your GitHub account. Then, push your first version tag. Note: You have to give permission from your account to jitpack. single library example

    Publishing Multiple Libraries on Jitpack

    1. Create a new android project.
    2. Add your libraries one by one by File -> New -> Import a module.
    3. Add the JitPack maven repository to the list of repositories:
    repositories {
    jcenter() maven { url "https://jitpack.io" }
    }

    Note: when using multiple repositories in build.gradle it is recommended to add JitPack at the end. Gradle will go through all repositories in order until it finds a dependency.

    1. Then publish your new project to your GitHub account. Then, push your first version tag. Note: You have to give permission from your account to jitpack. example for multiple libraries

    Important Notes 🚀 Lets assume you have module1, module2, module3 in your android project.

    To share only one module, you can add:

    implementationcom.github.yourproject:module1:module1sTAG'

    Or to share all your project directly:

    implementationcom.github.yourproject:yourproject'sTAG'

    If you are using apply plugins for such as androidanalyser, the developer who wants to use your project must embed it in her/his app. So be sure, that you embed as few plugins as possible.

    About

    A collection of Android tips & tricks that I've shared on my blog.

    Resources

    Stars

    Watchers

    Forks

    Releases

    No releases published

    Packages

    No packages published

    Contributors 2

    •  
    •