Skip to content

Commit

Permalink
Improved code to get namespace using ksp when possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
vsnappy1 committed Nov 8, 2024
1 parent a7870f7 commit e358164
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ResourceManager
ResourceManager is an Android plugin that simplifies accessing Android resources (strings, colors, drawables, etc.) in both Android and non-Android components (e.g., ViewModel) using generated code. It supports both Kotlin and Java projects.

[![Maven Central](https://img.shields.io/maven-central/v/dev.randos/resourcemanager-runtime.svg)](https://central.sonatype.com/artifact/dev.randos/resourcemanager-runtime)
[![Maven Central](https://img.shields.io/maven-central/v/dev.randos/resourcemanager-compiler.svg)](https://central.sonatype.com/artifact/dev.randos/resourcemanager-compiler)
![Platform Support](https://img.shields.io/badge/platform-Android-brightgreen.svg)

## Setup
Expand Down Expand Up @@ -57,14 +57,6 @@ class MyApplication: Application() {
}
}
```
__Note:__
If your `Application` class is located in the root package (e.g., `com.example.yourapp`), you don’t need to specify a namespace. However, if your Application class is in a sub-package (e.g., `com.example.yourapp.app`), you must specify the namespace in the annotation to correctly reference the resources.
```kotlin
@InstallResourceManager(namespace = "com.example.yourapp") // Specify the namespace if Application is in a sub-package
class MyApplication: Application() {
...
}
```

## Usage
Here’s an example of how to use ResourceManager in a ViewModel
Expand All @@ -88,4 +80,12 @@ To ensure that ResourceManager works correctly with __*ProGuard*__ or __*R8*__,
public void initialize(android.app.Application);
}
```
__Note:__
If your namespace (i.e., `com.example.yourapp`) is different than module package structure (e.g., `com/example/yourapp2/app`), then you must specify the namespace in the annotation to correctly reference the resources. (i.e., `com.example.yourapp.R`)
```kotlin
@InstallResourceManager(namespace = "com.example.yourapp")
class MyApplication: Application() {
...
}
```

5 changes: 1 addition & 4 deletions resourcemanager-compiler/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,7 @@ publishing {
from(components["java"])
groupId = "dev.randos"
artifactId = "resourcemanager-compiler"
version = "0.0.2"

val bundle = file("${publishBundleRepo.absolutePath}/$artifactId-$version.zip")
artifact(bundle)
version = "0.0.3"

pom {
name = "${groupId}:${artifactId}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ internal class ResourceManagerAnnotationProcessor(

val unprocessedSymbols = mutableListOf<KSAnnotated>()

// Get the namespace
val namespace = resolver.getAllFiles().firstOrNull()?.packageName?.asString()

for (symbol in symbols) {
val containingFile = symbol.containingFile ?: continue
val annotatedFile = File(containingFile.filePath)
Expand Down Expand Up @@ -63,8 +66,7 @@ internal class ResourceManagerAnnotationProcessor(

if (classFile == null) {
classFile = ClassFileGenerator.generateClassFile(
packageName = packageName,
namespace = getNameSpace(symbol) ?: packageName,
namespace = getNamespace(symbol) ?: namespace ?: packageName,
files = resources
)
}
Expand All @@ -86,7 +88,7 @@ internal class ResourceManagerAnnotationProcessor(
return unprocessedSymbols
}

private fun getNameSpace(it: KSClassDeclaration): String? {
private fun getNamespace(it: KSClassDeclaration): String? {
val namespaceAnnotation =
it.annotations.firstOrNull { annotation -> annotation.annotationType.resolve().declaration.qualifiedName?.asString() == InstallResourceManager::class.java.name }
val namespace = namespaceAnnotation?.arguments?.firstOrNull()?.value as? String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ internal class ClassFileGenerator {

companion object {
fun generateClassFile(
packageName: String,
namespace: String,
files: List<Resource>
): String {
return StringBuilder().apply {
appendLine("package $packageName\n")
appendLine("package $namespace\n")
appendLine("import android.app.Application")
appendLine("import android.graphics.drawable.Drawable")
appendLine("import android.content.res.Resources.Theme")
Expand Down
5 changes: 1 addition & 4 deletions resourcemanager-runtime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ publishing {
register<MavenPublication>("java") {
groupId = "dev.randos"
artifactId = "resourcemanager-runtime"
version = "0.0.2"

val bundle = file("${publishBundleRepo.absolutePath}/$artifactId-$version.zip")
artifact(bundle)
version = "0.0.3"

pom {
name = "${groupId}:${artifactId}"
Expand Down

0 comments on commit e358164

Please sign in to comment.