生成 APK 但未构建和部署时构建失败
Posted
技术标签:
【中文标题】生成 APK 但未构建和部署时构建失败【英文标题】:Build fails when generating APK but not building and deploying 【发布时间】:2018-04-08 07:30:33 【问题描述】:我的项目在我的模拟器上构建并运行,但当我尝试为我的项目生成 APK 时失败。我在尝试生成发布 APK 时收到以下错误:
警告:com.google.android.gms.gcm.GcmTaskService:找不到 程序类中引用的字段“android.os.IBinder zzaHj” com.google.android.gms.gcm.PendingCallback
警告:有 1 个 对程序类成员的未解决引用。
警告:异常 处理任务时 java.io.IOException: 请更正上述 首先警告。
错误:任务执行失败 ':app:transformClassesAndResourcesWithProguardForRelease'.
我的 proguard-rules.pro
-assumenosideeffects class android.util.Log
public static boolean isLoggable(java.lang.String, int);
public static int v(...);
public static int i(...);
public static int w(...);
public static int d(...);
public static int e(...);
# Attempted below to fix issue
#-keep public class com.google.android.gms.* public *;
#-dontwarn com.google.android.gms.**
Build.gradle
apply plugin: 'com.android.application'
repositories
google()
// Alternative attempt to resolve
// jcenter()
// maven
// url "https://maven.google.com"
//
dependencies
compile fileTree(include: ['*.jar'], dir: 'libs')
compile "com.android.support:support-v4:25.3.1"
compile "com.android.support:support-v13:25.3.1"
compile "com.android.support:cardview-v7:25.3.1"
compile "com.android.support:appcompat-v7:25.3.1"
compile 'com.google.firebase:firebase-auth:11.0.2'
compile 'com.google.firebase:firebase-database:11.0.2'
compile 'com.google.firebase:firebase-crash:11.0.2'
compile 'com.google.firebase:firebase-config:11.0.2'
compile 'com.google.firebase:firebase-storage:11.0.2'
compile 'com.google.firebase:firebase-messaging:11.0.2'
compile 'com.google.firebase:firebase-core:11.0.2'
compile 'com.google.android.gms:play-services:11.0.2'
compile 'com.firebase:firebase-jobdispatcher:0.6.0'
compile 'com.android.support:design:25.1.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
List<String> dirs = [
'main', // main sample code; look here for the interesting stuff.
'common', // components that are reused by multiple samples
'template'] // boilerplate code that is generated by the sample template process
android
compileSdkVersion 27
buildToolsVersion '27.0.0'
defaultConfig
applicationId "com.company.app"
minSdkVersion 21
targetSdkVersion 27
versionCode 12
versionName "1.11"
multiDexEnabled true
compileOptions
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
sourceSets
main
dirs.each dir ->
java.srcDirs "src/$dir/java"
res.srcDirs "src/$dir/res"
androidTest.setRoot('tests')
androidTest.java.srcDirs = ['tests/src']
buildTypes
release
minifyEnabled true // Enables code shrinking for the release build type.
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
debug
debuggable true
productFlavors
apply plugin: 'com.google.gms.google-services'
尝试修复
我已尝试将以下内容添加到我的proguard-rules.pro
(建议here):
-保持公共类 com.google.android.gms.* public *; -dontwarn com.google.android.gms.**
但这只会产生以下问题:
警告:处理任务 java.io.IOException 时出现异常:不能 写 [/Users/myName/Documents/myProjectFolder/app/build/intermediates/transforms/proguard/release/jars/3/1f/main.jar] (看不懂 [/Users/myName/.android/build-cache/a80bb41778b1f73h09e3t326jn804m46e280aw10/output/jars/classes.jar(;;;;;;**.class)] (重复的 zip 条目 [classes.jar:com/google/android/gms/gcm/PendingCallback.class]))
-
https://***.com/a/46047978/4206520
https://***.com/a/33953133/4206520
有谁知道为什么我的构建在生成 APK 时失败,但没有构建和部署(以及如何防止它发生)?
【问题讨论】:
你有"com.android.support:support-v4:25.3.1"
和"com.android.support:support-v13:25.3.1"
,也许这会产生问题。我认为添加最新的支持版本是最佳做法。
【参考方案1】:
请删除
compile 'com.google.android.gms:play-services:11.0.2'
来自dependencies
在build.gradle
文件中
使用下面链接中给出的依赖项中的各个模块 Set up Google Play Services
Duplicate Entry 错误意味着,您的项目中有两个相同的类,并且它们之间存在冲突
【讨论】:
感谢桑吉特的回复。我相信这可能是在正确的轨道上,但只有这样做(例如删除compile 'com.google.android.gms:play-services:11.0.2'
并添加compile 'com.google.android.gms:play-services-gcm:11.4.2'
)会导致错误Error:(37, 13) Failed to resolve: com.google.android.gms:play-services-gcm:11.4.2
然后当我单击提示“安装存储库并同步项目”时没有任何反应"
您可以从 Android SDK 管理器本身安装 google play 服务以上是关于生成 APK 但未构建和部署时构建失败的主要内容,如果未能解决你的问题,请参考以下文章
尝试将带有顺风的 MERN 堆栈部署到 Heroku 时构建失败