Phonegap functionality not working after eclipse apk export - proguard issue? - android

When I test my app on the device using Run as --> Android Application, Phonegap functionality works fine.
When I export a .apk file using Eclipse and run it on the device, Phonegap functionality is not working. I assume it's likely a Proguard issue. How do I solve this?
proguard-properties.txt
-keep public class * extends com.phonegap.api.Plugin
-keep public class * extends org.apache.cordova.api.Plugin
-keep public class org.apache.cordova.DroidGap
-keep public class org.apache.cordova.**
-libraryjars /path/to/adt-bundle-mac/sdk/tools/lib/commons-codec-1.4.jar
-dontwarn android.webkit.*

This worked:
-keep public class * extends com.phonegap.api.Plugin
-keep public class * extends org.apache.cordova.api.Plugin
-keep public class org.apache.cordova.DroidGap
-keep public class org.apache.cordova.**
-keep public class org.apache.**
-dontwarn android.webkit.*
-dontwarn org.apache.**
-keep public class * extends org.apache.cordova.api.CordovaPlugin
-keep class org.apache.cordova.** { *; }
(Phonegap 2.4 Android Proguard config)

I used :
-keep class org.apache.cordova.** { *; }
while proguarding my cordova application but it did not work. But when I wrote the same statement after '-keep' statements of all java files then it worked absolutely fine.(It might be because after keep of all cordova functions, keep of java functions overriding and excluding the cordova functions again).
But one of my project changing of order did not matter while in one it worked only after the order change i.e., by writing -keep class org.apache.cordova.** { *; } statement at the end in proguard config file.

Related

ProGuard with AppBundle and data-binding: IllegalStateException

I have a multi-module project (I use Android App Bundles) with data-binding, ViewModel. But when I enable Proguard the app crashes with the following error :
java.lang.RuntimeException: Unable to start activity ComponentInfo{...MyActivity}: java.lang.IllegalStateException: DataBindingUtil.setConte… R.layout.my_activity) must not be null
Caused by: java.lang.IllegalStateException: DataBindingUtil.setConte… R.layout.my_activity) must not be null
at MyActivity.onCreate(MyActivity.kt:38)
When I remove modules and create one app module, everything works. When I disable Proguard, it also works fine.
Here is part of my proguard-rules.pro:
-dontwarn android.databinding.**
-keep class android.databinding.** { *; }
-keep class com.example.module1.databinding.** { *; }
-keep class com.example.module2.databinding.** { *; }
In build.gradle:
minifyEnabled true
useProguard true
So the error points to this line of code:
val binding: com.example.module1.databinding.MyActivityBinding =
DataBindingUtil.setContentView(this, R.layout.my_activity)
Maybe any ideas how to fix?
Only need to add this to your proguard-rules.pro, where module1 and module2 are Dynamic Feature Modules with databinding enabled.
-keep class com.example.module1.DataBinderMapperImpl { *; }
-keep class com.example.module2.DataBinderMapperImpl { *; }
I don't really know why but for me, the following combination worked:
-dontwarn androidx.databinding.**
-keep class androidx.databinding.** { *; }
-keep class * extends androidx.databinding.DataBinderMapper
I tried using just -keep class * extends androidx.databinding.DataBinderMapper but it didn't work.
Also I tried using
-dontwarn androidx.databinding.**
-keep class androidx.databinding.** { *; }
but that didn't work either.
What worked was a combination of the three.
if you enable to view merged proguard file using
-printconfiguration proguard-merged-config.txt
You will see something like this is there to keep DataBinderMapperImp
# instant apps load these via reflection so we need to keep them.
-keep public class * extends android.databinding.DataBinderMapper
I think what is missung is androidx version
I added this line it working fine now
-keep class * extends androidx.databinding.DataBinderMapper { *; }
This worked for me
-keep class * extends androidx.databinding.DataBinderMapper { *; }

Where to add Proguard Custom Rules?

