Android gmaps无法使用minifyEnabled = true ... apiKey已设置

时间:2017-04-05 20:26:32

标签: android google-maps proguard

我不会说英语,但是......

我正在制作一款使用GMaps v2的Android应用。 一切都处于调试模式,但是当我在发布应用程序中午餐时,地图代码不会执行!

我确定发布的apiKey设置得很好(我在src / release / values / google_maps_api.xml中设置它并且apiKey没有限制),因为如果我在发布构建类型中设置minifyEnabled = false ,它有效!所以我认为这是proguard的一个问题......

我尝试了下一个proguard规则的所有组合,但没有...

-keep class com.google.** {*;}

-keep class com.hawk.angela.home.activities.MapFragment { *; }


-keep class com.google.android.gms.maps.** { *; }
-keep interface com.google.android.gms.maps.** { *; }

-keep class * extends java.util.ListResourceBundle { protected Object[][] getContents(); }
-keepattributes InnerClasses
-dontoptimize

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Fragment
-keep public class * extends android.support.v4.app.Fragment

# The Maps API uses custom Parcelables.
# Use this rule (which is slightly broader than the standard recommended one)
# to avoid obfuscating them.
-keepclassmembers class * implements android.os.Parcelable {
    static *** CREATOR;
}

# The Maps API uses serialization.
-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

此外,控制台不会返回任何异常

地图初始化位于片段中:

MapView mapView = (MapView) rootView.findViewById(R.id.mapView);
Bundle mapState = (savedInstanceState != null) ?
        savedInstanceState.getBundle("mapState") :
        null;
mapView.onCreate(mapState);
mapView.onResume(); // needed to get the map to display immediately

mapView.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(GoogleMap mMap) {
        googleMap = mMap;
        googleMap.setMaxZoomPreference(maxZoom);

        // ...
    }
});

有人能帮帮我吗?至少我必须找到问题...

谢谢!

编辑: AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.enzonotario.app">

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-feature
        android:name="android.hardware.location.network"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.location.gps"
        android:required="false" />

    <!-- GCM permissions -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <!-- Pusher service -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <!-- Storage -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="DATABASE"
            android:value="app.db" />
        <meta-data
            android:name="VERSION"
            android:value="2" />
        <meta-data
            android:name="QUERY_LOG"
            android:value="false" />
        <meta-data
            android:name="DOMAIN_PACKAGE_NAME"
            android:value="com.enzonotario.app" />

        <meta-data
            android:name="com.enzonotario.app.utils.GlideConfigurationModule"
            android:value="GlideModule" />

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyAqRP3ZQzc9gOhs3elhs7AL0qUo6wQOwZ8" />

        <!-- Pusher's FCM listeners and services -->
        <service android:name="com.pusher.android.notifications.fcm.FCMMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <service android:name="com.pusher.android.notifications.fcm.FCMInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".SyncService"
            android:exported="false" />

        <receiver android:name=".gps.receivers.GpsLocationReceiver">
            <intent-filter>
                <action android:name="android.location.PROVIDERS_CHANGED" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

        // so many activities
    </application>
</manifest>

google_maps_api.xml(发布)

<resources>
    <!--
    TODO: Before you release your application, you need a Google Maps API key.

    To do this, you can either add your release key credentials to your existing
    key, or create a new key.

    Note that this file specifies the API key for the release build target.
    If you have previously set up a key for the debug target with the debug signing certificate,
    you will also need to set up a key for your release certificate.

    Follow the directions here:

    https://developers.google.com/maps/documentation/android/signup

    Once you have your key (it starts with "AIza"), replace the "google_maps_key"
    string in this file.
    -->
    <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">
        AIzaSyAqRP3ZQzc9gOhs3elhs7AL0qUo6wQOwZ8
    </string>
</resources>

2 个答案:

答案 0 :(得分:0)

必须在AndroidManifest.xml中明确设置Google地图的API密钥才能生效。

对于调试模式,可以在google_maps_api.xml中设置但是对于发布版本,它必须在AndroidManifest.xml中

E.g。

<meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="<your API key here>"/>

我这样做时通常会创建两个键。一个用于调试,一个用于发布。这是必要的,因为调试的SHA对于发布是不同的,因此您需要为每个创建两个单独的API密钥。

所以我的AndroidManifest.xml看起来有点像下面的那个。当我将构建从调试转移到发布或发布到调试时,我只是取消注释元数据。

    <!-- Development/Debug Google Maps API Key -->
    <!--<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key"/> -->

    <!-- Release Google Maps API Key -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="<my API key here>"/>

答案 1 :(得分:0)

嗯,问题不在gmaps中,而是在我使用的materialtabs库中(https://github.com/neokree/MaterialTabs),现在我使用https://github.com/ogaclejapan/SmartTabLayout并且它完美无缺!