DEV Community

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

Posted on • Updated on

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

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.

Top comments (15)

Collapse
 
hadimousavi79 profile image
hadimousavi79

Hi dear @mohitrajput987
Can develop own SDK for Android in other language such as c++ and python?
If your answer is yes can you say how can develop in other language with example and short tutorial from scratch?
please

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Hi, yes
In many android projects, you can see .so libraries which are developed in c++.
For that you need NDK.
You can find many ndk tutorials in internet.

Collapse
 
hadimousavi79 profile image
hadimousavi79

Hi dear @mohitrajput987 . Thank you for answering my question.
But can you explain more about create Android SDK with c++ ( .so libraries and ndk ) please?
Because I couldn't find how to create SDK with c++

Collapse
 
omarjoya profile image
Omar • Edited

Hi, I have a question, once you publish your Android library to github packages, aws buckets or Bintray, and implemented into a new Android application, are you able to see the code?

I obfuscated my code, my aar file generated is obfuscated but the code is visible anyway after import the library into a new app

The obfuscated classes are not accesible from any class, but If I try to search the file, I can find it and see all the code

Collapse
 
mohitrajput987 profile image
Mohit Rajput

User will be able to see obfuscated code. To hide code like Google, I have tried many frameworks but they didn't work perfectly. I am still doing research on it.

Collapse
 
balasaptanglabs profile image
bala-murugan

do you have completed the research ? can you suggest me some of the best obfuscation methods you found ? Thank you

Thread Thread
 
mohitrajput987 profile image
Mohit Rajput

I didn't check but found a source: github.com/alipov/android-sdk-hide...
You can check this and share feedback

Collapse
 
christyjacob4 profile image
Christy Jacob

First of all, thanks a lot for this series.
I have a use case where I need to create a kotlin library that can be used in both android and other kotlin server projects. Do you have any resources for that ?

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Hey, you can follow the same procedure for that. In the Android Studio, you can click on "New Module" then "Java or Kotlin Library".
The publishing procedure will be same.

Just FYI, JCenter is not accepting new libs now so you can publish it somewhere else i.e. maven central.

Collapse
 
joaoclobocar profile image
joaoclobocar

Thanks,

This post helped me a lot.

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Thanks Joaoclobocar. Keep reading.

Collapse
 
samtech23 profile image
Uncle Sam

Well explained.. this is getting clearer

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Thank you so much. Keep reading my blogs and share your feedback.

Collapse
 
rajdeep_datta_c6496360390 profile image
Rajdeep Datta

I had one doubt, are you making a library in your android app project or creating a different project to create a library?

Collapse
 
mohitrajput987 profile image
Mohit Rajput

We can do both. Generally I make different project. In main app module, I make sample which demonstrates the use of SDK.