lupin
lupin

Reputation: 158

java.io.FileNotFoundException: ServiceAccountKey.json (No such file or directory)

I just tried to import firebase amdin sdk in my project.

I'm getting these following messages in android studio.

E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.lupin.ganzp.ticketfireauthmanager, PID: 7883

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lupin.ganzp.ticketfireauthmanager/com.lupin.ganzp.ticketfireauthmanager.MainActivity}: java.io.FileNotFoundException: /ServiceAccountKey.json (No such file or directory)

This is may simple code

const val relativePath = "/ServiceAccountKey.json"

class MainActivity : AppCompatActivity() 
{
    override fun onCreate(savedInstanceState: Bundle?) 
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        SDK_Init()
        Access_DB("newDB")
    }

    fun SDK_Init()
    {
        var serviceAccount = FileInputStream(relativePath)
        val options = FirebaseOptions.Builder()
            .setCredentials(GoogleCredentials.fromStream(serviceAccount))
            .setDatabaseUrl("https://<HiDE>.firebaseio.com/")
            .build()

        FirebaseApp.initializeApp(options)
    }       
}

I already did almost things that I can do...

I tried like that

  1. Path-1 = "c:\~to~ \.json"
  2. Path-2 = "ServiceAccountKey.json"
  3. Path-3 = "./ServiceAccountKey.json"
  4. Path-4 = "com/lupin/ganzp/ticketfireauthmanager/ServiceAccountKey.json"

But, this code can not find my json file, although it really exists as you see this pic.

enter image description here

Below code shows the created file does not exist.

val name = File(".").canonicalPath
    println("Test $name")
    val file = File(name, "/" + "ServiceAccountKey.json")
    println("file path : " + file)
    println(file.exists())

    println(file.exists())  //false
    println(file.canRead())  //false

anyone who knows a solution for this problem?

Upvotes: 0

Views: 4212

Answers (2)

Martin Zeitler
Martin Zeitler

Reputation: 76797

This is an Android project and not a Java project - therefore the location of the file should be res/raw or assets (which both are directories, which are being copied instead of being processed)... else that file won't ever make it into the built package, resulting in error 15.

just be aware, that it might be worth a consideration, to put a file called ServiceAccountKey.json into directories which are being copied 1:1... because that key can be easily extracted from there.

this behaves way different than with the google-services.json, because that file doesn't have to make it into the built package, because it is processed by the plugin and then added into the string resources.

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 599946

There's the way I initialize the Admin SDK in my Java project:

FileInputStream serviceAccount = new FileInputStream("project-credentials.json");

FirebaseOptions options = new FirebaseOptions.Builder()
        .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
        .setDatabaseUrl("https://mydatabase.firebaseio.com/")
        .build();

I keep the JSON file in the same directory as I've set as the working directory for the application.

Upvotes: 1

Related Questions