# Embed (inline) file into variable

**URL:** <https://discuss.kotlinlang.org/t/embed-inline-file-into-variable/17033>\
**Category:** Android\
**Created:** [April 8, 2020, 6:18pm UTC](https://discuss.kotlinlang.org/t/embed-inline-file-into-variable/17033 "2020-04-08T18:18:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [April 8, 2020, 6:18pm UTC](https://discuss.kotlinlang.org/t/embed-inline-file-into-variable/17033/1 "2020-04-08T18:18:41Z")

</div>

Hello, what is the way to embed (inline) file into variable in Kotlin (with Android, so, no extra permissions needed)? F. e.:

```
@Embed(name = 'mock.json')
val jsonFile: String

```

Is there a better way to use it with Moshi?

The bad way I found is to use an ad-hoc (JSON sample):

```
val mock =
"""
  {"key":"value"}
"""

```

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [April 9, 2020, 3:56pm UTC](https://discuss.kotlinlang.org/t/embed-inline-file-into-variable/17033/2 "2020-04-09T15:56:24Z")

</div>

There’s no Kotlin specific solution for resources that I’ve seen. You just embed resources the same way you would normally for the platform being used (JVM, Android, JS, Native). For Android, see [App resources overview](https://developer.android.com/guide/topics/resources/providing-resources)

---

<div class="post-metadata">

**Author:** ![topsys](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/topsys/32/7328_2.png) [@topsys](https://discuss.kotlinlang.org/u/topsys)\
**Post date:** [April 9, 2020, 11:19pm UTC](https://discuss.kotlinlang.org/t/embed-inline-file-into-variable/17033/3 "2020-04-09T23:19:21Z")

</div>

I inject compile timestamps, unique serial numbers, …  
via RegEx-replace directly into the source code.

To reduce errors, I replace variable contents in small classes used for this purpose only.

---

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [April 10, 2020, 7:18am UTC](https://discuss.kotlinlang.org/t/embed-inline-file-into-variable/17033/4 "2020-04-10T07:18:54Z")

</div>

Can you do it without a separate script/tools?

---

<div class="post-metadata">

**Author:** ![topsys](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/topsys/32/7328_2.png) [@topsys](https://discuss.kotlinlang.org/u/topsys)\
**Post date:** [April 10, 2020, 1:03pm UTC](https://discuss.kotlinlang.org/t/embed-inline-file-into-variable/17033/5 "2020-04-10T13:03:19Z")

</div>

No, of cause not.

If you will have fixed one-time resources to insert into the source, you can do it per hand.  
If you have dynamically created resources, you already have external tools to create them.

No no need for such a Kotlin function.

---

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [April 17, 2020, 7:06pm UTC](https://discuss.kotlinlang.org/t/embed-inline-file-into-variable/17033/6 "2020-04-17T19:06:01Z")

</div>

Yeah, for Android it could be the best way to read a resource like **R.raw.mock** from **res/raw/mock.json**

---

<div class="post-metadata">

**Author:** ![Slion](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Slion](https://discuss.kotlinlang.org/u/Slion)\
**Post date:** [October 6, 2022, 6:27pm UTC](https://discuss.kotlinlang.org/t/embed-inline-file-into-variable/17033/7 "2022-10-06T18:27:24Z")

</div>

I use this one:

> **[GitHub - anthonycr/Mezzanine: An annotation processor that reads files at...](https://github.com/anthonycr/Mezzanine)**
>
> An annotation processor that reads files at compile time - GitHub - anthonycr/Mezzanine: An annotation processor that reads files at compile time

Though I was also looking for alternative as embedded content does not get updated unless you rebuild your project.

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [October 16, 2022, 5:32pm UTC](https://discuss.kotlinlang.org/t/embed-inline-file-into-variable/17033/8 "2022-10-16T17:32:56Z")

</div>

You could use something like [BuildConfig](https://github.com/gmazzo/gradle-buildconfig-plugin) to read the file at compile time and output it into a string.
