1

我在我的应用程序中使用谷歌地图。我尝试在包含地图的片段内使用 MapView 和 SupportMapFragment。出现的问题导致谷歌地图在地图初始化期间“构建 APK”过程(在我测试过的所有设备中)后崩溃,但从 Android Studio 运行(使用运行)时,它按预期工作。这是堆栈跟踪(我在所有执行中得到的都是 AssertionError):

10-19 12:17:27.244 2256-2638/com.google.android.gms E/BaseAppContext: 试图停止全局 GMSCore RequestQueue。这可能是无意的,所以忽略。10-19 12:17:28.640 1921-1921/com.google.android.gms.persistent E/BluetoothAdapter:蓝牙绑定器为空 10-19 12:17:29.537 1921-1921/com.google.android.gms.persistent E/BluetoothAdapter:蓝牙绑定器为空 10-19 12:17:29.542 2256-2677/com.google.android.gms E/MDM:[142] rpv.a:无法连接到 Google API 客户端:ConnectionResult{statusCode =API_UNAVAILABLE,分辨率=null,消息=null} 10-19 12:17:30.112 1921-1921/com.google.android.gms.persistent E/ChimeraRcvrProxy:找不到 Chimera 接收器 impl 类 com.google.android。 gms.auth.setup.devicesignals.LockScreenChimeraReceiver,丢弃广播 10-19 12:17:31.602 2393-2501/com.app E/Surface:getSlotFromBufferLocked:未知缓冲区:0xaa112310 10-19 12:17:36.351 2776-2782/?E/art:向调试器发送回复失败:Broken pipe 10-19 12:17:37.267 1269-1617/? E/SurfaceFlinger: ro.sf.lcd_density 必须定义为构建属性 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: 未知缓冲区: 0xaa112700 [10-19 12:17: 47.094 2393: 2842 D/] HostConnection::get() 新主机连接建立 0xb4050b90, tid 2842 10-19 12:17:47.222 1899-2797/com.android.inputmethod.latin E/Surface: getSlotFromBufferLocked:  

如此处所述,我在构建 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();
}

}

这是片段布局:

<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>

如果代码在运行模式下工作但不在“构建 APK”模式下,可能会出现什么问题?

4

3 回答 3

2

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

于 2016-10-20T06:14:41.267 回答
1

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

于 2016-10-20T05:54:37.397 回答
0

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

    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", "***************"
    }
}
于 2016-10-30T06:21:41.953 回答