gpt4 book ai didi

Google Drive API doesn't play well with ProGuard (NPE)(Google Drive API在ProGuard(NPE)上表现不佳)

转载 作者:bug小助手 更新时间:2023-10-25 09:31:46 28 4
gpt4 key购买 nike



Currently, I'm having experience that, a piece of code, which makes use of Google Drive API is running fine without introducing ProGuard.

目前,我的经验是,一段利用Google Drive API的代码在没有引入ProGuard的情况下运行得很好。



However, after introducing ProGuard, I'm getting the following run-time error.

然而,在引入ProGuard之后,我得到了以下运行时错误。



    at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at com.google.api.client.util.Types.getActualParameterAtPosition(Types.java:329)
at com.google.api.client.util.Types.getIterableParameter(Types.java:309)
at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:546)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:350)
at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:586)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:289)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:76)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:71)
at com.google.api.client.http.HttpResponse.parseAs(HttpResponse.java:491)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:456)
at com.jstock.c.b.a(CloudFile.java:136)


Note, the crash happens at my code (which is com.jstock.c.b.a if I retrace using mapping.txt)

请注意,崩溃发生在我的代码(如果使用mapping.txt进行回溯,则为com.jstock.c.b.a)



// request is Files.List
FileList files = request.execute();


In my proguard, I thought having the following 2 key instructions, able to prevent the crash from happen : I tell ProGuard never touch on jackson and Google libraries.

在我的ProGuard中,我认为有以下两个关键说明,能够防止崩溃的发生:我告诉ProGuard永远不要接触杰克逊和谷歌的库。



-keep class org.codehaus.** { *; }
-keep class com.google.** { *; }
-keep interface org.codehaus.** { *; }
-keep interface com.google.** { *; }


But that doesn't work. NPE still happen at Types.java

但这并不管用。NPE仍在Types.java中发生



Note that, I had another try is that, I thought obfuscate process causes NPE happens. Hence, I try to disable it using -dontobfuscate. But this time, I will not able to generate APK file, and getting a popular error message : Conversion to Dalvik format failed with error 1

请注意,我有另一个尝试是,我认为混淆过程导致NPE发生。因此,我尝试使用-dontobfuscate禁用它。但这一次,我将无法生成APK文件,并收到一条常见的错误消息:转换为Dalvik格式失败,错误为1



Here is the proguard configuration which causes NPE at Google Drive API.

以下是在Google Drive API上导致NPE的proGuard配置。



-optimizationpasses 1
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

# Comment out the following line, will cause popular "Conversion to Dalvik format failed with error 1"
##-dontobfuscate

-dontwarn sun.misc.Unsafe
-dontwarn com.google.common.collect.MinMaxPriorityQueue
-dontwarn javax.swing.**
-dontwarn java.awt.**
-dontwarn org.jasypt.encryption.pbe.**
-dontwarn java.beans.**
-dontwarn org.joda.time.**
-dontwarn com.google.android.gms.**
-dontwarn org.w3c.dom.bootstrap.**
-dontwarn com.ibm.icu.text.**
-dontwarn demo.**

# Hold onto the mapping.text file, it can be used to unobfuscate stack traces in the developer console using the retrace tool
-printmapping mapping.txt

# Keep line numbers so they appear in the stack trace of the develeper console
-keepattributes *Annotation*,EnclosingMethod,SourceFile,LineNumberTable

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService

-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class com.actionbarsherlock.** { *; }
-keep interface com.actionbarsherlock.** { *; }

# https://sourceforge.net/p/proguard/discussion/182456/thread/e4d73acf
-keep class org.codehaus.** { *; }
-keep class com.google.** { *; }
-keep interface org.codehaus.** { *; }
-keep interface com.google.** { *; }

-assumenosideeffects class android.util.Log {
public static int d(...);
public static int i(...);
public static int e(...);
public static int v(...);
}

-keepclasseswithmembernames class * {
native <methods>;
}

-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}

-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}

-assumenosideeffects class android.util.Log {
public static *** d(...);
public static *** v(...);
public static *** i(...);
}

-keepclasseswithmembers class com.google.common.base.internal.Finalizer{
<methods>;
}


Is there anything else I can try?

还有什么我可以试一试的吗?



I'm not sure it might be caused by the combination of the libraries. (Although things run pretty well without introducing ProGuard)

我不确定这可能是由图书馆的组合造成的。(尽管在没有引入ProGuard的情况下,事情运行得很好)



enter image description here



If I look at the NPE crash location (Types.getActualParameterAtPosition(Types.java:329))

如果我看一看核反应堆坠毁地点(Types.getActualParameterAtPosition(Types.java:329))



