I have an android app using Mapstruct 1.2.0.Final and I am trying to use ProGuard for my release build.
Caused By: java.lang.RuntimeException java.lang.ClassNotFoundException: Cannot find implementation for my.company.app.e.b.k
The error is a bit obfuscated due to proguard, but it seems pretty clear what the issue is.
There is no clear documentation on what rules to use in the proguard file, and this error only happens in the release build, which immediately points me to a proguard issue.
@lordplagus02 if ProGuard obfuscates your code (I assume) and you use then the MapStruct Mappers cannot find the needed classes (I suppose it fails there).
The the Mappers factory does is search for a class suffixed with Impl on the name of the Mapper. I think that if you manage to not obfuscate the mappers classes at least it should work.
Maybe you need to use some DI (not sure how that fits with Android). I know that you can use Dagger. However, that is only available on master at the moment. Have a look at #571
It's going to be fun trying to figure out how to write some proguard rules for the Impl classes. Any thoughts on what they might look like?
There should be some sort of documentation on this, given that there are millions of apps using proguard, and probably many of those use mapstruct. I don't want to delve into dependency injection on android just yet. If I don't use proguard though, I have to use multidex in my release build, and that's not optimal.
@lordplagus02 If you have not changed Mapper#implementationName then the implementation name is the name of the mapper class / interface plus Impl in the same package.
So if you have:
package com.example;
@Mapper
public interface MyMapper {
}
The implementation will be:
package com.example;
public class MyMapperImpl implements MyMapper {
}
I have no idea how proguard rules are written. In any case feel free to ask questions in case you need some help from us.
Keep package with mappers working for me:
-keep public class com.example.path.to.your.mappers.**
Thanks for the info @Anrimian. I have also opened https://github.com/mapstruct/mapstruct.org/issues/71 so we add this info to our website as well.
Most helpful comment
Keep package with mappers working for me:
-keep public class com.example.path.to.your.mappers.**