Hari
Hari

Reputation: 144

How to hide definitions of functions in Android library

I developed some algorithms to calculate health-related parameters by using heart rates. I want to give it to other developers. But don't want to expose the implementation of functions. Like iOS does with frameworks. Is there any solution to create frameworks in Android like this?

Upvotes: 0

Views: 556

Answers (1)

Emanuel Moecklin
Emanuel Moecklin

Reputation: 28856

Android's build system (Gradle) allows to define dependencies to other artifacts / libraries.

A library on iOS is the equivalent of a jar file on Android.

A framework on iOS is the equivalent of an aar file on Android (aar = Android archive).

https://developer.android.com/studio/projects/android-library explains in detail how libraries are created. There are multiple ways how a library can be linked to a project:

  • the library is a library module in the project, in that case it's part of the same code base and the consumer of the library (the other developers) would see the details of the implementation
  • the library is linked as a jar or an aar file (you'd create the jar/aar file and commit it to the project repo), in which case the consumer of the library would not see the details of the implementation (more to that later)
  • the library is published to a Maven repository (public or private) and the consumer would simply define it as a project dependency (the typical implementation("group:artifact:version") dependency), there's plenty of articles that explain publication or artifacts of a Maven repository, one of them is my own article here: https://medium.com/nerd-for-tech/the-better-maven-central-90d7e529d606

Note: no matter what you do, the consumer of the library can still see the details of the implementation since the IDE can decompile the code. Depending on the reasons why you want to hide the details of the implementation there's different solutions:

  • the code is proprietary and needs to be protected as intellectual property: use obfuscation with ProGuard or even DexGuard
  • I don't want the consumer of my lib to use undocumented features: properly design the library, e.g. by coding against interfaces that hide the concrete classes which can e.g. be retrieved using factory classes or dependency injection

Upvotes: 2

Related Questions