Android Wear 操作系统 - AmbientModeSupport 在发布模式下无法正常工作

发布于 2025-01-12 03:32:55 字数 1016 浏览 1 评论 0原文

我正在为 Androi Wear OS 应用程序使用 AmbientModeSupport:https://developer. android.com/training/wearables/overlays/always-on

刚刚完成实施/测试,一切都按预期进行。 应用程序发布后,我注意到从 Google Play 下载应用程序时该功能无法正常工作。

我做了一些额外的测试:

  • 如果应用程序安装在调试模式下,一切都会正常。
  • 如果应用程序以发布模式安装,则 Ambient 功能无法正常工作。

在释放模式下,屏幕只是降低亮度,但 UI 上没有任何变化(在 onEnterAmbient 中执行的更改)。 AndroidManifest 中已提供 WAKE_LOCK 权限:

<uses-permission android:name="android.permission.WAKE_LOCK" />

有什么想法吗?


更新: 在我认为我完全没有想法之后,我意识到我的 build.gradle 对于发布模式有 2 个额外的东西,即 proguard 和 minify 标志:

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

似乎 minifyEnabled 导致了问题。我不知道为什么会出现这种情况,但如果我删除 minifyEnabled 标志,环境模式就会正常工作。

I'm using AmbientModeSupport for an Androi Wear OS application: https://developer.android.com/training/wearables/overlays/always-on

Just finished implementing/testing it and everything worked as expected.
After app release I noticed that the feature is not working properly when the app is downloaded from Google Play.

I did some extra testing and:

  • Everything works if app is installed in debug-mode.
  • The Ambient feature is not working well if app is installed in release-mode.

In release-mode, the screen just lowers it's brightness, but there are no changes on the UI (changes performed in onEnterAmbient).
The WAKE_LOCK permission is already provided in AndroidManifest:

<uses-permission android:name="android.permission.WAKE_LOCK" />

Any ideas?


UPDATE:
After I thought I'm completely out of ideas, I realized that my build.gradle has 2 extra things for the release-mode, the proguard and the minify flags:

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

It seems that the minifyEnabled caused the problem. I don't know why this behavior occured, but the Ambient Mode is working if I remove the minifyEnabled flag.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天涯离梦残月幽梦 2025-01-19 03:32:56

进一步更新:
关闭缩小当然会带来使您的应用程序变得更大的额外问题。以下是 Flutter 的设置,以便在发布模式下启用缩小,并且还允许环境模式支持。这假设您正在使用标准 pub.dev 包“wear”(这还包括保留“wear_os_plugin”和“wearable_rotary”包所需的类的命令)。
我们在此处创建/添加到选项的“my-proguard-rules.pro”文件将指示构建不删除支持环境模式 pub.dev 包所需的 kotlin/java 类。

android\app\build.gradle 的 buildTypes 部分的更改:

 buildTypes {
   release {
     signingConfig signingConfigs.release

     // THIS will make Ambient mode work, but app is larger by around 15 mb..
     //minifyEnabled false
     //shrinkResources false

     minifyEnabled true
     shrinkResources true
     // optimized default: 'proguard-android-optimize.txt'
     //      or unoptimized default: 'proguard-android.txt')
     proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'my-proguard-rules.pro'
   }
 }

以及您将在 `android\app\my-proguard-rules.pro' 中创建的文件 new 文件:

# Flutter
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.** { *; }
-keep class io.flutter.util.** { *; }
-keep class io.flutter.view.** { *; }
-keep class io.flutter.** { *; }
-keep class io.flutter.plugins.** { *; }

# Keep all these android wear package classes 
# (these are from the packages wear, wear_os_plugin and wearable_rotary)
-keep class com.mjohnsullivan.flutterwear.wear.** { *; }
-keep class org.qooapps.wear_os_plugin.** { *; }
-keep class com.samsung.wearable_rotary.** { *; }

Further update:
Turning off minifying of course has the added problem of making your app larger. Here are the settings for Flutter in order to enable minifying in release mode and ALSO allow ambient mode support. This assumes that you are using the standard pub.dev packages 'wear' (and this also includes commands to keep classes needed for the 'wear_os_plugin' and 'wearable_rotary' packages).
The 'my-proguard-rules.pro' file that we create/add to the options here will direct the build to NOT remove the kotlin/java classes that we need for support of the ambient mode pub.dev packages.

Changes to buildTypes section of android\app\build.gradle:

 buildTypes {
   release {
     signingConfig signingConfigs.release

     // THIS will make Ambient mode work, but app is larger by around 15 mb..
     //minifyEnabled false
     //shrinkResources false

     minifyEnabled true
     shrinkResources true
     // optimized default: 'proguard-android-optimize.txt'
     //      or unoptimized default: 'proguard-android.txt')
     proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'my-proguard-rules.pro'
   }
 }

and in the file new file you will create in `android\app\my-proguard-rules.pro' :

# Flutter
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.** { *; }
-keep class io.flutter.util.** { *; }
-keep class io.flutter.view.** { *; }
-keep class io.flutter.** { *; }
-keep class io.flutter.plugins.** { *; }

# Keep all these android wear package classes 
# (these are from the packages wear, wear_os_plugin and wearable_rotary)
-keep class com.mjohnsullivan.flutterwear.wear.** { *; }
-keep class org.qooapps.wear_os_plugin.** { *; }
-keep class com.samsung.wearable_rotary.** { *; }

夏雨凉 2025-01-19 03:32:55

我在 Flutter 应用程序中遇到了同样的问题,并且我的 build.gradle 文件没有明确将 minifyEnabled 设置为 true,但它是发布模式下的默认设置。
对于 flutter,您还需要将 shr​​inkResources 设置为 false(因为在发布模式下它也默认设置为 true,如果 minifyEnabled 为 false,则 shr​​inkResources code> 也必须为 false,如果不这样做,Flutter 将在构建时出错)。

(在 android\app\build.grade 中):

buildTypes {
    release {
        minifyEnabled false                   <<< add these
        shrinkResources false                 <<< lines
    }
}

一旦我完成了此环境模式支持,也可以在发布模式下工作。

I was having the same issue within my Flutter app, and my build.gradle file did not explicitly have minifyEnabled set to true, but it is the default in release mode.
For flutter you also need to set shrinkResources to false (because it is set to true by default also in release mode, and if minifyEnabled is false then shrinkResources must be also be false. Flutter will error on build if you do not do this).

(within android\app\build.grade):

buildTypes {
    release {
        minifyEnabled false                   <<< add these
        shrinkResources false                 <<< lines
    }
}

Once I did this Ambient mode support also worked in release mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文