如何在 cordova Android应用程序中使用proguard

uoifb46i  于 2022-11-15  发布在  Android
关注(0)|答案(3)|浏览(162)

我有一个完成的 cordova 项目,我想使用proguard,以防止其他人从逆向工程的APK。但我有麻烦这样做。
http://developer.android.com/tools/help/proguard.html
它说“当你创建一个Android项目时,会在项目的根目录下自动生成一个proguard.cfg文件。“但是我找不到这样的文件。那么我该如何将proguard应用到我的应用中呢?
谢谢你

jq6vz3qz

jq6vz3qz1#

您需要从www.example.com文件中取消对以下行的注解project.properties
要使ProGuard能够收缩和模糊处理代码,请取消注解以下内容(可用属性:sdk目录,用户主页):

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

此外,如果您将Webview与JS一起使用,请取消注解proguard-project.txt中得以下行,并为JavaScript接口类指定完全限定得类名:

-keepclassmembers class com.your.package.YourJSInterfaceClass {
   public *;
}
b1zrtrql

b1zrtrql2#

我和你分享我的 cordova proguard。这是一个hello world应用程序:

# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:
#-dontshrink
#-dontoptimize
#-dontobfuscate

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class android.webkit.WebView {
#   public *;
#}

#-injars      bin/classes
#-injars      libs
#-outjars     bin/classes-processed.jar

# Specifies to write out some more information during processing.
# If the program terminates with an exception, this option will print out the entire stack trace, instead of just the exception message.
-verbose

# Annotations are represented by attributes that have no direct effect on the execution of the code.
-keepattributes *Annotation*

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

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

# These options let obfuscated applications or libraries produce stack traces that can still be deciphered later on
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable

# Enable proguard with Cordova
-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.worklight.androidgap.push.** { *; }
-keep class com.worklight.wlclient.push.** { *; }

# Enable proguard with Google libs
-keep class com.google.** { *; }
-dontwarn com.google.common.**
-dontwarn com.google.ads.**
-dontwarn com.google.android.gms.**

# apache.http
-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);
}

# Remove debug logs in release build
-assumenosideeffects class android.util.Log {
    public static *** d(...);
}

# These classes contain references to external jars which are not included in the default MobileFirst project.
-dontwarn com.worklight.common.internal.WLTrusteerInternal*
-dontwarn com.worklight.jsonstore.**
-dontwarn org.codehaus.jackson.map.ext.*

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

# These clases contain references to cordova webView
-keep class com.ionicframework.cordova.webview.** {*; }
-keep class com.ionicframework.cordova.webview.*
5ssjco0h

5ssjco0h3#

出于某种原因,在React Native应用程序中,我可以看到它被自动创建,但在Cordova应用程序中却没有。通过下面的操作,我能够弄清楚要添加它必须做什么:
1.创建文件MY_PROJECT/platforms/android/app/proguard-rules.pro
1.将有关ProGuard的任何代码添加到文件MY_PROJECT/platforms/android/app/proguard-rules.pro
1.在MY_PROJECT/platforms/android/app/build.gradle(注意:在MY_PROJECT/platforms/build.gradle中有另一个build.gradle-不要**混淆),它应该在您在buildTypes-〉release中创建MY_PROJECT/platforms/android/app/proguard-rules.proproguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"的同一个文件夹中。

相关问题