loading...
Cover image for Develop & Publish your own SDK in Android - Part 2(Getting started with SDK development)

Develop & Publish your own SDK in Android - Part 2(Getting started with SDK development)

mohitrajput987 profile image Mohit Rajput Updated on ・3 min read

In the previous part, we learned about the structure of an AAR file and gradle dependency.
In this tutorial, you will learn-

  • How to develop a new module/library
  • How to obfuscate a module

If you haven't read the previous article, you can read it from here. If you have already read it or have sound knowledge of AAR file and gradle dependency, you can start developing your SDK with the help of this tutorial.

Development

Create a new Android library project with the following steps in Android Studio-

  1. Create a new Android project or use an existing one in which you will create a module. To create a new module, go to the File → New → New Module. You will see the following dialog- Create new module

Select Android Library and click Next.

  1. After clicking the Next button, you will see a small dialog which will ask some details about library- Fill Details

Fill library name, module name, and package name. Select minimum SDK version supported and click on Finish. You will see newly created module added in the project as follows-
New module created

  1. Add following line in your app level build.gradle to include newly created module in your project-
implementation project(":preference")

Replace preference with your module name.

There are a number of architectures for building an Android application i.e. MVP, MVVM etc. But when you don’t have any view, choosing these architectures would not be a good idea. There are different kinds of libraries-

  • Some libraries contain view and UI elements i.e. Facebook, Google Maps;
  • Sometimes your library is the abstraction of REST APIs i.e. Microsoft Cognitive SDK;
  • Sometimes you are providing widgets or custom views to prettify user’s applications i.e. Android Support Libraries, design library etc.

One more challenge while development is taking API Key from the user to authenticate the SDK. You also need to notify the user if it is a correct key or not. Google takes API key in Manifest, whereas Microsoft Cognitive SDK takes it in the constructor of its REST client.
To be precise, the decision of good architecture, API key management, exception handling will depend on your requirements.

Obfuscation

Proguard is used to shrink, obfuscate, optimize code. Proguard is necessary for your library to remove unused code and make reverse engineering little difficult.
Proguard rules for the library are different from the normal applications. As you know, Proguard renames classes, variables, and methods using meaningless names. You would like to keep the names of those methods and classes as it is that developers will call. You will need to test and verify obfuscated code from generated AAR file.

  1. In your project explorer, you will see two proguard files, one for the “app” module and another for the new library module you created now (in our case, it’s “preference”)-
    Proguard File

  2. Copy and paste the content of this Proguard File which has library specific proguard rules. You can customized rules according to your need.

  3. Now open build.gradle file of module and enable proguard as follows-

buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

Now sync the project and you can use any method or classes from this library module in your main project.
So finally we finished the development and obfuscation of our library.
Now, the next step is to create gradle dependency of this library which we will see in the PART-3 of this article which is the final part of this series.
Please see How to create gradle dependency.

Posted on by:

mohitrajput987 profile

Mohit Rajput

@mohitrajput987

Writing quality code and learning architectures are the hobby of Mohit. He is mainly working on Mobile technologies i.e. Android native apps, Java, Kotlin etc. He loves to write blogs.

Discussion

markdown guide
 

Thanks,

This post helped me a lot.

 

Thanks Joaoclobocar. Keep reading.