Android: minification obfuscating dynamically accessed classes - android

I am using Samsung's Motion library to create a pedometer for Samsung phones. When I create an APK without minification in the gradle configuration file the system works. However when I try to apply minification before releasing to the store,
buildTypes {
release {
minifyEnabled true
signingConfig signingConfigs.config
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
debuggable false
jniDebuggable false
}
I got errors claiming the some libraries (e.g. android.hardware.motion) do not exist.
I have checked and:
android.hardware.motion is not a standard library (probably existing only in Samsung phones?) in android.hardware
these classes are accesses dynamically (maybe because non standard?), e.g. Class.forName("android.hardware.scontext.SContextManager");
If I remove the line
minifyEnabled true
I got no error, so it must be the code obfuscation.
I have tried to add to my proguard rules file statements such as
-keep class android.hardware.** { *; }
-keep class com.samsung.android.sdk.** { *; }
but this does not seem to work.
Any idea? Thanks!

The missing classes are indeed only available on the actual device.
In order to let ProGuard process your application, you will have to include the following configuration:
-dontwarn com.samsung.android.sdk.motion.**
-dontnote com.samsung.android.sdk.motion.**
This will ignore the warnings and notes originating from the samsung sdk.

Related

How to make Amazon In-App Purchasing (IAP) 2.0 work with Android R8

It took a while after significant revenue loss to find out that Android R8 code shrinker prevents Amazon In-App Purchasing (IAP) 2.0 from working. Android Studio uses R8 by default now. Fortunately, the following in gradle.properties seems to be able to stop R8:
android.enableR8=false
I fear R8 will be mandated in the future, and it will be a disaster. I am trying to find a way to make them compatible.
Someone posted a similar question on Amazon's forum that is supposed to be the best place for such questions, but I have a feeling that Amazon has stopped supporting the forum because my recent two questions there have received no response from Amazon.
I have the following in proguard-project.txt that is used by R8
#amazon
-dontwarn com.amazon.**
-keep class com.amazon.** {*;}
-keepattributes *Annotation*
-optimizations !code/allocation/variable
-optimizationpasses 1
As my understanding, R8 ignores optimizationpasses. I suspect that is the cause.
Edit(2019-05-30)
The aforementioned script for proguard-project.txt is from Amazon's document for In-App Purchasing
The app's build.gradle has the following:
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
signingConfig signingConfigs.Release
}
proguard-android.txt uses -dontoptimize, so it shouldn't be optimizing at all with ProGuard or R8, regardless of the number of -optimizationpasses specified. If you are interested, you can see the available default ProGuard files under build/intermediates/proguard-files/. proguard-android-optimize.txt is the one that would allow optimization.
At this point, given that R8's optimization doesn't seem to be on, I'm not sure if there's much of a workaround. I'd recommend that you file an issue with Google's Issue Tracker, giving as much detail as you can to help them reproduce the issue. They are generally pretty responsive to issues like this.
Should work now with R8, add this to your main build.gradle dependencies:
buildscript {
repositories {
maven {
url 'http://storage.googleapis.com/r8-releases/raw'
}
}
dependencies {
classpath 'com.android.tools:r8:1.6.42' //Must be before the Gradle Plugin for Android. - Or any other version
classpath 'com.android.tools.build:gradle:...'
}
}
build.gradle
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
}
proguard-rules.pro
-dontwarn com.amazon.**
-keep class com.amazon.** {*;}
-keepattributes *Annotation*
-dontoptimize
gradle.properties
# Disables R8 for Android Library modules only.
android.enableR8.libraries = false
# Disables R8 for all modules.
android.enableR8 = false
This works for me.

Android Proguard have some trouble with AVG Antivirus

