8

我是第一次测试 Proguard 的混淆功能。为此,我做了以下

  • 下载了 Cordova Hello World 插件
  • 通过将字符串添加" This is top secret"到它发出的问候消息来修改它
  • 创建了一个简单的 Cordova Hello World 项目cordova create ...
  • 添加了Cordova Proguard 插件
  • 将本地来源的插件添加到该项目中
  • platforms\android\project.properties通过追加修改文件proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-custom.txt
  • 构建了apkcordova build --release

proguard-custom.txt基本上没有动过文件。其内容如下图

-keepclassmembers class * {@android.webkit.JavascriptInterface <methods>;}
#AddedThis 
-adaptresourcefilenames    **.json,**.gif,**.jpg 
-adaptresourcefilecontents **.json,META-INF/MANIFEST.MF

-keep class com.android.vending.licensing.ILicensingService
-keepattributes *Annotation*

-keepclassmembers enum * {
  public static **[] values();
  public static ** valueOf(java.lang.String);
}

-keepattributes InnerClasses
-keep class **.R
-keep class **.R$* {<fields>;}

-renamesourcefileattribute SourceFile    
-keepattributes SourceFile,LineNumberTable

-keep class org.apache.cordova.** { *; }
-keep public class * extends org.apache.cordova.CordovaPlugin

-keep class org.apache.cordova.CordovaBridge 
{org.apache.cordova.PluginManager pluginManager; }
-keep class org.apache.cordova.CordovaInterfaceImpl 
{org.apache.cordova.PluginManager pluginManager; }
-keep class org.apache.cordova.CordovaResourceApi
{org.apache.cordova.PluginManager pluginManager; }
-keep class org.apache.cordova.CordovaWebViewImpl 
{org.apache.cordova.PluginManager pluginManager; }
-keep class org.apache.cordova.ResumeCallback 
{org.apache.cordova.PluginManager pluginManager; }
-keep class org.apache.cordova.engine.SystemWebViewEngine 
{org.apache.cordova.PluginManager pluginManager; }

-keep class com.google.gson.internal.UnsafeAllocator { ** theUnsafe; }

-dontnote org.apache.harmony.xnet.provider.jsse.NativeCrypto
-dontnote sun.misc.Unsafe

-keep class com.worklight.androidgap.push.** { *; }
-keep class com.worklight.wlclient.push.** { *; }

-keep class com.google.** { *; }
-dontwarn com.google.common.**
-dontwarn com.google.ads.**

-optimizations 

!class/merging/vertical*,!class/merging/horizontal*,
!code/simplification/arithmetic,!field/*,!code/allocation/variable

-keep class net.sqlcipher.** { *; }
-dontwarn net.sqlcipher.**

-keep class org.codehaus.** { *; }
-keepattributes *Annotation*,EnclosingMethod

-keepclassmembers enum * {
 public static **[] values();
 public static ** valueOf(java.lang.String);
}

-assumenosideeffects class android.util.Log {
  public static *** d(...);
}

-dontwarn com.worklight.common.internal.WLTrusteerInternal*
-dontwarn com.worklight.jsonstore.**
-dontwarn org.codehaus.jackson.map.ext.*
-dontwarn com.worklight.androidgap.push.GCMIntentService
-dontwarn com.worklight.androidgap.plugin.WLInitializationPlugin

-dontwarn android.support.v4.**
-dontwarn android.net.SSLCertificateSocketFactory
-dontwarn android.net.http.*

Proguard 按预期完成所有动作。例如,我看到

Obfuscating...
Printing mapping to 
[buildpath\outputs\mapping\release\mapping.txt]...
Preverifying...
Writing output...
Preparing output jar 
[buildpath\intermediates\transforms\proguard\release\0.jar]
Copying resources from program jar 
[buildpath\intermediates\transforms\desugar\release\0.jar] 
(filtered)
Copying resources from program directory 
[buildpath\intermediates\transforms\desugar\release\1] 
(filtered)
Copying resources from program jar 
[buildpath\intermediates\transforms\mergeJavaRes\release\0.jar] 
(filtered)

并且 APK 按预期生成。我通过这个Java 反编译器运行了这个 APK并下载了输出。浏览到Hello.java那个下载我找到了完整的原始源代码

package com.example.plugin;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;

public class Hello extends CordovaPlugin 
{
 public boolean execute(String str,JSONArray jSONArray, 
 CallbackContext callbackContext) 
 {
  if (!str.equals("greet")) return false;
  callbackContext.success("Hello, " + jSONArray.getString(0) + 
  " This is a secret!");
  return true;
 }
}

就好像 Proguard 经历了所有的动作,但什么都没有。我显然错过了这里的一个关键步骤,但我不清楚那可能是什么。我将非常感谢任何能够让我走上正轨的人。

4

1 回答 1

0

文件中的这些行proguard-custom.txt阻止 proguard 混淆该文件:

-keep class org.apache.cordova.** { *; }
-keep public class * extends org.apache.cordova.CordovaPlugin

如果您删除它们,您应该会看到文件变得模糊。但是,这可能会破坏您的应用程序,因为 Cordova 可能包含它们是有原因的(我对 Cordova 不是很熟悉)。

于 2019-06-14T22:21:54.680 回答