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.
Related
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.
I am fetching data from server it is working fine in debug apk but I am trying on generate signed apk, data is not fetching data from server.
Is there any way to get solution?
Proguard may be causing that issue. Please check if it is enabled in your app's gradle file.
These lines enables proguard for release build:
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
If yes then you will need to keep some fields.
See this: https://developer.android.com/studio/build/shrink-code
You need to add #Keep annotation for all your network models (if you or your libs uses reflection). Because proguard obfuscate all classes in signed apk and Gson can`t parse JSON to you model.
EDIT:
for example you have class for parse from json (NetworkResponse.java class):
#Keep
public class NetworkResponse {
// fields of class...
}
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.
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.
I'm developing Android library and I want to hide/obfuscate the source code implementation of the library.
The way the user project app will use the library is:
startActivity( new Intent(context, LibraryActivityName.class) );
So I need to keep just the name of entry point Activity inside the library project, That's all.
When I used the default ProGuard settings:
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
as well as the suggested example for library - Nothing happened, and by clicking on the Activity name inside the user app (when he imports it) - One can see the source code.
Thanks,
As you do not have a typical library, you should not include the typical library example.
First of all, you need to enable Proguard execution, change this line:
minifyEnabled true
Second, you do not want to keep all public classes, but only the activity:
-keep class LibraryActivityName { public protected <methods>; }
The remaining classes can be fully obfuscated if I understand your question correctly, so there should be no need for further configuration, unless you use reflection somewhere.
It would also be good if you repackage the obfuscated classes into an internal package or something using
-repackageclasses my.library.package.internal
which might also required
-allowaccessmodification
btw. ProGuard will not obfuscate the code itself, only the class / method names.