вот мой код
Код: Выделить всё
package com.example.gactivity2;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
public class MainActivity extends AppCompatActivity {
private static final int RC_SIGN_IN = 1001;
private GoogleSignInClient googleSignInClient;
private FirebaseAuth firebaseAuth;
private static final String TAG = "MainActivity"; // Add this line
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize FirebaseAuth instance
firebaseAuth = FirebaseAuth.getInstance();
// Configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.client_id))
.requestEmail()
.build();
googleSignInClient = GoogleSignIn.getClient(this, gso);
SignInButton signInButton = findViewById(R.id.signIn);
signInButton.setOnClickListener(v -> signIn());
}
private void signIn() {
Intent signInIntent = googleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, handle error
Log.w(TAG, "Google sign in failed", e);
Toast.makeText(MainActivity.this, "Google sign in failed", Toast.LENGTH_SHORT).show();
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Toast.makeText(MainActivity.this, "Authentication success.", Toast.LENGTH_SHORT).show();
// Navigate to the next activity
startActivity(new Intent(MainActivity.this, ProfileActivity.class));
finish(); // Finish current activity
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Код: Выделить всё
package com.example.gactivity2;
import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import java.util.ArrayList;
import java.util.List;
public class ProfileActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ProfileAdapter profileAdapter;
private List userProfileList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
recyclerView = findViewById(R.id.profileRecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
userProfileList = new ArrayList();
// Retrieve current user's information
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
if (currentUser != null) {
String displayName = currentUser.getDisplayName();
String email = currentUser.getEmail();
// Check if display name and email are not null before adding them to the list
if (displayName != null && email != null) {
userProfileList.add(new UserProfile(displayName, email));
// Notify adapter about data changes
if (profileAdapter != null) {
profileAdapter.notifyDataSetChanged();
}
} else {
Toast.makeText(this, "Display name or email is null", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "User not found", Toast.LENGTH_SHORT).show();
}
profileAdapter = new ProfileAdapter(this, userProfileList);
recyclerView.setAdapter(profileAdapter);
}
}
Код: Выделить всё
package com.example.gactivity2;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class ProfileAdapter extends RecyclerView.Adapter
{
private Context context;
private List userProfileList;
public ProfileAdapter(Context context, List userProfileList) {
this.context = context;
this.userProfileList = userProfileList;
}
@NonNull
@Override
public ProfileViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.profile_item, parent, false);
return new ProfileViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ProfileViewHolder holder, int position) {
UserProfile userProfile = userProfileList.get(position);
holder.nameTextView.setText(userProfile.getName());
holder.emailTextView.setText(userProfile.getEmail());
}
@Override
public int getItemCount() {
return userProfileList.size();
}
public static class ProfileViewHolder extends RecyclerView.ViewHolder {
public TextView nameTextView;
public TextView emailTextView;
public ProfileViewHolder(@NonNull View itemView) {
super(itemView);
nameTextView = itemView.findViewById(R.id.nameTextView);
emailTextView = itemView.findViewById(R.id.emailTextView);
}
}
}
Код: Выделить всё
package com.example.gactivity2;
public class UserProfile {
private String name;
private String email;
public UserProfile(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
}
Код: Выделить всё
это отображается в моем журнале
E ФАТАЛЬНОЕ ИСКЛЮЧЕНИЕ: основной
Процесс: com.example.gactivity2, PID: 8890
java.lang.NoClassDefFoundError: Не удалось разрешить: Lcom/google/firebase/appcheck/interop/InternalAppCheckTokenProvider;
at com.google.firebase.database.DatabaseRegistrar.getComponents(DatabaseRegistrar.java: 38)
в com.google.firebase.tracing.ComponentMonitor.processRegistrar(ComponentMonitor.java:28)
в com.google.firebase.comComponents.ComponentRuntime.discoverComponents(ComponentRuntime.java:118)
в com.google.firebase.comComponents.ComponentRuntime.(ComponentRuntime.java:100)
в com.google.firebase.comComponents.ComponentRuntime.(ComponentRuntime.java:46)
в com.google .firebase.comComponents.ComponentRuntime$Builder.build(ComponentRuntime.java:407)
на com.google.firebase.FirebaseApp.(FirebaseApp.java:439)
на com.google.firebase.FirebaseApp. InitializeApp(FirebaseApp.java:296)
на com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:264)
на com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:249)
в com.google.firebase.provider.FirebaseInitProvider.onCreate(FirebaseInitProvider.java:69)
в android.content.ContentProvider.attachInfo(ContentProvider.java:2619)
в android.content .ContentProvider.attachInfo(ContentProvider.java:2589)
в com.google.firebase.provider.FirebaseInitProvider.attachInfo(FirebaseInitProvider.java:61)
в android.app.ActivityThread.installProvider(ActivityThread.java) :7765)
в android.app.ActivityThread.installContentProviders(ActivityThread.java:7276)
в android.app.ActivityThread.handleBindApplication(ActivityThread.java:6983)
в android.app. ActivityThread.-$$Nest$mhandleBindApplication(Неизвестный источник:0)
в android.app.ActivityThread$H.handleMessage(ActivityThread.java:2236)
в android.os.Handler.dispatchMessage(Handler. java:106)
в android.os.Looper.loopOnce(Looper.java:205)
в android.os.Looper.loop(Looper.java:294)
в android.app .ActivityThread.main(ActivityThread.java:8177)
в java.lang.reflect.Method.invoke(собственный метод)
в com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit. java:552)
на com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
Вызвано: java.lang.ClassNotFoundException: не найден класс «com.google. firebase.appcheck.interop.InternalAppCheckTokenProvider" по пути: DexPathList[[zip file "/data/app/~~LITB5I8JdGavlf5yifKG8A==/com.example.gactivity2-xKJC-q_5t825eUwNZVhB2A==/base.apk"],nativeLibraryDirectories=[/ data/app/~~LITB5I8JdGavlf5yifKG8A==/com.example.gactivity2-xKJC-q_5t825eUwNZVhB2A==/lib/x86_64, /system/lib64, /system_ext/lib64]]
at dalvik.system.BaseDexClassLoader.findClass( BaseDexClassLoader.java:259)
на java.lang.ClassLoader.loadClass(ClassLoader.java:379)
на java.lang.ClassLoader.loadClass(ClassLoader.java:312)
на com .google.firebase.database.DatabaseRegistrar.getComponents(DatabaseRegistrar.java:38)
на com.google.firebase.tracing.ComponentMonitor.processRegistrar(ComponentMonitor.java:28)
на com.google.firebase .comComponents.ComponentRuntime.discoverComponents(ComponentRuntime.java:118)
на com.google.firebase.comComponents.ComponentRuntime.(ComponentRuntime.java:100)
на com.google.firebase.comComponents.ComponentRuntime. (ComponentRuntime.java:46)
на com.google.firebase.comComponents.ComponentRuntime$Builder.build(ComponentRuntime.java:407)
на com.google.firebase.FirebaseApp.(FirebaseApp.java: 439)
на com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:296)
на com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:264)
на com .google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:249)
на com.google.firebase.provider.FirebaseInitProvider.onCreate(FirebaseInitProvider.java:69)
на android.content.ContentProvider.attachInfo (ContentProvider.java:2619)
в android.content.ContentProvider.attachInfo(ContentProvider.java:2589)
в com.google.firebase.provider.FirebaseInitProvider.attachInfo(FirebaseInitProvider.java:61) в android.app.ActivityThread.installProvider(ActivityThread.java:7765)
в android.app.ActivityThread.installContentProviders(ActivityThread.java:7276)
в android.app.ActivityThread.handleBindApplication( ActivityThread.java:6983)
в android.app.ActivityThread.-$$Nest$mhandleBindApplication(Неизвестный источник:0)
в android.app.ActivityThread$H.handleMessage(ActivityThread.java:2236)
в android.os.Handler.dispatchMessage(Handler.java:106)
в android.os.Looper.loopOnce(Looper.java:205)
в android.os.Looper.loop (Looper.java:294)
в android.app.ActivityThread.main(ActivityThread.java:8177)
в java.lang.reflect.Method.invoke(Native Method)
в com .android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
на com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
Я попробовал обновить свой манифест и Gradle, обновив классы Java.
Подробнее здесь: https://stackoverflow.com/questions/781 ... tudio-java
Мобильная версия