Reputation: 158
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
But, this code can not find my json file, although it really exists as you see this pic.
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
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
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