How do I get optimisation in Android without obfuscation using R8 - android

I have some big chunks of code which test the behaviour of some incompletely documented Android APis (sigh) which seem to behave differently for different Android versions and I want to switch them in and out of debug builds (they are always removed from release builds) which I try to do using buildConfigFields in build.gradle like this
buildTypes {
release {
minifyEnabled true
buildConfigField("boolean", "RINGERMODETEST", "false")
...
}
debug {
// set this to false to remove the optional code
buildConfigField("boolean", "RINGERMODETEST", "true")
...
}
}
Then in my code I have
if (BuildConfig.RINGERMODETEST) {
// optional code
}
To get the optional code removed I need to turn on the optimiser, but setting minifyEnabled would turn on obfuscation as well which I don't want in a debug build. There is an earlier answer to this question for Proguard at Proguard shrinking and optimizing without obfuscation, but there doesn't seem to be any documentation which says how to do it for R8. I really don't want to have to learn how to construct a complete proguard control file when it's supposed to be superseded by R8. and useProguard will apparently soon be deprecated.

Related

Undo from enable minify after release the app

At some point of devlopment I changed minifyEnabled to true without any rules in proguard then release the app to google play with v.1.4.0.
many of bugs occured to uses when updated the app, I knew the problem because the obfuscated of classes.
and some of users removed the app and reinstall to work well partially.
WebView, Camera, Gson, File Picker all this features have problems on version 1.4.0
It was my first experience with minifyEnabled, now I'm knowing that there are alot of rules should I write in the proguard to keep classes.
My question about make undo of minifyEnabled and set it to false, when I debug it, also a new problem occurs and one of them from the code below with NullPointerException.
abstract class LiveCoroutinesViewModel : ViewModel() {
inline fun <T> launchOnViewModelScope(crossinline block: suspend () -> LiveData<T>): LiveData<T> {
return liveData(viewModelScope.coroutineContext + Dispatchers.IO) {
emitSource(block())
}
}
}
note: I don't need to keep minifyEnabled = true, becuase I have alittle bit experinces and I think there are alot of keeping rules should I understand before I write them and I don't have the time now for that.
So, what is the optimal soultion (strategy) to do minifyEnabled = false for users which already working on minifyEnabled = true
The answer is not possible in my way. Because you don't have an option to handle your released app code.
My Suggestion:
Do you think that you have to write rules for Proguard? But not, Android proguard is R8 Guard not proguard. Proguard is another company guard(DexGuard). You can use the R8 guard without adding any rules because these rules are already included in all libraries(read library github and check R8 is included or not). If you enabled R8 guard then just add this line #Keep in your Model class to prevent R8 Guard to shrink that file. Add #Keep which file you don't want to minify.
Like this
#Keep // use to prevent R8 to minify this class.
public class ModelClass {
String id;
String text;
String image;
public String getImage() {
return image;
}
In new version of android, You can test your app by enabling R8 guard in debug mode by adding these below lines in your build.gradle(:app); // in module level
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug { // add this line after release and make ninify true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
It may take some time to build but it is a very good method to test if the app is caching or not (in app build time).
PS
minifyEnabled true helps to reduce app size. Very helpful if you enabled it.
It helps you in the future.

Google Maps location service not working in release build using proguard

I am facing a strange issue that Google maps location service(Place API) is not working when I build application in release mode while it works perfectly in debug builds.
I am guessing that applying proguard rules may have created this issue tried changing proguard rules but still the issue.
My build file is like:
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
}
debug {
ext.alwaysUpdateBuildId = false
}
}
My proguard rules are: Proguard Rule
I have checked the log and found that LatLong are coming fine in release build too but after that Maps API are not responding (could not found any thrown exception) but something like
I/GeoApiContext: Request: {0}
AsyncTask for getting place detail using LatLong: REtrieveAddressAsyncTask
[EDIT]
I confirmed that API_KEY is not thee issue here because when I built my release APK removing Proguard rule and disabling MinifyEnabled, Geolocation api started working, so I guess something I am doing wrong in my Proguard rules and couldn't find that.

Android Release build can be attached

I recently find my Android Release version can be attached through Android Studio and all logs are available to be seen as well, even though I'm sure that AndroidManifest.xml file doesn't contain "android:debuggable=true" and app's build.gradle file specified that
buildTypes {
...
release {
...
debuggable false
...
}
...
}
Do you guys have any good idea to avoid this?
You can only keep like this code below:
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
hopefully works it.
Logs are nothing to do with debuggable true.
The option debuggale is to making your application debuggable in release mode, so you can attach debugger even on your release build by default it is false.
If you are using Log class and printing any log it will always display until and unless you put the check before logging them.
What you can do is put a check before every log is Build.Debug == true then print the log.
Or you can use open source library for this work like this one which provide the control of logging based on your configuration.
Or you can find a more helpful answer here.

