Sharing Non Editable Code in Android : Libraries in Android

Abhinay Gupta
3 min readJun 11, 2023

To share your Android code with other developers without allowing them to edit it, you can follow these steps:

1. Create a Library Module:
— In Android Studio, create a new library module that contains your code.
— Move your code files (such as Java or Kotlin classes) into this module.

2. Configure Module Access:
— In the `build.gradle` file of your library module, specify the desired access modifiers for your code.
— For example, to make your code read-only and prevent modifications, you can use the `private` or `internal` access modifiers.

// Example of build.gradle file in the library module
apply plugin: 'com.android.library'

android {
// Configure other settings for your library module
}

// Specify access modifiers for your code
kotlinOptions {
freeCompilerArgs += ['-Xallow-private-fun']
}

dependencies {
// Include any necessary dependencies for your library module
}

In the example above, the `kotlinOptions` block with the `’-Xallow-private-fun’` argument allows other developers to use your code but prevents them from modifying any private functions.

3. Share the Library Module:
— Package the library module as a separate module or an AAR (Android Archive) file.
— You can share the module or AAR file with other developers, allowing them to import and use it in their projects.

Note: When sharing the library module, ensure that you provide clear documentation, including instructions on how to import and use the code in other projects.

By following these steps, you can share your Android code with other developers while protecting it from modifications. They will be able to use the code as a library in their projects, benefiting from its functionality without the ability to make direct edits.

Example Scenario:
Let’s assume you have created a library module called “MyLibrary” that contains a set of utility functions. Other developers can use this library in their projects but won’t be able to modify it. Here’s an example of how they can import and use the library in their Android projects:

1. Share the “MyLibrary” module or AAR file with other developers.

2. In the other developer’s Android project, add the “MyLibrary” module as a dependency in the project’s `build.gradle` file:

// Example of build.gradle file in the app module of the other developer's project
dependencies {
implementation project(':mylibrary')
}

Alternatively, if the “MyLibrary” module is shared as an AAR file, the developer can add it as a dependency using the AAR file’s location or a Maven repository.

3. Use the code from the “MyLibrary” module in the other developer’s project:

import com.example.mylibrary.MyUtils

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val result = MyUtils.calculateSum(10, 20)
Log.d(TAG, "Sum: $result")
}

companion object {
private const val TAG = "MainActivity"
}
}

In this example, the other developer imports the `MyUtils` class from the “MyLibrary” module and uses its `calculateSum` function. The developer can utilize the functionality provided by the library without being able to modify its code.

By packaging your code as a library module and sharing it with other developers, you can maintain control over your code while allowing others to benefit from its functionality in their Android projects.

--

--