How to solve error: cannot find symbol class DatabaseReference problem - android

I'm working on the android application and when I want to connect with firebase. I have an:
error: cannot find symbol class DatabaseReference
Error. My code:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseReference reference = FirebaseDatabase.getInstance().getreference();
}
}
My build.gradle(Project:android)
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.1'
classpath 'com.google.gms:google-services:4.3.2'
}
}
allprojects {
repositories {
google()
jcenter()
}
My build.gradle(Module: app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.ala.marouene"
minSdkVersion 21
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'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-analytics:17.2.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'
}
apply plugin: 'com.google.gms.google-services
And I already have JSON file in my .app folder.

Add Realtime Database:
implementation "com.google.firebase:firebase-database:19.2.0"
But you'd generally need to use elder versions of Firebase, which still were built against com.android.support libraries - or migrate the whole project to androidx libraries.

Related

Add a user to a firebase group

So I tried adding a user to a firebase group to be able to send data notification to multiple user on a simple app that show the notification when it receives it.
My problem is that following the firebase documentation I tried adding the following line to my mainactivity.java
FirebaseMessaging.getInstance().subscribeToTopic("weather")
But android studio told me that the .getInstance couldn't be called/"Cannot resolve symbol 'getInstance'é it so I don't know what to do to make it work
edit:
The app build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.shayru.myapplication"
minSdkVersion 21
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'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
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.google.firebase:firebase-messaging:17.3.4'
implementation 'com.google.firebase:firebase-core:16.0.6'
}
The Project build.gradle :
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.google.gms:google-services:4.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
and if needed this is what the mainactivity does :
package com.shayru.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.firebase.messaging.FirebaseMessaging;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
FirebaseMessaging.getInstance().subscribeToTopic("News");
}
The problem is not with Firebase, rather its with your code. Subscribe to the topic in your onCreate() method, not outside a method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseMessaging.getInstance().subscribeToTopic("News");
}

Message sent by Firebase not displayed in logcat

I'm a beginner in Android, and I'm trying to send a message with Firebase, but when I send it nothing is displayed in the logcat while my emulator is running and I sent the message.
My service :
package com.example.tibdev.notiffirebase;
import android.app.Service;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
//récupération du message
String message = remoteMessage.getNotification().getBody();
Log.d("FireBaseMessage", "Notfication : " + message);
}
}
My graddle file app :
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.tibdev.notiffirebase"
minSdkVersion 16
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'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
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-vector-drawable:28.0.0'
implementation 'com.android.support:exifinterface:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.google.firebase:firebase-database:16.0.4'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
implementation 'com.google.firebase:firebase-core:16.0.4'
implementation 'com.google.firebase:firebase-auth:16.0.5'
}
My gradle file build :
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.google.gms:google-services:4.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://maven.google.com" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I've been trying to debug this for a day, but nothing happens, even if I follow some tutorials

Why does the Android Instrumented Test example from the official docs not work?

Following the docs at https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests, I have created the following build.gradle file for my app module:
buildscript {
repositories {
mavenCentral()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.25.3'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
maven { url 'https://maven.fabric.io/public' }
}
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "com.myapp"
minSdkVersion 19
targetSdkVersion 19
versionCode 7
versionName "1.3.0"
ndk {
abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
}
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
lintOptions {
checkReleaseBuilds false
abortOnError false
}
dexOptions {
javaMaxHeapSize "4g"
}
}
dependencies {
// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.12'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:1.10.19'
androidTestImplementation 'com.android.support:support-annotations:27.1.1'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-accessibility:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-web:3.0.2'
androidTestImplementation 'com.android.support.test.espresso.idling:idling-concurrent:3.0.2'
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':deviceprint-lib-2.0.1')
implementation files('libs/gson-2.2.4.jar')
implementation files('libs/activation.jar')
implementation files('libs/additionnal.jar')
implementation files('libs/mail.jar')
implementation 'com.android.support:support-v4:27.1.1'
}
and the following project level build.gradle:
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
}
}
allprojects {
repositories {
jcenter()
google()
}
}
I created a directory at app/src/androidTest/java/com/mypackage and added the following file:
package com.mypackage;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class ApplicationTest
{
public ApplicationTest()
{
}
#Test public void failingTest()
{
assertThat(false, is(true));
}
}
All of the import statements, and the #Test annotation are marked as errors, with the message, "cannot resolve symbol" and If I right click on the file in the project window, I do not have the option to "Run". Why?
The example started to work correctly after I restarted Android Studio. :facepalm:
Edit:
another note, as I recently experienced these same symptoms for a different reason: the build variant must be set to "debug" (or you must set testBuildType inside the android {} block of your module's build.gradle file to the variant you want to test)

Can't import any Firebase classes

Out of nowhere, I suddenly can no longer import or use any Firebase classes. The class names show up in intellisense, but when hitting tab to import, it inserts their fully qualified name, and an error in the class name.
build.gradle (app)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 'android-P'
defaultConfig {
applicationId "com.opticalgenesis.lbp.dogg"
minSdkVersion 21
targetSdkVersion 'P'
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
implementation 'com.google.firebase:firebase-core:16.0.0'
implementation 'com.google.firebase:firebase-ml-model-interpreter:16.0.0'
implementation 'com.google.android.material:material:1.0.0-alpha1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
}
apply plugin: 'com.google.gms.google-services'
build.gradle (project)
buildscript {
ext.kotlin_version = '1.2.41'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0-alpha16'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url="https://maven.google.com"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
small snippet
private fun setupModel() {
val builder = com.google.firebase.ml.custom.model.FirebaseModelDownloadConditions.Builder()
}
FirebaseModelDownloadConditions is not the only class that doesn't import, they all don't.
Any help is appreciated.
Thanks.

can't call kotlin module from java module

I want to have an android app in java, but one library module in kotlin. However when I try to run the app in my phone, there's an error saying that it can't find my Kotlin class. This is my kotlin class:
package com.example.mylibrary
import android.util.Log
class A {
fun helloWorld(){
Log.d("Kotlin", "Hello World!")
}
}
and gradle file for my kotlin module:
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 11
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
buildscript {
ext.kotlin_version = '0.6.+'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
repositories {
mavenCentral()
}
Here's my main activity:
package com.example.uisample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.mylibrary.A;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
A obj = new A();
obj.helloWorld();
}
}
Notice how android studio imports com.example.mylibrary.A instead of com.example.mylibrar.Akt as the reference says. Android studio reports no errors before compiling.
Gradle file for app:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.example.uisample"
minSdkVersion 11
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile project(':mylibrary')
}
After running the project, gradle throws this error: "cannot find symbol class A". What am I doing wrong?
A few things:
Apply the kotlin-android plugin to your library module build.gradle (see the docs):
apply plugin: 'kotlin-android'
As it stands now, you're not even telling gradle to compile Kotlin files.
Your buildscript block should probably be at the top-level build.gradle rather than the module build.gradle.
Consider updating the fully released version of Kotlin 1.0.1-2
For that, you need to add
buildscript {
ext.kotlin_version = '1.4.30' <-- This line
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" <-- This line
}
}
In the gradle.build (project)
and then add the
apply plugin: 'kotlin-android'
In the gradle.build (App)

Categories

Resources