How to use protobuf with proguard - java

After adding protobuf dependency to a project that is using proguard for releases I run into the following issues:
Warning: ***.***.Api: can't find referenced method 'com.google.protobuf.Descriptors$FileDescriptor getDescriptor()' in library class com.google.protobuf.AnyProto
Warning: ***.***.Api$Information: can't find referenced method 'com.google.protobuf.Any$Builder toBuilder()' in library class com.google.protobuf.Any
Warning: ***.***.Api$Information: can't find referenced method 'com.google.protobuf.Any$Builder mergeFrom(com.google.protobuf.Any)' in library class com.google.protobuf.Any$Builder
Warning: ***.***.Api$Information: can't find referenced method 'com.google.protobuf.Any buildPartial()' in library class com.google.protobuf.Any$Builder
Warning: ***.***.Api$Information$Builder: can't find referenced method 'com.google.protobuf.Any build()' in library class com.google.protobuf.Any$Builder
Warning: ***.***.Api$Information$Builder: can't find referenced method 'com.google.protobuf.Any$Builder mergeFrom(com.google.protobuf.Any)' in library class com.google.protobuf.Any$Builder
Warning: ***.***.Api$Information$Builder: can't find referenced method 'com.google.protobuf.Any buildPartial()' in library class com.google.protobuf.Any$Builder
Warning: ***.***Service: can't find referenced method 'com.google.protobuf.Any pack(com.google.protobuf.Message)' in library class com.google.protobuf.Any

In my case the problem was that another dependency already had a transitive dependency on protobuf-javalite and it conflicted with the protobuf-java I was adding.
As per https://github.com/protocolbuffers/protobuf/issues/8104#issuecomment-887076083:
protobuf-java and protobuf-javalite should
never be included in the same project [...] if you're using Gradle, we
recommend an approach like this:
https://docs.gradle.org/current/userguide/dependency_downgrade_and_exclude.html#sec:excluding-transitive-deps
Telling the system to exclude protobuf-javalite because you explicitly
depend on protobuf-java is technically OK, but this might have
negative side effects to performance or code size on mobile platforms,
it also might be totally fine for you, your mileage may vary.
The problem was solved for me after excluding protobuf-javalite like this
configurations {
all*.exclude group: 'com.google.protobuf', module: 'protobuf-javalite'
}

Related

Can't find referenced method 'java.lang.Object injectMembers(dagger.MembersInjector,java.lang.Object)

