【问题标题】:Android App Bundle introduces crashes and does not respect screenDensity?Android App Bundle 引入崩溃,不尊重 screenDensity?
【发布时间】:2019-08-01 07:52:46
【问题描述】:

我得到的 Resources$NotFoundException 很像 thisthis question

错误是这样的-

Caused by android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f080166
   at android.content.res.ResourcesImpl.getResourceName + 267(ResourcesImpl.java:267)
   at android.content.res.ResourcesImpl.loadDrawableForCookie + 831(ResourcesImpl.java:831)
   at android.content.res.ResourcesImpl.loadDrawable + 677(ResourcesImpl.java:677)
   at android.content.res.Resources.loadDrawable + 912(Resources.java:912)
   at android.content.res.TypedArray.getDrawableForDensity + 955(TypedArray.java:955)
   at android.content.res.TypedArray.getDrawable + 930(TypedArray.java:930)
   at android.widget.TextView.(TextView.java:1317)
   at android.widget.TextView.(TextView.java:1112)
   at android.support.v7.widget.AppCompatTextView.(AppCompatTextView.java:87)
   at android.support.v7.widget.AppCompatTextView.(AppCompatTextView.java:83)
   at com.myapp.myapp.widgetstore.CustomTextView.(CustomTextView.java:24)
   at com.myapp.myapp.widgetstore.roboto.RobotoTextView.(RobotoTextView.java:40)
   at java.lang.reflect.Constructor.newInstance0(Constructor.java)
   at java.lang.reflect.Constructor.newInstance + 343(Constructor.java:343)
   at android.view.LayoutInflater.createView + 686(LayoutInflater.java:686)
   at android.view.LayoutInflater.createViewFromTag + 829(LayoutInflater.java:829)
   at android.view.LayoutInflater.createViewFromTag + 769(LayoutInflater.java:769)
   at android.view.LayoutInflater.rInflate + 902(LayoutInflater.java:902)
   at android.view.LayoutInflater.rInflateChildren + 863(LayoutInflater.java:863)
   at android.view.LayoutInflater.parseInclude + 1034(LayoutInflater.java:1034)
   at android.view.LayoutInflater.rInflate + 898(LayoutInflater.java:898)
   at android.view.LayoutInflater.rInflateChildren + 863(LayoutInflater.java:863)
   at android.view.LayoutInflater.rInflate + 905(LayoutInflater.java:905)
   at android.view.LayoutInflater.rInflateChildren + 863(LayoutInflater.java:863)
   at android.view.LayoutInflater.inflate + 554(LayoutInflater.java:554)
   at android.view.LayoutInflater.inflate + 461(LayoutInflater.java:461)
   at android.view.LayoutInflater.inflate + 383(LayoutInflater.java:383)
   at android.support.v7.app.AppCompatDelegateImpl.setContentView + 469(AppCompatDelegateImpl.java:469)
   at android.support.v7.app.AppCompatActivity.setContentView + 140(AppCompatActivity.java:140)
   at com.myapp.myapp.core.bottomnavigation.BottomNavigationBaseActivity.onCreate + 163(BottomNavigationBaseActivity.java:163)
   at android.app.Activity.performCreate + 7327(Activity.java:7327)
   at android.app.Activity.performCreate + 7318(Activity.java:7318)
   at android.app.Instrumentation.callActivityOnCreate + 1275(Instrumentation.java:1275)
   at android.app.ActivityThread.performLaunchActivity + 3101(ActivityThread.java:3101)
   at android.app.ActivityThread.handleLaunchActivity + 3264(ActivityThread.java:3264)
   at android.app.servertransaction.LaunchActivityItem.execute + 78(LaunchActivityItem.java:78)
   at android.app.servertransaction.TransactionExecutor.executeCallbacks + 108(TransactionExecutor.java:108)
   at android.app.servertransaction.TransactionExecutor.execute + 68(TransactionExecutor.java:68)
   at android.app.ActivityThread$H.handleMessage + 1955(ActivityThread.java:1955)
   at android.os.Handler.dispatchMessage + 106(Handler.java:106)
   at android.os.Looper.loop + 214(Looper.java:214)
   at android.app.ActivityThread.main + 7078(ActivityThread.java:7078)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 493(RuntimeInit.java:493)
   at com.android.internal.os.ZygoteInit.main + 964(ZygoteInit.java:964)

在讨论之后,我实现了side-loading check,但由于某种原因它还不够,因为第一个活动无法膨胀并抛出 InflateException。我创建了一个解决方法并在 catch 块中添加了一个manual check。像这样-

@Override
public void onCreate(Bundle savedInstanceState) {
    try{
        // the regular stuff
    } catch (Exception e){
        if(!isValidInstallation()){
            startInstallFromPlaystoreActivity();
        } else {
            throw e;
        }
    }
}

private boolean isValidInstallation(){
    boolean isValid = true;
    try {
        String installer = this.getPackageManager()
                .getInstallerPackageName(this.getPackageName());
        isValid = !TextUtils.isEmpty(installer);
    } catch (Throwable e) {
        isValid = false;
    }
    return isValid;
}

private void startInstallFromPlaystoreActivity(){
    startActivity(new Intent(this, InstallFromPlaystore.class));
}

这是目前有效的方法,但我正在寻找更清洁的东西。为什么 Google 推荐的方法不能解决我的问题?

为了测试这一点,我创建了一个包并使用此规范构建了 apk -

{
   "supportedAbis": ["x86"],
   "supportedLocales": ["en-US"],
   "screenDensity": 50,
   "sdkVersion": 24
}

这肯定会导致我的应用程序因同样的错误而崩溃。根据设备将 screenDensity 更新为正确的值不再导致崩溃。

如果我使用supportedLocales,我可以触发Google 提供的Missing Split APK 功能。

我也怀疑对于较低的屏幕密度,我拥有的自定义 RobotoTextView 无法访问支持库中涟漪效应的正确资源。会这样吗?

谢谢

【问题讨论】:

  • 您是否在文本视图中使用了可绘制的矢量?
  • 不。我不这么认为。
  • 或任何类型的drawables?因为当我在文本视图中使用可绘制对象并且仅在某些设备中获得崩溃报告时,我遇到了类似的问题..
  • 我确定使用支持库中的涟漪的 RobotoTextView 是它崩溃的地方。可能是因为侧载。
  • 就我而言,崩溃并非特定于某些设备

标签: android crash google-play android-app-bundle sideloading


【解决方案1】:

更改上传格式后。您是否设置了以下值,特别是density = true。我们通常将所有资源放在所有 drawable-hdpi/xhdpi/xxhdpi 变体中。但是如果说只有 hdpi 文件夹中有一个资源,并且您使用 App bundle 创建了一个 APK。 App bundle 为 hdpi、xhdpi... 创建单独的 APK,这意味着在 xhdpi 设备中它只会使用 drawable-xhdpi 文件夹的资源。因此,如果您只有在 drawable-hdpi 中的资源将无法在 xhdpi 设备上使用,因此会崩溃。

bundle {
   language {
       enableSplit = true
   }
   density {
       enableSplit = true
   }
   abi {
       enableSplit = true
   }
}

解决方案我们这里有两个解决方案一个:通过将其设置为 false 来禁用密度拆分或确保每个资源在每个可绘制文件夹中都可用,并在不同密度的设备上进行测试。 ABI 也是如此。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    相关资源
    最近更新 更多