Search code examples
androidkotlindotenv

kotlin dotenv doesn't take directory configuration


Trying to use kotlin-dotenv, but this is not working, using kotlin object class, to manage singleton

Got error Could not find /asset/env on the classpath whereas the env file is in /assets folder as mentionned in the doc

object EnvVariables {
    val envVar: Dotenv =
        dotenv {
            directory = "/assets"
            filename = "env"
    }
}

object RetrofitInstance {

    val api: TodoService by lazy {
        Retrofit.Builder()
            .baseUrl(EnvVariables.envVar["BASE_URL"])
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(TodoService::class.java)

    }
}

env in assets folder

Error


Solution

  • Most likely the doc is not accurate

    In file DotEnvReader.java

    return ClasspathHelper
                .loadFileFromClasspath(location.replaceFirst("./", "/"))
                .collect(Collectors.toList());
    

    So, you can fix it by adding "."

    var dotenv = dotenv {
        directory = "./assets" //use ./ instead of /
        filename = "env"
    }