Recently I upgrade Dagger 2.13 to 2.19, I have the below compile error at Proguard stage of this warning.
Warning: com.mypackage.MyClass_Factory: can't find referenced method 'java.lang.Object injectMembers(dagger.MembersInjector,java.lang.Object)' in program class dagger.internal.MembersInjectors
This also happens from Dagger 2.14.1 onwards. This happens only to MyClass which is in a library I include.
If I use
-dontwarn com.mypackage.MyClass_Factory
Then it will crash on runtime
java.lang.NoSuchMethodError: No static method injectMembers(Ldagger/MembersInjector;Ljava/lang/Object;)Ljava/lang/Object; in class Ldagger/internal/MembersInjectors; or its super classes (declaration of 'dagger.internal.MembersInjectors' appears in MyClass
Which means the warning from Proguard is a legit warning I should take care.
I search and found the issue is reported in https://github.com/google/dagger/pull/950#issuecomment-353223029
The solution by #ronshapiro is
You should shade the dagger.internal from one (or both) of the libraries. The old one is probably easiest
What does shade the dagger.internal means? How to solve my problem?

Not able to generate signed .apk due to including proguard

I am trying to generate a signed .apk in android studio. But I have not been able to do the same showing me the following error. I have tried searching a lot and found no solution. Please see the following code:
Warning:com.fasterxml.jackson.databind.ext.DOMSerializer: can't find
referenced class org.w3c.dom.bootstrap.DOMImplementationRegistry
Warning:com.fasterxml.jackson.databind.ext.PathDeserializer: can't
find referenced class java.nio.file.Path
Warning:com.fasterxml.jackson.databind.ext.PathDeserializer: can't
find referenced class java.nio.file.Paths
Warning:com.fasterxml.jackson.databind.ext.PathDeserializer: can't
find referenced class java.nio.file.Path
Warning:com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector$Java7Support:
can't find referenced class java.beans.Transient
Warning:com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector$Java7Support:
can't find referenced class java.beans.ConstructorProperties
Two possibilities:
1.Try disabling minify. Set minify to false on release build type and try to generate Apk.
2.Exclude the required java class files from minify using the proguard file. Libraries including Jackson usually needs to be excluded from minify.

Proguard configuration for Android Support v4 22.2.0

After updating dependencies on Gradle Android build to use com.android.support:support-v4:22.2.0 from local Maven extras repository (within SDK), Proguard started throwing these problems.
Warning: android.support.v4.app.DialogFragment: can't find referenced class android.support.v4.app.DialogFragment$DialogStyle
Warning: android.support.v4.app.FragmentTransaction: can't find referenced class android.support.v4.app.FragmentTransaction$Transit
Warning: android.support.v4.view.ViewCompat: can't find referenced class android.support.v4.view.ViewCompat$ResolvedLayoutDirectionMode
Warning: android.support.v4.view.ViewCompat: can't find referenced class android.support.v4.view.ViewCompat$LayoutDirectionMode
Warning: android.support.v4.view.ViewCompat: can't find referenced class android.support.v4.view.ViewCompat$LayerType
Warning: android.support.v4.view.ViewCompat: can't find referenced class android.support.v4.view.ViewCompat$AccessibilityLiveRegion
Warning: android.support.v4.view.ViewCompat: can't find referenced class android.support.v4.view.ViewCompat$ImportantForAccessibility
Warning: android.support.v4.view.ViewCompat: can't find referenced class android.support.v4.view.ViewCompat$OverScroll
Warning: android.support.v4.widget.DrawerLayout: can't find referenced class android.support.v4.widget.DrawerLayout$EdgeGravity
Warning: android.support.v4.widget.DrawerLayout: can't find referenced class android.support.v4.widget.DrawerLayout$LockMode
Warning: android.support.v4.widget.DrawerLayout: can't find referenced class android.support.v4.widget.DrawerLayout$State
Warning: there were 11 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)
Simply adding -dontwarn android.support.v4.** solves the problem, but I'd like an more specific/elegant solution, than ignoring all problems on support.v4 package
Can anybody tell what rules should be added, so these classes/#interfaces are correctly processed by Proguard?
The only solution is what you have mentioned, i.e -dontwarn android.support.v4.**. This is actually taken from the <path-to-android-sdk>/tools/proguard/proguard-android.txt, where it says:
# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version. We know about them, and they are safe.
-dontwarn android.support.**
It is safe to set don't warn for the support library classes according to the Android team. You can do this via:
## Support library
-dontwarn android.support.**

proguard can't find reference method

how can i solve this warnings ?
the "invoke" method referenced below,
public final native Object invoke(Object[] paramArrayOfObject) throws Throwable;
so, i must ignore this (just my opinion). but ignorewarnings are not helpful
does anybody knows how ignore this warnings?
ProGuard, version 4.9
Reading program jar [C:\Users\Las Plagas\Desktop\wbsys.jar]
Reading library jar [C:\Program Files\Java\jre7\lib\rt.jar]
Warning: WebAnalysisMain: can't find referenced method 'void invoke(java.lang.Class)' in class java.lang.invoke.MethodHandle
Warning: there were 1 unresolved references to program class members.
Your input classes appear to be inconsistent.
You may need to recompile the code or update the library versions.
Alternatively, you may have to specify the option
'-dontskipnonpubliclibraryclassmembers'.
Please correct the above warnings first.

Scala unsatisfiable cyclic dependency in "table-layout" library (Toolkit class)

When I try to compile with sbt some code containing an instance of a Table from this library I get this error:
java.lang.AssertionError: assertion failed: unsatisfiable cyclic dependency in 'class Toolkit'
It seems to work with Java so I don't understand why it fails in Scala.
Here is the toolkit class: http://code.google.com/p/table-layout/source/browse/branches/v1/tablelayout/src/com/esotericsoftware/tablelayout/Toolkit.java
As long as I get this error I'm totally stopped in my project :(.
Edit: It works with Scala 2.10.0 every Milestone, But this version of scala doesn't work with Android (or at least not yet ... [or at least not with libgdx ...]). So I still need a way to solve this problem even if it's a bit constraignant.
it will compile if you force scalac to load dependencies in correct order like this:
classOf[com.esotericsoftware.tablelayout.Toolkit[_,_,_]]
println(new com.badlogic.gdx.scenes.scene2d.ui.Table toString)
must be a bug which was accidentally fixed in 2.10

Categories