Develop & Publish your own SDK in Android - Part 2(Getting started with SDK development)
Tags: sdk, android, obfuscation, androidstudio
Published: 2019-01-27 14:00:23

---
title: Develop & Publish your own SDK in Android - Part 2(Getting started with SDK development)
published: true
description: Develop an Android SDK using Android Studio
cover_image: https://thepracticaldev.s3.amazonaws.com/i/a3fcuizve1znyo0s8lxw.png
tags: sdk, android, obfuscation, android-studio
---

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](https://dev.to/mohitrajput987/develop--publish-your-own-sdk-in-android---part-1know-an-aar-file-g9k). 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](https://raw.githubusercontent.com/mohitrajput987/media-repository/master/sdk-development/development/Create-new-module.png)

Select **Android Library** and click **Next**.

2. After clicking the Next button, you will see a small dialog which will ask some details about library-
![Fill Details](https://raw.githubusercontent.com/mohitrajput987/media-repository/master/sdk-development/development/fill-details.png)

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](https://raw.githubusercontent.com/mohitrajput987/media-repository/master/sdk-development/development/created-module.png)

3. Add following line in your app level `build.gradle` to include newly created module in your project-

```gradle
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](https://raw.githubusercontent.com/mohitrajput987/media-repository/master/sdk-development/development/proguard-location.png)

2. Copy and paste the content of this [Proguard File](https://github.com/mohitrajput987/android-utility/blob/master/preference/proguard-rules.pro) 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-
```gradle
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](https://dev.to/mohitrajput987/develop--publish-your-own-sdk-in-android---part-3create-gradle-dependency-4nam) of this article which is the final part of this series.
Please see [How to create gradle dependency](https://dev.to/mohitrajput987/develop--publish-your-own-sdk-in-android---part-3create-gradle-dependency-4nam).