Generate Kotlin constants or XML string resources from files in your Android assets/ directories. Use generated names in code and keep raw asset paths out of call sites.
If your module contains:
src/main/assets/
├── models/
│ └── ml_model.tflite
└── configs/
└── settings.json
you can replace raw strings like this:
assets.open("models/ml_model.tflite")
assets.open("configs/settings.json")with generated constants:
import com.yourcompany.yourapp.assets.AssetFiles
assets.open(AssetFiles.ASSET_MODELS_ML_MODEL_TFLITE_527533696)
assets.open(AssetFiles.ASSET_CONFIGS_SETTINGS_JSON_2053859403)Expect a hash suffix on each generated name. The suffix keeps names unique when two paths normalize to the same identifier.
The Gradle badge above and the Gradle Plugin Portal show the latest published version.
Apply the plugin after com.android.application or com.android.library in the Android module that owns the assets/ directory.
plugins {
id("com.android.application")
// or id("com.android.library")
id("com.github.utilx.android-assets-journalist") version "<latest-version>"
}If you leave both outputs disabled, the plugin generates Kotlin output. Unless you set packageName, it writes the generated file to com.github.utilx.
androidAssetsJournalist {
kotlinFile {
enabled = true
packageName = "com.yourcompany.yourapp.assets"
className = "AssetFiles"
constNamePrefix = "ASSET_"
}
xmlFile {
enabled = false
}
}Enable XML generation when you want Android string resources instead of Kotlin constants.
androidAssetsJournalist {
xmlFile {
enabled = true
stringNamePrefix = "asset_"
}
kotlinFile {
enabled = false
}
}Run your usual Android build.
./gradlew assembleDebugGradle registers variant-aware tasks such as generateAssetsKotlinFileDebug and generateAssetsXmlFileDebug.
You will find generated files under build/generated/assetsjournalist/src/<variant>/.
With the Kotlin config above, you get:
package com.yourcompany.yourapp.assets
object AssetFiles {
const val ASSET_MODELS_ML_MODEL_TFLITE_527533696 = "models/ml_model.tflite"
const val ASSET_CONFIGS_SETTINGS_JSON_2053859403 = "configs/settings.json"
}With XML output enabled, you get:
<resources>
<string name="asset_models_ml_model_tflite_527533696">models/ml_model.tflite</string>
<string name="asset_configs_settings_json_2053859403">configs/settings.json</string>
</resources>Use replaceInAssetsPath when you want the generated name and value to use a transformed path.
androidAssetsJournalist {
kotlinFile {
enabled = true
replaceInAssetsPath = listOf(
mapOf("match" to "^dev_", "replaceWith" to "prod_")
)
}
}Use constValuePrefix when you need values such as file:///android_asset/....
androidAssetsJournalist {
kotlinFile {
enabled = true
className = "Assets"
packageName = "com.yourcompany.yourapp.assets"
constNamePrefix = "ASSET_"
constValuePrefix = "file:///android_asset/"
}
}const val ASSET_MODELS_ML_MODEL_TFLITE_527533696 =
"file:///android_asset/models/ml_model.tflite"kotlinFile
enabled: turns Kotlin generation on or offclassName: generated object name. DefaultAssetFilespackageName: generated package name. Defaultcom.github.utilxconstNamePrefix: prefix added before the sanitized path. Defaultasset_. The generator uppercases the final constant name.constValuePrefix: prefix added to each generated value. Default""replaceInAssetsPath: regex replacements applied before the plugin builds the generated name and value
xmlFile
enabled: turns XML generation on or offstringNamePrefix: prefix added to each generated string resource name
- Java 17 or newer
- Gradle 8.0 or newer
- Android Gradle Plugin 8.0.0 or newer
namespaceset in your Android block when you use AGP 8+
- Apply the plugin in the Android module that owns the
assets/directory. - Each variant gets its own generated output directory.
- The Kotlin generator removes duplicate entries after path transformation.
- Examples in this README use Kotlin DSL.
Apache 2.0. See LICENSE.