I made an android app that always worked fine. Suddenly, the AVG Antivirus reported that my app is a malware. After a couple hours, I found the issue:
In the build.gradle, if I use the config below to generate signed APK, AVG reports (the APK) as malware:
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
But if I comment the last line, no threat is found:
buildTypes {
release {
shrinkResources true
minifyEnabled true
//proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
My proguard-rules.pro has nothing special:
-keepattributes Signature
-keepattributes *Annotation*
-keepattributes EnclosingMethod
-keep class io.codetail.animation.arcanimator.** { *; }
-keep class com.example.viewholders.** {
*;
}
-keep class com.android.vending.billing.**
-keep class cn.pedant.SweetAlert.Rotate3dAnimation {
public <init>(...);
}
-keepclassmembers class com.example.models.** {
*;
}
I'm using Android Studio 2.2.2 and my project uses Firebase. The AVG Antivirus version that I have in my android device is 5.9.0.1.224656.
The "malware" identified by AVG is Android/gp oi bccfdd.
Is there something that I can do to solve this problem?
Finally I got a solution. Just to change this line:
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
With this:
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
According to Android Studio User Guide:
"Tip: For even more code shrinking, try the proguard-android-optimize.txt file that's in the same location. It includes the same ProGuard rules, but with other optimizations that perform analysis at the bytecode level—inside and across methods—to reduce your APK size further and help it run faster."
Unfortunately, as I realized, this can cause false detections of malware by antivirus.

Differences between debuggable and not debuggable builds in android studio?

This question is related to a problem with my firebase, having the "debuggable true" option, which impact has in my program apart of enable testing?
When I configure another build or just the same but with "debuggable=false" the firebase can't be serialized. Why is this happening? Thanks
I though the proguard could be the solution but I'm using this build and changing debuggable to false is the problem.
Update
With proguard enabled or not, the result is the same with these permissions (I dont want any change in my code):
-dontwarn com.firebase.**
-dontoptimize
-keep class acr.acr_app.** { *; }
-keep class com.firebase.** { *; }
Gradle.Build
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
debuggable true
signingConfig signingConfigs.release
}
Udpate 2
I updated the libraries with no effect, somebody can help me a bit? How run app compile the app?

Using proguard with espresso/androidTest

I'm trying to configure proguard to use it with my espresso UI test flavor. The thing is Proguard tends to ignore my debug proguard config.
This is how the config looks:
buildTypes {
debug {
minifyEnabled true
proguardFiles 'proguard-debug.pro'
testProguardFile 'proguard-debug.pro'
signingConfig signingConfigs.release
}
}
I added testProguardFile but it doesn't seem to work on androidTest. I'm running mockDebug flavor variant. When I just run the app it works fine, but when I try to run test which is located in adnroidTest it won't run due to proguard warnings, like the proguard file wasn't processed at all and the file is pretty straight forward:
proguard-debug.pro
-dontobfuscate
-dontoptimize
-dontwarn
Before someone starts to advise me turning off proguard for debug builds: I need to have it enabled because of multidex.
If you want your test build as close as possible from the real deal, try this one:
# build.gradle
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
testProguardFile 'proguard-test.pro'
}
and
# proguard-test.pro:
-include proguard-rules.pro
-keepattributes SourceFile,LineNumberTable
On other hand, if you need it only because multidex, if your are using minSdkVersion < 21, ProGuard is tied to multidex flag and run automatically.
You also need to add the default proguard rules:
proguardFiles getDefaultProguardFile('proguard-android.txt')
This line can be removed, as it is a duplicate:
testProguardFile 'proguard-debug.pro'

How to use -dontwarn in ProGuard?

I am using android studio to build debug and release application.
When i build debug/release application
./gradlew assembleDebug
./gradlew assembleRelease
both build are created perfectly and run as well. Shows appropriate dialog box for debug or release
now i have added proguard details in build.gradle:
signingConfigs {
myConfig {
storeFile file("keystore.jks")
storePassword "abc123!"
keyAlias "androidreleasekey"
keyPassword "pqrs123!"
}
}
buildTypes {
release {
runProguard true
proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
signingConfig signingConfigs.myConfig
}
}
productFlavors {
defaultFlavor {
proguardFile 'proguard-rules.txt'
}
}
Now it shows error in event log as
Warning: there were 7 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 2 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)
:Flash Sales:proguardDefaultFlavorRelease FAILED
If i turn the runProguard option to false then its running.
I have these questions:
1) is it ok to release apk with runProguard = false?
2) How to use dontwarn while creating release build?
When I add new lib to project Generally this How I need to define for Progaurd.
Let's say I am using Twitter4J lib then I add dontwarn this way.
-keep class twitter4j.** { *; }
-dontwarn twitter4j.**
Put the -dontwarn lines in the proguard-rules.pro or proguard-rules.txt file.
Had to google this elsewhere since none of the answers included this info. Check the other answers for the specific lines you need for your case.

Categories

Resources