Android-Proguard和改装2?
我正在为我的项目使用fromAndroid-Proguard和改装2?,android,retrofit,obfuscation,retrofit2,android-proguard,Android,Retrofit,Obfuscation,Retrofit2,Android Proguard,我正在为我的项目使用fromProguard,下面的代码在我的Proguard规则中。pro: # Retrofit -keep class com.google.gson.** { *; } -keep class com.google.inject.** { *; } -keep class org.apache.http.** { *; } -keep class org.apache.james.mime4j.** { *; } -keep class javax.inject.** {
Proguard
,下面的代码在我的Proguard规则中。pro
:
# Retrofit
-keep class com.google.gson.** { *; }
-keep class com.google.inject.** { *; }
-keep class org.apache.http.** { *; }
-keep class org.apache.james.mime4j.** { *; }
-keep class javax.inject.** { *; }
-keep class javax.xml.stream.** { *; }
-keep class retrofit.** { *; }
-keep class com.google.appengine.** { *; }
-keepattributes *Annotation*
-keepattributes Signature
-dontwarn com.squareup.okhttp.*
-dontwarn rx.**
-dontwarn javax.xml.stream.**
-dontwarn com.google.appengine.**
-dontwarn java.nio.file.**
-dontwarn org.codehaus.**
-dontwarn retrofit2.**
-dontwarn org.codehaus.mojo.**
-keep class retrofit2.** { *; }
-keepattributes Exceptions
-keepattributes RuntimeVisibleAnnotations
-keepattributes RuntimeInvisibleAnnotations
-keepattributes RuntimeVisibleParameterAnnotations
-keepattributes RuntimeInvisibleParameterAnnotations
-keepattributes EnclosingMethod
-keepclasseswithmembers class * {
@retrofit2.http.* <methods>;
}
-keepclasseswithmembers interface * {
@retrofit2.* <methods>;
}
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions
# Add any classes the interact with gson
# the following line is for illustration purposes
-keep class com.example.asheq.zanis_postmans.ListAddressesActivity
-keep class com.example.asheq.zanis_postmans.ListOrderActivity
-keep class com.example.asheq.zanis_postmans.LoginActivity
-keep class com.example.asheq.zanis_postmans.SendReportsActivity
-keep class com.example.asheq.track.TrackLocationService
-keep class com.example.asheq.track.TrackLocationApplication
# Hide warnings about references to newer platforms in the library
-dontwarn android.support.v7.**
# don't process support library
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }
这是我的TrackLocationApplication.class
:
public class TrackLocationApplication extends Application {
private static final String KEY_REQUEST_DATA_NAME = "KEY_REQUEST_DATA_NAME";
private LocationRequestData locationRequestData;
private Location startLocation;
@Override
public void onCreate() {
super.onCreate();
//checkAndSetDeviceId();
checkAndSetUserName();
if (!retrieveLocationRequestData()) {
//setLocationRequestData(LocationRequestData.FREQUENCY_MEDIUM);
setLocationRequestData(LocationRequestData.FREQUENCY_HIGH);
}
initializeDB();
}
private boolean retrieveLocationRequestData() {
String name = PreferenceManager.getDefaultSharedPreferences(this).getString(KEY_REQUEST_DATA_NAME, null);
if (!TextUtils.isEmpty(name)) {
LocationRequestData data = LocationRequestData.valueOf(name);
if (data != null) {
locationRequestData = data;
return true;
}
}
return false;
}
public void setLocationRequestData(LocationRequestData requestData) {
locationRequestData = requestData;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
preferences.edit().putString(KEY_REQUEST_DATA_NAME, requestData.name()).apply();
}
public LocationRequestData getLocationRequestData() {
if (locationRequestData == null) {
if (!retrieveLocationRequestData()) {
setLocationRequestData(LocationRequestData.FREQUENCY_HIGH);
}
}
return locationRequestData;
}
public Location getStartLocation() {
return startLocation;
}
public void setStartLocation(Location startLocation) {
this.startLocation = startLocation;
}
public LocationRequest createLocationRequest() {
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(locationRequestData.getInterval());
locationRequest.setFastestInterval(locationRequestData.getFastestInterval());
locationRequest.setPriority(locationRequestData.getPriority());
locationRequest.setSmallestDisplacement(locationRequestData.getSmallestDisplacement());
return locationRequest;
}
// private void checkAndSetDeviceId() {
// if (TextUtils.isEmpty(TrackLocationPreferencesManager.getDeviceId(this))) {
// String deviceId = Utils.getUniqueDeviceId(this);
// TrackLocationPreferencesManager.setDeviceId(deviceId, this);
// }
// }
private void checkAndSetUserName() {
if (TextUtils.isEmpty(TrackLocationPreferencesManager.getUserName(this))) {
String userName = android.os.Build.MODEL;
TrackLocationPreferencesManager.setUserName(userName, this);
}
}
private void initializeDB() {
Configuration.Builder configurationBuilder = new Configuration.Builder(this);
configurationBuilder.addModelClasses(LocationData.class);
ActiveAndroid.initialize(configurationBuilder.create());
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
你需要保留你的模型,不要混淆它们。我看到您的模型有错误,sqlite无法处理您的模型。应该是这样的:
-keep class package.to.yourmodels.** { *; }
解决了我的问题:
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\asheq\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Retrofit
-keep class com.google.gson.** { *; }
-keep public class com.google.gson.** {public private protected *;}
-keep class com.google.inject.** { *; }
-keep class org.apache.http.** { *; }
-keep class org.apache.james.mime4j.** { *; }
-keep class javax.inject.** { *; }
-keep class javax.xml.stream.** { *; }
-keep class retrofit.** { *; }
-keep class com.google.appengine.** { *; }
-keepattributes *Annotation*
-keepattributes Signature
-dontwarn com.squareup.okhttp.*
-dontwarn rx.**
-dontwarn javax.xml.stream.**
-dontwarn com.google.appengine.**
-dontwarn java.nio.file.**
-dontwarn org.codehaus.**
-dontwarn retrofit2.**
-dontwarn org.codehaus.mojo.**
-keep class retrofit2.** { *; }
-keepattributes Exceptions
-keepattributes RuntimeVisibleAnnotations
-keepattributes RuntimeInvisibleAnnotations
-keepattributes RuntimeVisibleParameterAnnotations
-keepattributes RuntimeInvisibleParameterAnnotations
-keepattributes EnclosingMethod
-keepclasseswithmembers class * {
@retrofit2.http.* <methods>;
}
-keepclasseswithmembers interface * {
@retrofit2.* <methods>;
}
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions
# Add any classes the interact with gson
# the following line is for illustration purposes
-keep class com.example.asheq.zanis_postmans.ListAddressesActivity
-keep class com.example.asheq.zanis_postmans.ListOrderActivity
-keep class com.example.asheq.zanis_postmans.LoginActivity
-keep class com.example.asheq.zanis_postmans.SendReportsActivity
-keep class com.example.asheq.track.TrackLocationService
-keep class com.example.asheq.track.TrackLocationApplication
-keep class com.example.asheq.models.** { *; }
# Hide warnings about references to newer platforms in the library
-dontwarn android.support.v7.**
# don't process support library
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.MapActivity
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
# To support Enum type of class members
-keepclassmembers enum * { *; }
-keep class com.activeandroid.** { *; }
-keep class com.activeandroid.**.** { *; }
-keep class * extends com.activeandroid.Model
-keep class * extends com.activeandroid.serializer.TypeSerializer
-keepclassmembers class ** {
@org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
<init>(java.lang.Throwable);
}
#在此处添加特定于项目的程序规则。
#默认情况下,此文件中的标志附加到指定的标志
#在C:\Users\asheq\AppData\Local\Android\Sdk/tools/proguard/proguard-Android.txt中
#您可以通过更改文件来编辑包含路径和顺序
#build.gradle中的指令。
#
#有关详细信息,请参阅
# http://developer.android.com/guide/developing/tools/proguard.html
#在此处添加任何特定于项目的保留选项:
#如果您的项目使用带JS的WebView,请取消注释以下内容
#并为JavaScript接口指定完全限定的类名
#类别:
#-keepclassmembers类fqcn.of.javascript.interface.for.webview{
#公众*;
#}
#改造
-保持类com.google.gson.*{*;}
-保持公共类com.google.gson.*{public-private-protected*;}
-保持类com.google.inject.*{*;}
-保持类org.apache.http.*{*;}
-保持类org.apache.james.mime4j.*{*;}
-保持类javax.inject.*{*;}
-保持类javax.xml.stream.*{*;}
-保持类更新。**{*;}
-保持类com.google.appengine.*{*;}
-keepattributes*注释*
-保留署名
-dontwarn com.squareup.okhttp*
-dontwarn rx**
-dontwarn javax.xml.stream**
-dontwarn com.google.appengine**
-dontwarn java.nio.file**
-dontwarn org.codehaus**
-dontwarn 2**
-dontwarn org.codehaus.mojo**
-保持类2.*{*;}
-保留特例
-KeepAttribute运行时可视注释
-keepattributes运行时不可见标记
-keepattributes RuntimeVisibleParameterAnnotations
-keepattributes RuntimeInvisibleParameterAnnotations
-keepattributes封闭方法
-keepclassswithmembers类*{
@2.http.*;
}
-KeepClassSwithMembers接口*{
@改装2.*;
}
#平台对Android上不存在的类型调用Class.forName以确定平台。
-dontnote 2.站台
#在iOS上的RoboVM上运行时使用的平台。将不会在运行时使用。
-dontnote 2.平台$IOS$MainThreadExecutor
#在Java 8虚拟机上运行时使用的平台。将不会在运行时使用。
-dontwarn 2.Platform$Java8
#保留泛型类型信息以供转换器和适配器反射使用。
-保留署名
#保留声明的已检查异常以供代理实例使用。
-保留特例
#添加任何与gson交互的类
#下一行用于说明
-保持类com.example.asheq.zanis_postmans.listAddresses活动
-keep class com.example.asheq.zanis_postmans.ListOrderActivity
-保持类com.example.asheq.zanis_postmans.LoginActivity
-保持类com.example.asheq.zanis_postmans.SendReportsActivity
-keep class com.example.asheq.track.TrackLocationService
-keep class com.example.asheq.track.tracklocation应用程序
-保持类com.example.asheq.models.*{*;}
#隐藏有关引用库中较新平台的警告
-dontwarn android.support.v7**
#不处理支持库
-keep类android.support.v7.*{*;}
-保留android.support.v7.*{*;}
-keep public class*扩展android.app.Activity
-keep public class*扩展android.app.Application
-keep public class*扩展android.app.MapActivity
-keep public class*扩展了android.app.Service
-keep public class*扩展了android.content.BroadcastReceiver
-keep public class*扩展了android.content.ContentProvider
#支持类成员的枚举类型的步骤
-keepclassmembers枚举*{*;}
-保持类com.activeandroid.*{*;}
-保持类com.activeandroid.**{*;}
-keep class*扩展com.activeandroid.Model
-keep class*扩展com.activeandroid.serializer.TypeSerializer
-keepclassmembers类**{
@org.greenrobot.eventbus.Subscribe;
}
-保持枚举org.greenrobot.eventbus.ThreadMode{*;}
#仅当使用AsyncExecutor时才需要
-keepclassmembers类*扩展org.greenrobot.eventbus.util.ThrowableFailureEvent{
(java.lang.Throwable);
}
根据第3.8.1条的规定
当前的Proguard规则(截至2018年12月)为
#改装不反映通用参数。InnerClass需要使用签名和
#使用InnerClass需要EnclosingMethod。
-keepattributes签名、内部类、封闭方法
#优化时保留服务方法参数。
-keepclassmembers、allowshrinking、AllowFusion接口*{
@2.http.*;
}
#忽略用于构建工具的注释。
-dontwarn org.codehaus.mojo.animal_sniffer.ignorejrrequirement
#忽略JSR305注释以嵌入可空性信息。
-dontwarn javax.annotation**
#由NoClassDefFoundError try/catch保护,仅在类路径上使用。
-dontwarn kotlin.单位
#只能由Kotlin使用的顶级函数。
-dontwarn改装2.-科特林
对于改装GSON您需要在proguard rules.pro
文件中添加以下规则
对于改装,请添加以下规则
2021解决方案
在数据类之前使用@保留
注释,以便保留它们。这样就不需要有单独的Proguard规则
表示在生成时缩小代码时不应删除带注释的元素。这通常用于仅通过反射访问的方法和类,因此编译器可能认为代码未使用
保持
数据类列表(
val id:String=“”,
val name:String=“”
)
来源:我注意到为每个类添加@Keep
注释,实际上它修复了所有问题,但也不会更改e的名称
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\asheq\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Retrofit
-keep class com.google.gson.** { *; }
-keep public class com.google.gson.** {public private protected *;}
-keep class com.google.inject.** { *; }
-keep class org.apache.http.** { *; }
-keep class org.apache.james.mime4j.** { *; }
-keep class javax.inject.** { *; }
-keep class javax.xml.stream.** { *; }
-keep class retrofit.** { *; }
-keep class com.google.appengine.** { *; }
-keepattributes *Annotation*
-keepattributes Signature
-dontwarn com.squareup.okhttp.*
-dontwarn rx.**
-dontwarn javax.xml.stream.**
-dontwarn com.google.appengine.**
-dontwarn java.nio.file.**
-dontwarn org.codehaus.**
-dontwarn retrofit2.**
-dontwarn org.codehaus.mojo.**
-keep class retrofit2.** { *; }
-keepattributes Exceptions
-keepattributes RuntimeVisibleAnnotations
-keepattributes RuntimeInvisibleAnnotations
-keepattributes RuntimeVisibleParameterAnnotations
-keepattributes RuntimeInvisibleParameterAnnotations
-keepattributes EnclosingMethod
-keepclasseswithmembers class * {
@retrofit2.http.* <methods>;
}
-keepclasseswithmembers interface * {
@retrofit2.* <methods>;
}
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions
# Add any classes the interact with gson
# the following line is for illustration purposes
-keep class com.example.asheq.zanis_postmans.ListAddressesActivity
-keep class com.example.asheq.zanis_postmans.ListOrderActivity
-keep class com.example.asheq.zanis_postmans.LoginActivity
-keep class com.example.asheq.zanis_postmans.SendReportsActivity
-keep class com.example.asheq.track.TrackLocationService
-keep class com.example.asheq.track.TrackLocationApplication
-keep class com.example.asheq.models.** { *; }
# Hide warnings about references to newer platforms in the library
-dontwarn android.support.v7.**
# don't process support library
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.MapActivity
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
# To support Enum type of class members
-keepclassmembers enum * { *; }
-keep class com.activeandroid.** { *; }
-keep class com.activeandroid.**.** { *; }
-keep class * extends com.activeandroid.Model
-keep class * extends com.activeandroid.serializer.TypeSerializer
-keepclassmembers class ** {
@org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
<init>(java.lang.Throwable);
}
-dontwarn okio.**
-dontwarn javax.annotation.**
-dontwarn retrofit2.Platform$Java8
# Retrofit does reflection on generic parameters. InnerClasses is required to use Signature and
# EnclosingMethod is required to use InnerClasses.
-keepattributes Signature, InnerClasses, EnclosingMethod
# Retain service method parameters when optimizing.
-keepclassmembers,allowshrinking,allowobfuscation interface * {
@retrofit2.http.* <methods>;
}
# Ignore annotation used for build tooling.
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
# Ignore JSR 305 annotations for embedding nullability information.
-dontwarn javax.annotation.**
# Guarded by a NoClassDefFoundError try/catch and only used when on the classpath.
-dontwarn kotlin.Unit
# Top-level functions that can only be used by Kotlin.
-dontwarn retrofit2.-KotlinExtensions
# Retrofit
-dontwarn retrofit2.**
-dontwarn org.codehaus.mojo.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepattributes *Annotation*
-keepattributes RuntimeVisibleAnnotations
-keepattributes RuntimeInvisibleAnnotations
-keepattributes RuntimeVisibleParameterAnnotations
-keepattributes RuntimeInvisibleParameterAnnotations
-keepattributes EnclosingMethod
-keepclasseswithmembers class * {
@retrofit2.* <methods>;
}
-keepclasseswithmembers interface * {
@retrofit2.* <methods>;
}
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions
##--- Begin:GSON ----
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature
# For using GSON @Expose annotation
-keepattributes *Annotation*
# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }
# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }
# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
# keep enum so gson can deserialize it
-keepclassmembers enum * { *; }
# Application classes that will be serialized/deserialized over Gson
-keep class net.mreunionlabs.wob.model.request.** { *; }
-keep class net.mreunionlabs.wob.model.response.** { *; }
-keep class net.mreunionlabs.wob.model.gson.** { *; }
##--- End:GSON ----
##--- End:GSON ----
-keep,allowobfuscation class com.your_library.rest_objects.** { *; }
-keep public class * {
public protected *;
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
// https://developer.android.com/studio/projects/android-library
consumerProguardFiles 'consumer-rules.pro'
}
}