尽管可用 proguard.txt 禁止混淆,但 AAR 依赖项仍被混淆

AAR dependency is obfuscated in spite of available proguard.txt forbidding an obfuscation

我们有一个编译为 AAR 并存储在 Maven 存储库中的工件。 我们的工件可以添加为依赖

compile ('com.ourcompany:sdk:2.1.16@aar') {
    transitive = true
}

我们在 build.gradle 脚本中有以下设置。

android {
   compileSdkVersion 23
   buildToolsVersion "23.0.2"

   defaultConfig {
       ...
       consumerProguardFiles 'proguard-rules.pro'
   }
}

文件 proguard-rules.pro 包含以下规则

# Common
-dontoptimize
-dontobfuscate
-dontpreverify
-optimizations !code/allocation/variable
-keep public class * {
    public protected *;
}

# attributes
-keepattributes InnerClasses, Signature, *Annotation*, Exceptions

# java
-dontwarn javax.naming.**
-dontwarn java.lang.invoke**
-dontwarn java.nio.file.Files
-dontwarn java.nio.file.Path
-dontwarn java.nio.file.OpenOption
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

# retrofit
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-dontwarn com.squareup.okhttp.**
-dontwarn rx.**
-dontwarn retrofit.**
-keep class retrofit.** { *; }
-keepclasseswithmembers class * {
    @retrofit.http.* <methods>;
}

# gson
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }

# otto
-keepclassmembers class ** {
    @com.squareup.otto.Subscribe public *;
    @com.squareup.otto.Produce public *;
}

# google play services
-keep public class com.google.android.gms.* { public *; }
-dontwarn com.google.android.gms.**

# support
-dontwarn android.support.v7.**
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }

我们正在尝试将库作为依赖项添加到应用程序中,该应用程序包含在 build.gradle 中,设置如下

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
       ...
       proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

buildTypes {
    release {
        minifyEnabled true
    }

    debug {
        minifyEnabled true
    }
}

dependencies {
   compile fileTree(include: ['*.jar'], dir: 'libs')
   testCompile 'junit:junit:4.12'
   compile 'com.android.support:appcompat-v7:23.1.1'
   compile ('com.ourcompany:sdk:2.1.16@aar') {
      transitive = true
   }
   compile 'com.google.android.gms:play-services-location:+'
   compile 'com.google.android.gms:play-services-maps:+'
}

我们检查过 proguard.txt 存在于我们的 AAR 工件中。 我们已经在两个环境中测试了此集成:

Android Studio 1.5.1 以以下结果结束构建过程: 首次构建:

    :app:proguardDebug
