从play商店下载的应用程序和从安卓工作室安装的应用程序之间存在差异



好的,嗨。

我有一个应用程序,它使用了一些JPG和PNG图像,如果我从安卓工作室安装该应用程序,图像会显示得很好,但当我下载并从play商店安装时,图像显示为黑色方块。

首先,我认为这是因为所有图像都存储在文件夹mdpi中,我上传了一个aab文件到播放商店,所以我认为问题是我必须将文件复制到所有可绘制的文件夹:ldpi、mdpi、hdpi、xhdpi、xxhdpi、xxxhdpi和通用可绘制文件夹中,但问题仍然存在。

然后我创建了一个apk文件,并将其直接安装在我的智能手机中,问题就在那里,所以我认为问题与谷歌播放控制台无关,也与aab格式无关。

我在那个文件夹中有其他图像显示得很好。图像没有损坏,如果我从安卓工作室安装应用程序,它们可以正常工作。

我不知道哪些文件可能有用,但我会留下模块级别的清单和build.gradle:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myapppackage">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:name="com.the.name.of.application"
android:theme="@style/Theme.MyStyle">
<activity android:name="com.the.name.of.the.activity" android:configChanges="orientation|screenSize|keyboardHidden" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.my.application.id"
minSdkVersion 15
targetSdkVersion 30
versionCode 5
versionName "1.2.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
packagingOptions {
exclude 'META-INF/LICENSE'
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.8.5'
implementation 'com.fasterxml.jackson.core:jackson-core:2.8.5'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.8.5'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.code.gson:gson:2.8.5'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
}

------------------这是一篇讨论问题解决方案的编辑文章-------------事实证明,我的问题是proguard规则,出于某种原因,android决定我没有使用原始文件夹中存储的90%的图像和文件,并将它们排除在项目之外。由于收缩代码和资源的过程仅在发布版本中应用,当我从android studio构建并运行该项目时,该项目运行良好,但当生成并安装apk或aab时出现问题。

也许我的图像被排除在项目之外,因为我没有直接访问它们,我将它们的名称存储在原始资源中的JSON文件中,然后我读取文件以获取名称。

解决方案只是设置minifyEnabled falseshrinkResources false属性。

还有其他解决方案,比如编写新的proguard规则或添加带有标签的xml文件,但这一个更快

将这些权限添加到您的清单文件:

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

然后在你的活动中,你必须将此权限授予你的应用程序,如

private static final int EXTERNAL_STORAGE_REQUEST = 1;
private static String[] STORAGE_PERMISSIONS = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};

/* do the app has permission to write to device storage?
*
* if not then the user will be asked to grant permissions
*
*/
public static void getStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
STORAGE_PERMISSIONS,
EXTERNAL_STORAGE_REQUEST
);
}
}

如果许可被授予,你应该做点什么

@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the results are always empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted Do the
// image displaying things you want to do.
} else {
// permission denied. if there is other option to continue 
//without permission do it here or finish the activity and ask 
//for permission in another run. maybe it was a misclick yay?

}
return;
}

}
}

相关内容

最新更新