Google 地图在 APK 构建执行期间崩溃

Google Maps crash during APK build execution

我在我的应用程序中使用 Google 地图。我尝试在包含地图的片段中使用 MapView 和 SupportMapFragment。 出现的问题导致 Google 地图在地图初始化期间 'Build APK' 处理后(在我测试过的所有设备中)崩溃,但是当 运行 来自 Android Studio 时(使用 运行) 它按预期工作。这是堆栈跟踪(我在所有执行中得到的只是 AssertionError):

10-19 12:17:27.244 2256-2638/com.google.android.gms E/BaseAppContext: Tried to stop global GMSCore RequestQueue. This is likely unintended, so ignoring. 10-19 12:17:28.640 1921-1921/com.google.android.gms.persistent E/BluetoothAdapter: Bluetooth binder is null 10-19 12:17:29.537 1921-1921/com.google.android.gms.persistent E/BluetoothAdapter: Bluetooth binder is null 10-19 12:17:29.542 2256-2677/com.google.android.gms E/MDM: [142] rpv.a: Couldn't connect to Google API client: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null} 10-19 12:17:30.112 1921-1921/com.google.android.gms.persistent E/ChimeraRcvrProxy: Can't find Chimera receiver impl class com.google.android.gms.auth.setup.devicesignals.LockScreenChimeraReceiver, dropping broadcast 10-19 12:17:31.602 2393-2501/com.app E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaa112310 10-19 12:17:36.351 2776-2782/? E/art: Failed sending reply to debugger: Broken pipe 10-19 12:17:37.267 1269-1617/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property 10-19 12:17:46.449 1269-1269/? E/EGL_emulation: tid 1269: eglCreateSyncKHR(1370): error 0x3004 (EGL_BAD_ATTRIBUTE) 10-19 12:17:47.050 2393-2501/com.app E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaa112700                                                                                                                   [ 10-19 12:17:47.094  2393: 2842 D/         ]                                                          HostConnection::get() New Host Connection established 0xb4050b90, tid 2842 10-19 12:17:47.222 1899-2797/com.android.inputmethod.latin E/Surface: getSlotFromBufferLocked: unknown buffer: 0xae4428c0 10-19 12:17:48.704 2393-2769/com.app E/UncaughtException: java.lang.AssertionError                                                                        at com.google.a.b.a.m$a.(Unknown Source)                                                                        at com.google.a.b.a.m.a(Unknown Source)                                                                        at com.google.a.f.a(Unknown Source)                                                                        at com.google.a.b.a.b.a(Unknown Source)                                                                        at com.google.a.f.a(Unknown Source)                                                                        at com.google.a.b.a.i.a(Unknown Source)                                                                        at com.google.a.b.a.i.a(Unknown Source)                                                                        at com.google.a.b.a.i.(Unknown Source)                                                                        at com.google.a.b.a.i.a(Unknown Source)                                                                        at com.google.a.b.a.i.a(Unknown Source)  

如此处所述,我在构建 gradle 中声明 API 键: Google Maps Signed APK Android

我如何初始化地图的代码示例:

public class MainMapFragment extends BaseFragment implements{...
    @Override
public void onCreate(Bundle savedInstanceState) {
    Log.d(Consts.TAGS.FRAG_MAIN_MAP,"BEGIN onCreate()");
    super.onCreate(savedInstanceState);

    FragmentManager fm = getChildFragmentManager();
    _mapFragment = (SupportMapFragment) fm.findFragmentByTag(Consts.TAGS.UTIL_MAP);
    if (_mapFragment == null) {
        Log.d(Consts.TAGS.FRAG_MAIN_MAP,"mapFragment is null. creating new map...");
        _mapFragment = SupportMapFragment.newInstance();
        fm.beginTransaction().replace(R.id.map_container, _mapFragment).commit();
        _mapFragment.getMapAsync(this);
    }
@Override
public void onMapReady(GoogleMap googleMap) {
    Log.d(Consts.TAGS.FRAG_MAIN_MAP,"BEGIN onMapReady()");
    _map = googleMap;
    //_map.setInfoWindowAdapter(this);
    _map.setInfoWindowAdapter(new CDInfoWindowAdapter(getActivity(),_markersPos));
    _map.setOnInfoWindowClickListener(this);
    _map.setPadding(120, 120, 170, 200);
    MapsInitializer.initialize(getActivity());        
    initilizeMap();
}

}

这是 Fragment 布局:

<RelativeLayout tools:context=".MainMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map_container"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
 <!--<fragment-->
        <!--android:id="@+id/map"-->
        <!--android:name="com.google.android.gms.maps.SupportMapFragment"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="match_parent"-->
        <!--/>-->
 <!--<com.google.android.gms.maps.MapView-->
     <!--xmlns:android="http://schemas.android.com/apk/res/android"-->
     <!--android:layout_width="match_parent"-->
     <!--android:layout_height="match_parent"-->
     <!--android:id="@+id/map_view"-->
     <!--/>
-->
</RelativeLayout>

如果代码在 运行 模式下工作但在 'Build APK' 模式下不工作,可能是什么问题?

您的 apk 地图密钥似乎有问题。您是否为您的包生成了一个新密钥以及您用于构建 apk 的密钥?

您已经找到了获取签名 APK 的 SHA-1 密钥的正确方法。我认为,您应该检查开发人员控制台上的注册密钥并将两个密钥都放在那里:调试和发布。如果两者都存在,请查找包名称,确保它是清单文件中定义的名称。然后等待几分钟,直到服务器配置您的密钥。希望能帮助到你。祝你好运

我做了什么来更正它(因为应用程序尚未准备好生产并且 Google API 和 SDK 尝试使用与 apk 构建类型无关的密钥)正在配置gradle 按照我的预期构建工作:

    buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        minifyEnabled false
        **debuggable true**
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        resValue "string", "google_maps_api_key", "***************"
    }
}