I am trying to add custom Proguard rules for my app, but I am not sure into which file I should add those custom rules. I know of three files
1)Android/sdk/tools/proguard/proguard-android.txt
2)Android/sdk/tools/proguard/proguard-project.txt
3)Project --> app --> proguard-rules.pro
First of all you will use the
3)Project --> app --> proguard-rules.pro
and
few generic custom rules which are safe to apply on app and wouldn't
hinder the functioning of it
depends on the code in your app but if you will set minifyEnabled to true(which is recommended) this could lead to a lot of issues which can be solved through proguard rules file.
This is my proguard file for one of my projects where I'm using Fabric, httpApacheClient, shareActionProvider and some other libraries that needs specific rules for proguard.
# This will ignore warnings for missing translations and some other wanrinings
-ignorewarnings
# Keeping line numbers for easy error tracking :
-keepattributes SourceFile,LineNumberTable
# Support ShareActionProvider will not work without this line :
-keep class android.support.v7.widget.ShareActionProvider { *; }
# Flurry Crashlytics
-keep class com.flurry.** { *; }
-dontwarn com.flurry.**
-keepattributes *Annotation*,EnclosingMethod
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int); }
# Preserve Flurry mediation classes for DFP/AdMob Ads ­keep public class com.google.ads.mediation.flurry.**
# Google Play Services library
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents(); }
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
public static final *** NULL; }
-keepnames #com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
#com.google.android.gms.common.annotation.KeepName *; }
-keepnames class * implements android.os.Parcelable { public static final ** CREATOR;
}
-keepattributes InnerClasses, EnclosingMethod
-keep class com.ironsource.mobilcore.**{ *; }
-dontwarn org.apache.http.**
-dontwarn android.net.http.AndroidHttpClient
-dontwarn android.app.Notification
-keep class com.dianxinos.DXStatService.stat.TokenManager {
public static java.lang.String getToken(android.content.Context);
}
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.app.Activity
-keep public class * extends android.content.ContentProvider
And almost you will find related proguard settings in the documentation of any external library you use.
in
3)Project --> app --> proguard-rules.pro
There are not generic proguard rules. All those generic ones already included with SDK, It depends on what features and libraries are you using in your project. Also there is no safeway of doing it. You have to apply rules and test app -> fix any issues then repeat same process.

Proguard doesnt obfuscate Class name, only methods are obfuscated

I try using Proguard in android studio, but seems like Proguard is not obfuscating the class name, for example, my app structure, and the config:
and config
but when i try trigger the exception in the app:
the exception is listed in ADB console:
only the methods are obfuscated, the MainActivity.class is not
This is an expected behaviour because the class is an activity!
All classes that are mentioned in AndroidManifest.xml have to keep their names (activities, services, providers, receivers, application, instrumentation). Otherwise the system won't be able to find them.
Gradle build automatically generates some rules for your ProGuard configuration to achieve this. It scans AndroidManifest.xml and adds rules for each class found there.
If you want to see all the rules that are used, add this line to your ProGuard rules:
-printconfiguration "build/outputs/mapping/configuration.txt"
It will create configuration.txt file containing all the rules.
There should be something like this:
# view AndroidManifest.xml #generated:50
-keep class com.github.browep.proguard.MainActivity {
<init>(...);
}
I was facing the same problems,
After updating my Android plugin for Gradle, Proguard stop obfuscating my utility and other class files.
After few searching, I found that Android studio gradle now uses newer version of Proguard.
And according to this stack-overflow answer, which stated that:
proguard automatically add rules specific for android/google package.
Therefore, After few rule changes in my app, Proguard obfuscated the class names again.
Old proguard-rules.pro:
#support-v4
##link https://stackoverflow.com/questions/18978706/obfuscate-android-support-v7-widget-gridlayout-issue
-dontwarn android.support.v4.**
-keep class android.support.v4.** { *; }
-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
#support-v7
-dontwarn android.support.v7.**
-keep class android.support.v7.** { *; }
#https://stackoverflow.com/a/34895791/4754141
-keep class !android.support.v7.view.menu.**
-keep interface android.support.v7.* { *; }
#support design
##link https://stackoverflow.com/a/31028536
-dontwarn android.support.design.**
-keep class android.support.design.** { *; }
-keep interface android.support.design.** { *; }
-keep public class android.support.design.R$* { *; }
#error : Note: the configuration refers to the unknown class 'com.google.vending.licensing.ILicensingService'
#solution : #link https://stackoverflow.com/a/14463528
-dontnote com.google.vending.licensing.ILicensingService
-dontnote **ILicensingService
#updating to Gradle 2.14.1 caused error : https://stackoverflow.com/q/17141832/4754141
-keepattributes EnclosingMethod
#render script
##link https://stackoverflow.com/questions/22161832/renderscript-support-library-crashes-on-x86-devices
-keepclasseswithmembernames class * { native <methods>; }
-keep class android.support.v8.renderscript.** { *; }
New proguard-rules.pro:
#https://stackoverflow.com/a/41901653/4754141
#https://stackoverflow.com/a/23840049/4754141
-keep class android.support.** { *; }
-keep interface android.support.** { *; }

ProGuard Reading library directory

