我正在使用Firebase数据库在Android上工作。使用 Firebase 工具 我连接到 Firebase 并使用语句添加了 Firebase 数据库
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
我可以导入数据库参考,但不能导入 FirebaseDatabase
我的 gradle(应用程序( 文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.yadav.yadavapp"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:support-annotations:27.1.1'
}
apply plugin: 'com.google.gms.google-services'
我的主活动文件
package com.example.yadav.yadavapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.google.firebase.database.DatabaseReference; ///working
import com.google.firebase.database.FirebaseDatabase; //cannot import
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
这是不合逻辑的,因为它是同一个包。
Android Studio将简单地删除所有未使用的导入
。除非您在MainActivity
中使用它们.
例如:
public class MainActivity extends AppCompatActivity {
/* when declaring these, it should import both of them */
protected FirebaseDatabase fb = FirebaseDatabase.getInstance();
protected DatabaseReference defaultRef = null;
public MainActivity() {}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}