Is BuildConfig.DEBUG still bugged?

According to this Blog BuildConfig.DEBUG was unreliable.
Since my colleague is using BuildConfig.DEBUG extensively (seemingly like test code in production code), I'm wondering if this flag is still as bugged as it was a few years ago.
I can confirm this bug still exists, tested with Android Studio 1.2 Build AI-140.1782451 and Gradle 1.1 compiling against Android API Level 21.
The issue is visible with a Nexus 10 on Android 5.0.2 or a similar device.
If you open BuildConfig.DEBUG in the source editor it says:
public static final boolean DEBUG = Boolean.parseBoolean("true");
But if you debug the app under question, DEBUG stays on false.
This hinders my Retrofit-debugging, as I wanted to enable it conditionally depending on the build-type.
It seems the issue to which you are referring is specific to ADT + Eclipse. So I believe that if you're using Gradle and Android Studio, this should not be an issue.
Crucially: this only occurs if you're using the Build Automatically option, and you don't clean your project. Because of this, I would hardly consider this a bug. After all, who says what should and shouldn't be rebuilt whenever you make a code change and have Build Automatically enabled?
As a matter of good practice, you should always clean and rebuild your project prior to an actual release, in which case this is a non-issue.
So yes, this is still a problem if you're using this setting, and not rebuilding your project prior to release, and you're still using ADT and Eclipse (which seems to be destined for deprecation).
Here's the discussion of the bug: https://code.google.com/p/android/issues/detail?id=27940
I had always problems with the predefined variable, so I created my own:
buildTypes {
// If we use the same keystore for debug and release, we don't have to wipe all preferences
debug {
//noinspection GroovyAssignabilityCheck
signingConfig signingConfigs.releaseConfig
zipAlignEnabled true
resValue "bool", "DEBUG", "true"
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//noinspection GroovyAssignabilityCheck
signingConfig signingConfigs.releaseConfig
zipAlignEnabled true
resValue "bool", "DEBUG", "false"
}
}
I your code you can read this variable:
if (!MyApplication.get().getResources().getBoolean(R.bool.DEBUG)) {
// Firebase Crash reporting
FirebaseCrash.report(e);
}

Enable LogCat on Release Build in Android Studio

By default, when I change Build Variants to release I don't get any Logs on the logcat, but I do need to read release logs of my app, how can I enable this?
Add android:debuggable="true" (default is false) to your Manifest inside the <application> tag.
From the docs:
android:debuggable
Whether or not the application can be debugged,
even when running on a device in user mode — "true" if it can be, and
"false" if not.
respectively
You can disable debugging by removing the android:debuggable attribute
from the tag in your manifest file, or by setting the
android:debuggable attribute to false in your manifest file.
Edit
You may need to add the following to your build.gradle file inside the android{...} tag:
lintOptions {
checkReleaseBuilds false
}
And as a side-note: Right on the device the Logs are always written, no matter if your application's debuggable is set to false or true. But via the LogCat in Android Studio it's only possible if debuggable is set to true. (Just tested this)
You should add
android {
buildTypes {
release {
debuggable true
In this case you can use Log. or System.out.println and see logs.
If you cannot run release version (app is disabled), and error is shown: "apk is not signed. Please configure the signing information for the selected flavor using the Project Structure dialog", see app-release-unsigned.apk is not signed.
I do not like the other solution because then you are not testing how the App really is deployed.
A better solution is to open the Android Device Monitor where you can see the logs even when in release configuration with debuggable=false.
Find it here:
Tools -> Android -> Android Device Monitor
Update:
Android Device Monitor was removed in Android Studio 3.2. However, it is still present in SDK, and you can use it to see the logs (it is located in $ANDROID_SDK/tools/)
debuggable true in build.gradle works well, except that BuildConfig.DEBUG is also going to be true. This might be a problem if your app relies on BuildConfig.DEBUG to do something only when it's a debug build.
In such a case, try Log.wtf(BuildConfig.APPLICATION_ID, "something went wrong"), which will print to logcat even if it's a release build.
This approach will obviously help you to get logs while testing the production build. But be careful while uploading your app to Google Play Store, Toggle debuggable to false before uploading to production.
buildTypes {
debug {
manifestPlaceholders = [crashlyticsCollectionEnabled: "false"]
}
release {
manifestPlaceholders = [crashlyticsCollectionEnabled: "false"]
lintOptions {
checkReleaseBuilds false
abortOnError false
}
shrinkResources true
minifyEnabled true
debuggable true
signingConfig signingConfigs.productionrelease
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
set crashlyticsCollectionEnabled to false to avoid your crashes to report to Google Play-Store while debugging.

Categories

Resources