private static Type getActualParameterAtPosition(Type type, Class<?> superClass, int position) {
ParameterizedType parameterizedType = Types.getSuperParameterizedType(type, superClass);
Type valueType = parameterizedType.getActualTypeArguments()[position];
// this is normally a type variable, except in the case where the class of iterableType is
// superClass, e.g. Iterable<String>
if (valueType instanceof TypeVariable<?>) {
Type resolve = Types.resolveTypeVariable(Arrays.asList(type), (TypeVariable<?>) valueType);
if (resolve != null) {
return resolve;
}
}
return valueType;
}


I suspect Types.getSuperParameterizedType returning null. So, I further look into Types.getSuperParameterizedType.

我怀疑Types.getSuperParamizedType返回空值。因此,我进一步研究了Types.getSuperPareterizedType。



public static ParameterizedType getSuperParameterizedType(Type type, Class<?> superClass) {
if (type instanceof Class<?> || type instanceof ParameterizedType) {
outer: while (type != null && type != Object.class) {
Class<?> rawType;
if (type instanceof Class<?>) {
// type is a class
rawType = (Class<?>) type;
} else {
// current is a parameterized type
ParameterizedType parameterizedType = (ParameterizedType) type;
rawType = getRawClass(parameterizedType);
// check if found Collection
if (rawType == superClass) {
// return the actual collection parameter
return parameterizedType;
}
if (superClass.isInterface()) {
for (Type interfaceType : rawType.getGenericInterfaces()) {
// interface type is class or parameterized type
Class<?> interfaceClass =
interfaceType instanceof Class<?> ? (Class<?>) interfaceType : getRawClass(
(ParameterizedType) interfaceType);
if (superClass.isAssignableFrom(interfaceClass)) {
type = interfaceType;
continue outer;
}
}
}
}
// move on to the super class
type = rawType.getGenericSuperclass();
}
}
return null;
}


What is the possible root cause that may cause getSuperParameterizedType returning null, after processed by ProGuard?

可能的根本原因是什么导致在ProGuard处理后,getSuperPareterizedType返回NULL?


更多回答
优秀答案推荐

A combination of the following has worked for me:

以下几点对我来说很管用:



-keep class com.google.** { *;}
-keep interface com.google.** { *;}
-dontwarn com.google.**

-dontwarn sun.misc.Unsafe
-dontwarn com.google.common.collect.MinMaxPriorityQueue
-keepattributes *Annotation*,Signature
-keep class * extends com.google.api.client.json.GenericJson {
*;
}
-keep class com.google.api.services.drive.** {
*;
}


This provided a working proguard compatible solution for a recent Google Drive project.

这为最近的一个Google Drive项目提供了一个有效的前卫兼容解决方案。



Cannot take all credit for this solution though, originally found at this link here

不过,我不能完全归功于此解决方案,最初可在此处的链接中找到



Proper combination is :

适当的组合为:



-keepattributes Signature,RuntimeVisibleAnnotations,AnnotationDefault

-保留属性签名、运行可见注释、注释默认



There's proguard configuration prepared by Google for project google-api-java-client

这是Google为Google-API-Java-Client项目准备的前卫配置



https://github.com/google/google-api-java-client/blob/57fe35766cbba0a0d5a9a296be81468d730a29f8/google-api-client-assembly/proguard-google-api-client.txt

Https://github.com/google/google-api-java-client/blob/57fe35766cbba0a0d5a9a296be81468d730a29f8/google-api-client-assembly/proguard-google-api-client.txt



First -keeping a class does not mean to not touch it. It means do not change its name, and use it as a basis for determining if other classes are not referenced & can be deleted.

第一,上课并不意味着不去碰它。这意味着不要更改其名称,并将其用作确定其他类是否未被引用或可以删除的基础。



Optimization still occurs, which is likely your problem. Next step I would do is try with:
-dontoptimize

优化仍然会发生,这很可能是您的问题。我要做的下一步是尝试:-不要优化



This should cause your other optimizations to be ignored.

这将导致您的其他优化被忽略。



BTW, not sure what version of SDK you are using. Am using 15, 20 is latest, and a proguard-project.txt file is create with the project. The optimization options it uses is:

顺便说一句,不确定您使用的是什么版本的SDK。我使用的是15,20是最新的,并且使用该项目创建了一个proGuard-project t.txt文件。它使用的优化选项是:



-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*


