【问题标题】:Application Stopped on GPS Location Request Programmatically in Released APK应用程序在已发布的 APK 中以编程方式停止 GPS 位置请求
【发布时间】:2018-07-29 13:22:07
【问题描述】:

更新

忘了说这是一个 xamarin 编码。

以防万一:
我将 android 选项中的链接选项从无更改为仅 sdk 程序集
我还启用了 proguard


我在位置请求方面遇到问题,但仅在发布的 apk 中,它总是说应用程序已停止。我尝试调试它并找到发生异常的部分。我的应用程序在调试模式下运行良好,所以我不知道为什么它在发布的 apk 测试期间遇到异常。使用 Asus zenfone 3 Nougat 和 OPPO lollipop 测试,结果相同(发布失败,调试没有问题)。

这是我的部分代码

    public void m4_setUpAllClickable()
    {
        try
        {
            btnEnableGPS.Click += delegate
            {
                enableGPSLocation();
            };
        }
        catch { throw; }
    }

    private async void enableGPSLocation()
    {
        try
        {
            GoogleApiClient
                googleApiClient = new GoogleApiClient.Builder(this)
                    .AddApi(LocationServices.API)
                    .Build();

            googleApiClient.Connect();

            LocationRequest
                locationRequest = LocationRequest.Create()
                    .SetPriority(LocationRequest.PriorityHighAccuracy)
                    .SetSmallestDisplacement(LocationValues.minimunDisplacement)
                    .SetInterval(LocationValues.normalInterval)
                    .SetFastestInterval(LocationValues.fastestInterval);

            LocationSettingsRequest.Builder
                locationSettingsRequestBuilder = new LocationSettingsRequest.Builder()
                    .AddLocationRequest(locationRequest);

            locationSettingsRequestBuilder.SetAlwaysShow(false);

//****** -> Problem Occur in this part and I don't know why since it is going straight to app has stopped instead of catch exception
            LocationSettingsResult
                locationSettingsResult = await LocationServices.SettingsApi.CheckLocationSettingsAsync(
                    googleApiClient, locationSettingsRequestBuilder.Build());
//****** -> ends here

            if (locationSettingsResult.Status.StatusCode == CommonStatusCodes.ResolutionRequired)
            {
                locationSettingsResult.Status.StartResolutionForResult(this, 1);
            }
            else if (locationSettingsResult.Status.StatusCode == CommonStatusCodes.Success)
            {
                if (activitySource == "GPS Failed")
                {
                    Intent intent;
                    intent = new Intent(this, typeof(Activity_AssignedList));
                    StartActivity(intent);
                    FinishAffinity();
                }
                else if (activitySource == "GPS Disabled")
                {
                    Finish();
                }
            }
        }
        catch { throw; }
    }

【问题讨论】:

  • 异常是什么?
  • 1) 查看logcat 输出以确定原因 2) 您是否为发布版本打开了链接器?如果是,请将其关闭并重新测试,因为它可能是未找到的类/方法异常(查看logcat 确定)
  • @SushHangover,您的意思是关于链接器是仅使用链接选项 sdk 程序集的 android 选项设置吗?我还启用了我的 proguard,以防万一
  • @jace “可能性”是否与所需的 Linker 和/或 Proguard 剥离类/方法有关,您需要查看 logcat 输出以确定这一点。
  • @SushiHangover 感谢您的帮助。我会看看是不是因为proguard和linker。明天我会更新我对 logcat 的问题。我现在只需要出去工作。

标签: android xamarin xamarin.android android-gps


【解决方案1】:

您是否在项目中添加了 proguard 规则?如果没有,请尝试此操作并重新运行您的发布版本

-keep public class com.google.android.gms.* { public *; }
-dontwarn com.google.android.gms.**

【讨论】:

【解决方案2】:

正如@SushiHangover 在评论中所建议的那样,我尝试将我的应用程序链接选项从仅 sdk 程序集更改为无,并且它在已发布的 apk 上运行良好。我不知道为什么在本教程中:https://developer.xamarin.com/guides/android/deployment,_testing,_and_metrics/publishing_an_application/part_1_-_preparing_an_application_for_release/ 它说要启用 proguard 并将链接选项从无更改为 sdk 程序集,所以我这样做是为了使我的应用程序更小。它从 66mb 下降到 40mb,所以我认为这是一件好事,但问题是它削减了我的应用程序需要的一些类。

到目前为止,它在没有链接选项的情况下工作,并且我的 proguard 仍然处于启用状态。感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多