Netro is a Gradle plugin that automates the generation of Retrofit API interfaces and Kotlin data models using YAML configuration files. It simplifies API integration in Kotlin projects by eliminating the need for manually writing API service interfaces and data models.
- Automatic API Service Generation: Converts YAML configurations into Retrofit API interfaces.
- Model Class Generation: Generates Kotlin data classes using KotlinPoet and Kotlin Serialization.
- Supports Multiple HTTP Methods: GET, POST, PUT, DELETE are handled seamlessly.
- Organized Code Structure:
- API services and models are structured into separate packages.
- Naming conventions ensure maintainability.
- Easy Integration: Can be applied as a Gradle plugin in any Kotlin project.
Then, apply the plugin in your build.gradle.kts:
plugins {
id("com.github.probelalkhan.netro") version "0.2"
}Run:
gradle syncnetroConfig {
path = project.rootProject.layout.projectDirectory.dir("netro-configs").asFile.absolutePath
packageAlias = android.namespace!!
}You can customize the configuration as needed:
path: The directory path where YAML configuration files are stored.packageAlias: The package name for generated API services and models.
Inside the config directory (you have defined config directory in the netroConfig block), create a YAML file following the naming convention:
π projectRoot/netro-configs/user_config.yaml
baseUrl: https://dummyYAML.com/
endpoints:
- name: loginUser
path: /user/login
method: POST
requestModel: LoginRequest
responseModel: LoginResponse
models:
LoginRequest:
username: String
password: String
LoginResponse:
id: Int
username: String
email: String
accessToken: StringExecute the following Gradle task to generate API service interfaces and model classes:
./gradlew netroSyncAfter execution, Netro generates the following structure:
π com.example.user
- π
UserApiService.kt(Retrofit API Interface) - π
models- π
UserResponse.kt(Response Model) - π
CreateUserRequest.kt(Request Model)
- π
public interface UsersApiService {
@GET("/users")
public suspend fun getUsers(): UserListResponse
@GET("/users/{id}")
public suspend fun getUserById(@Path("id") id: String): User
}@Serializable
public data class UserListResponse(
@SerialName("users")
public val users: List<User>,
@SerialName("total")
public val total: Int,
@SerialName("skip")
public val skip: Int,
@SerialName("limit")
public val limit: Int,
)
@Serializable
public data class User(
@SerialName("id")
public val id: Int,
@SerialName("firstName")
public val firstName: String,
@SerialName("lastName")
public val lastName: String,
@SerialName("age")
public val age: Int,
@SerialName("gender")
public val gender: String,
@SerialName("email")
public val email: String,
)Netro automatically organizes API services into separate packages based on YAML filenames. Ensure your configuration file follows the naming convention:
{package_name}_config.yaml
Where:
{package_name}is used as the package name.- API services are generated inside
{package_name}. [{package_name}+ApiService.kt] - Data models are placed inside
{package_name}.models.
You can define multiple YAML configurations for different API modules. Each file will generate a separate API service and models.
π src/main/resources/api_config/order_config.yaml
package: com.example.order
baseUrl: https://api.example.com
endpoints:
- name: getOrder
path: /orders/{id}
method: GET
response: OrderResponse
This will generate:
π com.example.order
- π
OrderApiService.kt - π
models- π
OrderResponse.kt
- π
- Kotlin: 1.9+
- Gradle: 8.0+
- Java: 17+
- Retrofit: 2.x+
- KotlinPoet: 1.17+
Contributions are welcome! Feel free to fork the repo and submit a pull request.
- Clone the repository:
git clone https://github.com/probelalkhan/netro.git
- Create a feature branch:
git checkout -b feature-name
- Commit changes:
git commit -m "feat: Add new feature" - Push to the branch:
git push origin feature-name
- Create a pull request.
Netro is licensed under the MIT License. See LICENSE for details.
Developed by Belal Khan
gradle-plugin retrofit kotlin api-generator yaml-to-kotlin kotlinpoet code-generation serialization
If you find this plugin useful, consider giving it a β on GitHub!
Happy Coding! π