使用 Joda Time 时无法生成启用 proguard 的签名 APK

Posted

技术标签:

【中文标题】使用 Joda Time 时无法生成启用 proguard 的签名 APK【英文标题】:Unable to generate signed APK with proguard enabled when Joda Time is used 【发布时间】:2016-07-25 02:38:43 【问题描述】:

我尝试构建一个签名的 APK。我也在使用 proguard。在我的应用程序中,我出于某些目的使用了 JodaTime。我在那里使用了 joda-time-2.7.jar。首先,我可以在不启用 proguard 的情况下获得签名的 APK,没有错误。然后我启用了 proguard 并尝试生成 APK,但它产生了错误。以下是生成的错误。

Warning:org.joda.time.LocalDateTime: can't find referenced class org.joda.convert.ToString
Warning:org.joda.time.LocalTime: can't find referenced class org.joda.convert.FromString
Warning:org.joda.time.LocalTime: can't find referenced class org.joda.convert.ToString
Warning:org.joda.time.Minutes: can't find referenced class org.joda.convert.FromString
Warning:org.joda.time.Minutes: can't find referenced class org.joda.convert.ToString
Warning:org.joda.time.MonthDay: can't find referenced class org.joda.convert.FromString
Warning:org.joda.time.MonthDay: can't find referenced class org.joda.convert.ToString
Warning:org.joda.time.Months: can't find referenced class org.joda.convert.FromString
Warning:org.joda.time.Months: can't find referenced class org.joda.convert.ToString
Warning:org.joda.time.MutableDateTime: can't find referenced class org.joda.convert.FromString
Warning:org.joda.time.MutablePeriod: can't find referenced class org.joda.convert.FromString
Warning:org.joda.time.Period: can't find referenced class org.joda.convert.FromString
Warning:org.joda.time.Seconds: can't find referenced class org.joda.convert.FromString
Warning:org.joda.time.Seconds: can't find referenced class org.joda.convert.ToString
Warning:org.joda.time.Weeks: can't find referenced class org.joda.convert.FromString
Warning:org.joda.time.Weeks: can't find referenced class org.joda.convert.ToString
Warning:org.joda.time.YearMonth: can't find referenced class org.joda.convert.FromString
Warning:org.joda.time.YearMonth: can't find referenced class org.joda.convert.ToString
Warning:org.joda.time.Years: can't find referenced class org.joda.convert.FromString
Warning:org.joda.time.Years: can't find referenced class org.joda.convert.ToString
Warning:org.joda.time.base.AbstractDateTime: can't find referenced class org.joda.convert.ToString
Warning:org.joda.time.base.AbstractDuration: can't find referenced class org.joda.convert.ToString
Warning:org.joda.time.base.AbstractInstant: can't find referenced class org.joda.convert.ToString
Warning:org.joda.time.base.AbstractPeriod: can't find referenced class org.joda.convert.ToString
Warning:there were 37 unresolved references to classes or interfaces.
     You may need to add missing library jars or update their versions.
     If your code works fine without the missing classes, you can suppress
     the warnings with '-dontwarn' options.
     (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedclass)
Exception while processing task java.io.IOException: Please correct the above warnings first.
  at proguard.Initializer.execute(Initializer.java:473)
  at proguard.ProGuard.initialize(ProGuard.java:233)
  at proguard.ProGuard.execute(ProGuard.java:98)
  at proguard.gradle.ProGuardTask.proguard(ProGuardTask.java:1074)
  at com.android.build.gradle.tasks.AndroidProGuardTask.doMinification(AndroidProGuardTask.java:139)
  at com.android.build.gradle.tasks.AndroidProGuardTask$1.run(AndroidProGuardTask.java:115)
  at com.android.builder.tasks.Job.runTask(Job.java:48)
  at com.android.build.gradle.tasks.SimpleWorkQueue$EmptyThreadContext.runTask(SimpleWorkQueue.java:41)
  at com.android.builder.tasks.WorkQueue.run(WorkQueue.java:227)
  at java.lang.Thread.run(Thread.java:745)

下面是我的 build.gradle 文件:

apply plugin: 'com.android.application'

android 
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig 
        applicationId "com.theacetechnologies.voicetype"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    
    buildTypes 
        release 
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        
    


dependencies 
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile files('libs/joda-time-2.7.jar')

知道为什么我在启用 proguard 时无法生成单点 APK 吗?我相信这也与 Joda Time 有关,因为我之前在没有 JODA 时间的情况下生成了启用 proguard 的签名 APK。

【问题讨论】:

请提供您的 proguard conf。您需要为 Joda 类设置例外,否则 proguard 也会混淆它们。最终出现此错误。 并且您的依赖项未配置,第 1 行包含第 5 行;)因为 fileTree(...) 包含 libs 文件夹中的所有 jar。 【参考方案1】:

将这些行添加到您的 proguard 文件中

-dontwarn org.joda.convert.**
-dontwarn org.joda.time.**
-keep class org.joda.time.**  *; 
-keep interface org.joda.time.**  *;

【讨论】:

以上是关于使用 Joda Time 时无法生成启用 proguard 的签名 APK的主要内容,如果未能解决你的问题,请参考以下文章

HSQLDB + EclipseLink + 生成 DDL + DATE JODA TIME = 错误的 TIMESTAMP

无法使用 Joda Time API 将日期转换为 UTC [关闭]

杰克逊未能将字符串反序列化为 Joda-Time

如何在没有 Joda Time 的情况下在 Java 7 中正确处理夏令时?

joda 时间与 java 时间的默认语言环境

Joda-Time 学习笔记