Warning:retrofit.RxSupport: can't find superclass or interface rx.Observable$OnSubscribe
Warning:retrofit.client.ApacheClient$GenericEntityHttpRequest: can't find superclass or interface org.apache.http.client.methods.HttpEntityEnclosingRequestBase
Warning:retrofit.client.ApacheClient$GenericHttpRequest: can't find superclass or interface org.apache.http.client.methods.HttpRequestBase
Warning:retrofit.client.ApacheClient$TypedOutputEntity: can't find superclass or interface org.apache.http.entity.AbstractHttpEntity
Warning:okio.DeflaterSink: can't find referenced class org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
Warning:okio.Okio: can't find referenced class java.nio.file.Files
Warning:okio.Okio: can't find referenced class java.nio.file.Path
Warning:okio.Okio: can't find referenced class java.nio.file.OpenOption
Warning:okio.Okio: can't find referenced class java.nio.file.Path
Warning:okio.Okio: can't find referenced class java.nio.file.OpenOption
Warning:okio.Okio: can't find referenced class org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
Warning:okio.Okio: can't find referenced class java.nio.file.Path
Warning:okio.Okio: can't find referenced class java.nio.file.OpenOption
Warning:okio.Okio: can't find referenced class java.nio.file.Path
Warning:okio.Okio: can't find referenced class java.nio.file.OpenOption
Warning:okio.Okio: can't find referenced class org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
Warning:retrofit.RestMethodInfo$RxSupport: can't find referenced class rx.Observable
Warning:retrofit.RxSupport: can't find referenced class rx.Observable
Warning:retrofit.RxSupport: can't find referenced class rx.Observable$OnSubscribe
Warning:retrofit.RxSupport: can't find referenced class rx.Observable
Warning:retrofit.RxSupport: can't find referenced class rx.Subscriber
Warning:retrofit.RxSupport: can't find referenced class rx.subscriptions.Subscriptions
Warning:retrofit.RxSupport: can't find referenced class rx.Subscriber
Warning:retrofit.RxSupport: can't find referenced class rx.Observable$OnSubscribe
Warning:retrofit.RxSupport: can't find referenced class rx.subscriptions.Subscriptions
Warning:retrofit.RxSupport: can't find referenced class rx.Observable
Warning:retrofit.RxSupport: can't find referenced class rx.Subscriber
Warning:retrofit.RxSupport: can't find referenced class rx.Observable$OnSubscribe
Warning:retrofit.RxSupport: can't find referenced class rx.Subscriber
Warning:retrofit.android.AndroidApacheClient: can't find referenced class android.net.http.AndroidHttpClient
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPMethod
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.URLFetchServiceFactory
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.URLFetchService
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPRequest
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPHeader
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPRequest
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPResponse
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPRequest
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPResponse
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPHeader
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPResponse
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPMethod
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPRequest
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPResponse
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPHeader
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPMethod
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.URLFetchServiceFactory
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.URLFetchService
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPResponse
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.URLFetchService
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPMethod
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.URLFetchService
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPRequest
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPResponse
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.URLFetchService
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPRequest
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPResponse
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.URLFetchService
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPRequest
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPMethod
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPRequest
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPResponse
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPRequest
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPHeader
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPResponse
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPRequest
Warning:retrofit.appengine.UrlFetchClient: can't find referenced class com.google.appengine.api.urlfetch.HTTPHeader
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.params.BasicHttpParams
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.impl.client.DefaultHttpClient
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.client.HttpClient
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.HttpResponse
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.StatusLine
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.HttpResponse
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.Header
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.HttpResponse
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.util.EntityUtils
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.HttpResponse
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.StatusLine
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.Header
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.HttpEntity
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.client.HttpClient
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.HttpResponse
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.StatusLine
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.Header
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.util.EntityUtils
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.client.HttpClient
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.client.methods.HttpUriRequest
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.HttpResponse
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.client.HttpClient
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.client.methods.HttpUriRequest
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.HttpResponse
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.client.HttpClient
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.client.methods.HttpUriRequest
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.HttpResponse
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.Header
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.HttpResponse
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.StatusLine
Warning:retrofit.client.ApacheClient: can't find referenced class org.apache.http.HttpEntity
Warning:retrofit.client.ApacheClient$GenericEntityHttpRequest: can't find referenced class org.apache.http.client.methods.HttpEntityEnclosingRequestBase
Warning:retrofit.client.ApacheClient$GenericEntityHttpRequest: can't find referenced method 'void setURI(java.net.URI)' in program class retrofit.client.ApacheClient$GenericEntityHttpRequest
Warning:retrofit.client.ApacheClient$GenericEntityHttpRequest: can't find referenced class org.apache.http.message.BasicHeader
Warning:retrofit.client.ApacheClient$GenericEntityHttpRequest: can't find referenced method 'void addHeader(org.apache.http.Header)' in program class retrofit.client.ApacheClient$GenericEntityHttpRequest
Warning:retrofit.client.ApacheClient$GenericEntityHttpRequest: can't find referenced method 'void setEntity(org.apache.http.HttpEntity)' in program class retrofit.client.ApacheClient$GenericEntityHttpRequest
Warning:retrofit.client.ApacheClient$GenericEntityHttpRequest: can't find referenced class org.apache.http.client.methods.HttpEntityEnclosingRequestBase
Warning:retrofit.client.ApacheClient$GenericHttpRequest: can't find referenced class org.apache.http.client.methods.HttpRequestBase
Warning:retrofit.client.ApacheClient$GenericHttpRequest: can't find referenced method 'void setURI(java.net.URI)' in program class retrofit.client.ApacheClient$GenericHttpRequest
Warning:retrofit.client.ApacheClient$GenericHttpRequest: can't find referenced class org.apache.http.message.BasicHeader
Warning:retrofit.client.ApacheClient$GenericHttpRequest: can't find referenced method 'void addHeader(org.apache.http.Header)' in program class retrofit.client.ApacheClient$GenericHttpRequest
Warning:retrofit.client.ApacheClient$GenericHttpRequest: can't find referenced class org.apache.http.client.methods.HttpRequestBase
Warning:retrofit.client.ApacheClient$TypedOutputEntity: can't find referenced class org.apache.http.entity.AbstractHttpEntity
Warning:retrofit.client.ApacheClient$TypedOutputEntity: can't find referenced method 'void setContentType(java.lang.String)' in program class retrofit.client.ApacheClient$TypedOutputEntity
Warning:retrofit.client.ApacheClient$TypedOutputEntity: can't find referenced class org.apache.http.entity.AbstractHttpEntity
Warning:there were 148 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)
Warning:there were 6 unresolved references to program class members.
         Your input classes appear to be inconsistent.
         You may need to recompile the code.
         (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedprogramclassmember)
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.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)
:app:dexDebug
:app:validateDebugSigning
:app:packageDebug FAILED
Error:Execution failed for task ':app:packageDebug'.
> Unable to compute hash of D:\Workspace\AppAndroid\TestApp\app\build\intermediates\classes-proguard\debug\classes.jar