I've been struggling with this one for a while now. I'm trying to Generate Signed APK in IntelliJ IDEA 13.1.3 and Run ProGuard 4.7 on the APK but I can't get rid of the following error:
Error: ProGuard: [project_name] Error: Can't read [C:\Program Files (x86)\Android\android-sdk\tools\proguard\libs] (No such file or directory)
The error message is preceded by this message:
Information: ProGuard: [project_name] Reading library directory [C:\Program Files (x86)\Android\android-sdk\tools\proguard\libs]
There is only a lib directory in that location containing proguard.jar and the two other jar files. I created an empty libs directory but got even more errors after that.
Otherwise the obfuscation seems to work properly, and without the obfuscation the signed APK is generated correctly.
The Android SDK Build-tools version is 20.
This is the proguard-project file:
-libraryjars libs
-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class * extends java.util.ListResourceBundle
{
protected Object[][] getContents();
}
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable
{
public static final *** NULL;
}
-keepnames #com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class *
{
#com.google.android.gms.common.annotation.KeepName *;
}
-keepnames class * implements android.os.Parcelable
{
public static final ** CREATOR;
}
UPDATE
It seems that this problem boils down to the line -libraryjars libs in the proguard-project file. I managed to properly Generate Signed APK with a simple test project when I removed that line from proguard-project.txt. However, when -libraryjars libs wasn't removed, the obfuscation failed.
If that line is stripped when trying to run ProGuard on the actual project, it gives even more errors.
Thanks for your answer, Eric.
Actually, I already solved the problem. Just hadn't updated the answer here until now.
Yes, -libraryjars in proguard-project.txt was the root of all evil. I just included it there because of some example configurations I came across.
Here's the properly working configuration:
-keep class * extends java.util.ListResourceBundle
{
protected Object[][] getContents();
}
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable
{
public static final *** NULL;
}
-keepnames #com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class *
{
#com.google.android.gms.common.annotation.KeepName *;
}
-keepnames class * implements android.os.Parcelable
{
public static final ** CREATOR;
}
-keep public class com.google.android.gms.ads.**
{
public *;
}
-keep public class com.google.ads.**
{
public *;
}
-keep class com.android.vending.billing.**
-dontwarn android.support.v4.**
-keep class android.support.v4.** { *; }
-dontwarn com.google.android.gms.**
You should have -libraryjars or -injars in your configuration. The Android build process (Ant, Eclipse, Gradle, and presumably IntelliJ IDEA as well) internally already specifies those options for you.
I haven't come across the Android SDK adding -libraryjars to proguard-project.txt (and specified paths are relative to the directory of the configuration file itself, not to .../tools/proguard), but maybe you should update to the latest version of the SDK to avoid the problem.

Running ProGuard having AQuery library

I'm trying to configure Proguard, but I can't manage to get it working.
This is the error:
I've tried things like:
-keep class com.android.auth.TwitterHandle.** { *; }
-keep class oauth.** { *; }
Without any luck.
Anyways, I don't really think ignoring is the answer. Because that might mean something is broken.
Any tips?
Thanks!
The warnings indicate that the AndroidQuery library depends on the OAuth library. Apparently, you're using the former library in your project, but the latter library is missing. You could add the missing library, but if your application is working fine without it in debug mode, you can just tell ProGuard to ignore the missing dependency. In this case:
-dontwarn com.androidquery.auth.**
or, to the same effect:
-dontwarn oauth.signpost.**
See the ProGuard manual > Troubleshooting > Warning: can't find referenced class
(I am the developer of ProGuard)
Add these lines to your proguard file.
-dontwarn oauth.**
-dontwarn com.android.auth.TwitterHandle.**
-keep class oauth.** { *; }
-keep class com.android.auth.TwitterHandle.** { *; }
Edit:
Anyways, I don't really think ignoring is the answer. Because that
might mean something is broken.
If you want to use Proguard and you are having some errors such as class not found, then you must disable/ignore their obfuscation. Because Proguard renames names, fields and methods of classes while obfuscating. This becomes a big problem if reflection is used for that classes. So you have to say proguard to ignore(not obfuscate) some classes to prevent this problem.
Please try this post https://stackoverflow.com/a/15477898/1665964
Insert to skip obfuscated in ProGuard your libs, jars, Classes and Subclasses with this example
-optimizationpasses 5
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-allowaccessmodification
-repackageclasses ''
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.MapActivity
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-libraryjars libs/commons-io-2.2.jar
-libraryjars libs/gson-2.2.2.jar
-keep public class org.apache.commons.io.**
-keep public class com.google.gson.**
-keep public class com.google.gson.** {public private protected *;}
##---------------Begin: proguard configuration for Gson ----------
-keepattributes Signature
-keepattributes *Annotation*
-keep class com.mypackage.ActivityMonitor.ClassMultiPoints.** { *; }
-keep public class com.mypackage.ActivityMonitor$ClassMultiPoints { public protected *; }
-keep public class com.mypackage.ActivityMonitor$ClassMultiPoints$ClassPoints { public protected *; }
-keep public class com.mypackage.ActivityMonitor$ClassMultiPoints$ClassPoints$ClassPoint { public protected *; }
##---------------End: proguard configuration for Gson ----------
The reason for error must be that you might be using an external jar file (in libs folder).
If this is the case, adding the following line before -keep class ... lines should solve your problem.
-libraryjars libs/<jar_filename>
If u are unfamiliar with proguard cmd tool then you can try proguard gui located at tools/proguard/lib/proguardgui of your android SDK folder.
open proguardgui and load your configuration file.
then in Input / output section add your input and output jar as well as add your android lib used in your project.
***** your main problem is that you havent added your library (Aquery.jar and others) jar that is used in your project , so that proguard can't find the required Classes.***

Categories

Resources