java - 将 firebase 添加到应用程序时出现 appcompat 依赖项错误

标签 java android firebase google-cloud-firestore location

我开发了一个简单的应用程序,在按下按钮后显示设备的 IMEI 以及纬度和经度。现在,我想将此数据(纬度、经度、IMEI)保存在 FireStore 中,我有一些理解问题,我应该在哪里以及如何将这些数据保存到 firestore 中。当我尝试通过工具将实时 firebase 添加到我的应用程序时,我遇到了一些错误

我已尝试更改依赖项版本,但无法解决问题。

package com.example.locatieimei;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

private FusedLocationProviderClient client;
private TextView imei;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imei = findViewById(R.id.imei);
    loadIMEI();






    client = LocationServices.getFusedLocationProviderClient(this);
    Button button = findViewById(R.id.Chk);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (ActivityCompat.checkSelfPermission(MainActivity.this, 
 Manifest.permission.ACCESS_FINE_LOCATION) != 
 PackageManager.PERMISSION_GRANTED && 
 ActivityCompat.checkSelfPermission(MainActivity.this, 
 Manifest.permission.ACCESS_COARSE_LOCATION) != 
 PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then 
overriding
                //   public void onRequestPermissionsResult(int 
requestCode, String[] permissions,
                //                                          int[] 
grantResults)
                 // to handle the case where the user grants the 
permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            client.getLastLocation().addOnSuccessListener(new 
OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    if (location!=null){
                        TextView textView = findViewById(R.id.textView);

                        Double longitude = location.getLongitude();
                        Double latitude = location.getLatitude();

textView.setText(String.valueOf("latitudine"+longitude+ 
"\n"+"longitutine"+latitude));

                    }
                }
            });



        }

    });

}
public void loadIMEI() {
    // Check if the READ_PHONE_STATE permission is already available.
    if (ActivityCompat.checkSelfPermission(this, 
Manifest.permission.READ_PHONE_STATE)
            != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.READ_PHONE_STATE)) {
//                get_imei_data();
        } else {
            ActivityCompat.requestPermissions(this, new String[] 
{Manifest.permission.READ_PHONE_STATE},
                    MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
        }
    } else {

        TelephonyManager mngr = 
 (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

        IMEI = mngr.getDeviceId();

        imei.setText(mngr.getDeviceId());
        // READ_PHONE_STATE permission is already been granted.


    }
}
private String device_unique_id;
private static String IMEI;
private static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 0;

}

这些是错误

    ERROR: In project 'app' a resolved Google Play services library dependency 
    depends on another at an exact version (e.g. "[11.0.
    1]", but isn't being resolved to that version. Behavior exhibited by the 
    library will be unknown.

    Dependency failing: com.google.android.gms:play-services-location:11.0.1 - 
    > com.google.android.gms:play-services-tasks@[
    11.0.1], but play-services-tasks version was 16.0.1.

    The following dependencies are project dependencies that are direct or 
    have transitive dependencies that lead to the art
    ifact with the issue.
    -- Project 'app' depends onto com.google.firebase:firebase- 
    database@{strictly 16.0.4}
    -- Project 'app' depends onto com.google.android.gms:play-services- 
    base@{strictly 16.0.1}
    -- Project 'app' depends onto com.google.android.gms:play-services- 
    tasks@{strictly 16.0.1}
    -- Project 'app' depends onto com.google.firebase:firebase-database@16.0.4
    -- Project 'app' depends onto com.google.firebase:firebase- 
    common@{strictly 16.0.3}
    -- Project 'app' depends onto com.google.android.gms:play-services- 
    location@{strictly 11.0.1}
    -- Project 'app' depends onto com.google.android.gms:play-services- 
    location@11.0.1

    For extended debugging info execute Gradle from the command line with 
    ./gradlew --info :app:assembleDebug to see the dep
    endency paths to the artifact. This error message came from the google- 
    services Gradle plugin, report issues at https://
    github.com/google/play-services-plugins and disable by adding 
    "googleServices { disableVersionCheck = false }" to your b
    uild.gradle file.

 And if i change to 17.0.0 version from 11.0.1 in 'com.google.android.gms:play- 
services-location:17.0.0' the it is showing the following error

    ERROR: Manifest merger failed : Attribute application@appComponentFactory 
    value=(android.support.v4.app.CoreComponentFactory) from 
    [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91
    is also present at [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 
    value=(androidx.core.app.CoreComponentFactory).
    Suggestion: add 'tools:replace="android:appComponentFactory"' to 
    <application> element at AndroidManifest.xml:10:5-24:19 to override.

最佳答案

This release is a MAJOR version update and breaking change.

The latest update to Google Play services and Firebase includes the following changes:

Migration from Android Support Libraries to Jetpack (AndroidX) Libraries. Libraries will not work unless you make the following changes in your app:

  • Upgrade com.android.tools.build:gradle to v3.2.1 or later.
  • Upgrade compileSdkVersion to 28 or later.
  • Update your app to use Jetpack

(AndroidX); follow the instructions in Migrating to AndroidX.

Firebase Bill of Materials (BoM)

问题是最新的 firebase 和 play-services 依赖被迁移到了 androidx。所以一个修复是将您的项目迁移到 androidx,请参阅 migrate to androidx (我更喜欢,因为所有新升级都使用 androidx)。或者您可以将 firebase-core 和 play 服务降级到此更新之前的版本。

关于java - 将 firebase 添加到应用程序时出现 appcompat 依赖项错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57357568/

相关文章:

java - Spring Boot + 云 | Zuul 代理 |集成测试

java - 有人可以解释一下 EventQueue 对象的用途吗?

java - 列表 fragment 中的项目没有改变颜色

Android:如何对齐底部的复选框文本?

java - 在 Firebase (Android) 中同时更新多个节点中的多个字段

java - 线程中出现异常 "main"java.lang.StringIndexOutOfBoundsException

java - 无法调用 ViewDebug.ExportedProperty 方法

android - 如何检索 firebase 数据库条目的细节?

jquery - Firebase REST API POST 请求失败,错误为 : "Invalid data; couldn' t parse JSON object, 数组或值...”

java - 自动完成 Struts2 jQuery