第二次构建,刚刚:

:app:packageDebug FAILED
Error:Execution failed for task ':app:packageDebug'.
> Unable to compute hash of D:\Workspace\AppAndroid\TestApp\app\build\intermediates\classes-proguard\debug\classes.jar

Android Studio 1.4.2

构建成功完成,但在运行时我们收到由代码混淆引起的 NullPointerException。

我们还尝试通过 gradlew :app:clean :app:assembleDebug 构建项目 最后得到

':app:proguardDebug', task ':app:dexDebug', task ':app:validateDebugSigning', task ':app:packageDebug', task ':app:zipalignDebug', task ':app:assembleDebug']
    22:15:56.077 [INFO] [org.gradle.execution.taskgraph.AbstractTaskPlanExecutor] :app:clean (Thread[main,5,main]) started.
    22:15:56.077 [LIFECYCLE] [class org.gradle.TaskExecutionLogger] :app:clean
    22:15:56.097 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter] Starting to execute task ':app:clean'
    22:15:56.097 [DEBUG] [org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter] Determining if task ':app:clean' is up-to-date
    22:15:56.098 [INFO] [org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter] Executing task ':app:clean' (up-to-date check took 0.001 secs) due to:
      Task has not declared any outputs.
    22:15:56.098 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter] Executing actions for task ':app:clean'.
    22:15:56.099 [DEBUG] [org.gradle.api.internal.file.copy.DeleteActionImpl] Deleting D:\Workspace\AppAndroid\TestApp\app\build
    22:15:56.933 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter] Finished executing task ':app:clean'
    22:15:56.934 [LIFECYCLE] [class org.gradle.TaskExecutionLogger] :app:clean FAILED
    22:15:56.957 [INFO] [org.gradle.execution.taskgraph.AbstractTaskPlanExecutor] :app:clean (Thread[main,5,main]) completed. Took 0.88 secs.
    22:15:56.957 [DEBUG] [org.gradle.execution.taskgraph.AbstractTaskPlanExecutor] Task worker [Thread[main,5,main]] finished, busy: 0.88 secs, idle: 0.001 secs
    22:15:56.977 [LIFECYCLE] [org.gradle.BuildResultLogger] 
    22:15:56.977 [LIFECYCLE] [org.gradle.BuildResultLogger] BUILD FAILED
    22:15:56.977 [LIFECYCLE] [org.gradle.BuildResultLogger] 
    22:15:56.977 [LIFECYCLE] [org.gradle.BuildResultLogger] Total time: 7.678 secs
    22:15:57.003 [DEBUG] [org.gradle.api.internal.tasks.compile.daemon.CompilerDaemonManager] Stopping 0 compiler daemon(s).
    22:15:57.003 [INFO] [org.gradle.api.internal.tasks.compile.daemon.CompilerDaemonManager] Stopped 0 compiler daemon(s).
    22:15:57.005 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on buildscript class cache for settings file 'D:\Workspace\AppAndroid\TestApp\settings.gradle'

非常感谢您的帮助!提前致谢!

我们清除了所有缓存,从头开始重新创建项目,现在一切正常。