If turning off optimization gets it running, maybe turning off all the optimizations (that's what ! does) the SDK does, will allow you to do optimization as well.

如果关闭优化使其运行,可能会关闭所有优化(就是这样!做)SDK做,也会让你做优化。



There have been few update to GooglePlayServices lately. I don't like the new API. I had the same problems.

最近GooglePlayServices的更新很少。我不喜欢新的API。我也有同样的问题。



I couldn't compile signed app with proguard. Proguard template from Google didn't work for me.

我无法用ProGuard编译签名的应用程序。谷歌的ProGuard模板对我不起作用。



I add these four lines to my proguard config and it is working:

我将以下四行添加到我的proGuard配置中,它正在工作:



-dontwarn com.google.android.gms.**
-keep interface com.google.** { *; }
-keep class * extends com.google.api.client.json.GenericJson {*;}
-keep class com.google.api.services.drive.** {*;}


This is strange. Previous version of google-api-services-drive-v2 compiled without any problems.

这太奇怪了。以前版本的google-api-services-Drive-v2编译时没有任何问题。



I'm using the latest version at the moment: google-api-services-drive-v2-rev47-1.12.0-beta.jar

我目前使用的是最新版本:google-api-services-drive-v2-rev47-1.12.0-beta.jar



The method Types.getSuperParameterizedType relies on information about generics. Generics are erased in Java. The compiler only adds them as annotation attributes, the JVM ignores them, and ProGuard discards them unless you tell it not to. So this might help:

方法Types.getSuperParamizedType依赖于有关泛型的信息。泛型在Java中被删除了。编译器只将它们作为注释属性添加,JVM会忽略它们,ProGuard会丢弃它们,除非您告诉它不要这样做。因此,这可能会有所帮助:



-keepattributes *Annotation*


This worked for me.

这对我很管用。


-keepattributes Signature,RuntimeVisibleAnnotations,AnnotationDefault
-keepclassmembers class * {
@com.google.api.client.util.Key <fields>;
}
-dontwarn com.google.api.client.extensions.android.**
-dontwarn com.google.api.client.googleapis.extensions.android.**
-dontwarn com.google.android.gms.**
-dontnote java.nio.file.Files, java.nio.file.Path
-dontnote **.ILicensingService
-dontnote sun.misc.Unsafe
-dontwarn sun.misc.Unsafe
-keep class * extends com.google.api.client.json.** { *; }
-keep class * extends com.google.api.client.util.** { *; }
-keep class com.google.api.services.drive.** { *; }


does your code use any thing that implement Serializable? All of those need to be excluded too.

您的代码是否使用任何实现Serializable的东西?所有这些都需要被排除在外。


更多回答

Thanks. Going to try it out few more days later as I do not have a development machine right now.

谢谢。我现在没有显影机,几天后再试一试。

Thanks for acknowledging your sources an including a link back to my site :-)

感谢您确认您的消息来源,并包括指向我的站点的链接:-)

Signature was missing for me under -keepattributes

我的Under-Keep属性缺少签名

Instead of keeping all of com.google, the following fixed any crashes for me: -keep class com.google.api.client.** { *; }

以下代码没有保留com.google的所有内容,而是为我修复了所有崩溃:-保留类com.google.api.lient.**{*;}

I don't know Google prepares proguard configuration for their libraries. Gonna to try them out.

我不知道谷歌会为他们的库准备前卫配置。我要去试试看。

Try by using -keepattributes *Annotation* only, and disable optimization. No luck. Same NPE still happen :( dropbox.com/s/av2y9how104zsq1/proguard-project.txt dropbox.com/s/7yqvl652bua3gx5/project.properties

尝试仅使用-Keep Attributes*Annotation*,并禁用优化。没什么好运气的。同样的NPE仍然发生:(dropbox.com/s/av2y9how104zsq1/proguard-project.txt dropbox.com/s/7yqvl652bua3gx5/project.properties

Maybe check bin/proguard/obfuscated.jar to see what getSuperParameterizedType() method really has for code afterward. You could visually inspect for comparison to the source above. Not a direct answer, but may be useful depending on the result. Maybe use link if you need a decompiler.

也许可以检查bin/proGuard/obfuscated.jar,看看getSuperPareterizedType()方法对以后的代码到底有什么影响。您可以通过视觉检查与上面的源代码进行比较。不是直接的答案,但根据结果可能有用。如果需要反编译器,可以使用LINK。

Although I can see I already have -keepattributes *Annotation*,EnclosingMethod,SourceFile,LineNumberTable, I guess I'm going to try again with -keepattributes *Annotation* tonight. Thanks for the clarification.

虽然我可以看到我已经有了-KeepAttributes*Annotation*、EnlosingMethod、SourceFileLineNumberTable,但我想今晚我还会用-Keep Attributes*Annotation*再试一次。谢谢你的澄清。

Try by using -keepattributes *Annotation* only, and disable optimization. No luck. Same NPE still happen :( dropbox.com/s/av2y9how104zsq1/proguard-project.txt dropbox.com/s/7yqvl652bua3gx5/project.properties

尝试仅使用-Keep Attributes*Annotation*,并禁用优化。没什么好运气的。同样的NPE仍然发生:(dropbox.com/s/av2y9how104zsq1/proguard-project.txt dropbox.com/s/7yqvl652bua3gx5/project.properties

You're right, I had missed that it was already in your configuration. The problem must lie elsewhere. I'd have to investigate.

你说得对,我没想到它已经在你的配置中了。问题肯定出在其他地方。我得去调查一下。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com