我不明白如何在Proguard文件中使用此-keepattributes Signature
?
Proguard手册提示是""签名"在JDK 5.0及更高版本中进行编译时,要求属性能够访问泛型类型。"
我使用这个attritube测试,这是代码和progurad代码?
Souce Code
```
package com.example.pc.myapplication.utils;
import android.support.annotation.Keep;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GenericTypeTest {
public static void test() {
Student t = beanRepository();
GenericInterface<Student> genericInterface = new GenericInterface<Student>() {
@Override public void print(Student o) {
System.out.println(o.toString());
}
};
System.out.println(genericInterface);
genericInterface.print(t);
GenericClass<Student> genericClass = new GenericClass<>();
System.out.println(genericClass);
genericClass.print(t);
genericMethod(t);
genericCollection();
}
public static void genericCollection() {
List<Student> students = listsRepository();
if (students != null && students.size() > 0) {
for (Student item : students) {
System.out.println(item);
}
}
HashMap<Integer, Student> hashMap = mapsRepository();
if (hashMap != null && hashMap.size() > 0) {
for (Map.Entry<Integer, Student> item : hashMap.entrySet()) {
Integer key = item.getKey();
Student value = item.getValue();
System.out.println(key + ";" + value);
}
}
}
public interface GenericInterface<T> {
void print(T t);
}
public static class GenericClass<T> {
public void print(T t) {
System.out.println(t);
}
}
public static <T> void genericMethod(T t) {
System.out.println(t);
}
public static Student beanRepository() {
Student student = new Student();
student.setName("Hello");
student.setAge(30);
student.setSex(false);
return student;
}
public static List<Student> listsRepository() {
ArrayList<Student> students = new ArrayList<>();
for (int i = 0; i < 50; i++) {
Student student = new Student();
student.setName("Index" + i);
student.setAge(i + 30);
student.setSex(false);
students.add(student);
}
return students;
}
public static HashMap<Integer, Student> mapsRepository() {
HashMap<Integer, Student> hashMap = new HashMap<>();
for (int i = 0; i < 50; i++) {
Student student = new Student();
student.setName("Index" + i);
student.setAge(i + 30);
student.setSex(false);
hashMap.put(i, student);
}
return hashMap;
}
@Keep public static class Student {
private String name;
private int age;
private boolean sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
@Override public String toString() {
return "Student{" + "name='" + name + '\'' + ", age=" + age + ", sex=" + sex + '}';
}
}
}
```
不要添加attribetu生成的代码
```
package com.example.pc.myapplication.utils;
import android.support.annotation.Keep;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
public class GenericTypeTest
{
public static void a()
{
Student localStudent = c();
Object localObject = new b()
{
public void a(GenericTypeTest.Student paramAnonymousStudent)
{
System.out.println(paramAnonymousStudent.toString());
}
};
System.out.println(localObject);
((b)localObject).a(localStudent);
localObject = new a();
System.out.println(localObject);
((a)localObject).a(localStudent);
a(localStudent);
b();
}
public static <T> void a(T paramT)
{
System.out.println(paramT);
}
public static void b()
{
Object localObject1 = d();
Object localObject2;
if ((localObject1 != null) && (((List)localObject1).size() > 0))
{
localObject1 = ((List)localObject1).iterator();
while (((Iterator)localObject1).hasNext())
{
localObject2 = (Student)((Iterator)localObject1).next();
System.out.println(localObject2);
}
}
localObject1 = e();
if ((localObject1 != null) && (((HashMap)localObject1).size() > 0))
{
localObject1 = ((HashMap)localObject1).entrySet().iterator();
while (((Iterator)localObject1).hasNext())
{
Object localObject3 = (Map.Entry)((Iterator)localObject1).next();
localObject2 = (Integer)((Map.Entry)localObject3).getKey();
localObject3 = (Student)((Map.Entry)localObject3).getValue();
System.out.println(localObject2 + ";" + localObject3);
}
}
}
public static Student c()
{
Student localStudent = new Student();
localStudent.setName("Hello");
localStudent.setAge(30);
localStudent.setSex(false);
return localStudent;
}
public static List<Student> d()
{
ArrayList localArrayList = new ArrayList();
int i = 0;
while (i < 50)
{
Student localStudent = new Student();
localStudent.setName("Index" + i);
localStudent.setAge(i + 30);
localStudent.setSex(false);
localArrayList.add(localStudent);
i += 1;
}
return localArrayList;
}
public static HashMap<Integer, Student> e()
{
HashMap localHashMap = new HashMap();
int i = 0;
while (i < 50)
{
Student localStudent = new Student();
localStudent.setName("Index" + i);
localStudent.setAge(i + 30);
localStudent.setSex(false);
localHashMap.put(Integer.valueOf(i), localStudent);
i += 1;
}
return localHashMap;
}
@Keep
public static class Student
{
private int age;
private String name;
private boolean sex;
public int getAge()
{
return this.age;
}
public String getName()
{
return this.name;
}
public boolean isSex()
{
return this.sex;
}
public void setAge(int paramInt)
{
this.age = paramInt;
}
public void setName(String paramString)
{
this.name = paramString;
}
public void setSex(boolean paramBoolean)
{
this.sex = paramBoolean;
}
public String toString()
{
return "Student{name='" + this.name + '\'' + ", age=" + this.age + ", sex=" + this.sex + '}';
}
}
public static class a<T>
{
public void a(T paramT)
{
System.out.println(paramT);
}
}
public static abstract interface b<T>
{
public abstract void a(T paramT);
}
}
```
添加-keepattributes Signature
生成的代码
```
package com.example.pc.myapplication.utils;
import android.support.annotation.Keep;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
public class GenericTypeTest
{
public static void a()
{
Student localStudent = c();
Object localObject = new b()
{
public void a(GenericTypeTest.Student paramAnonymousStudent)
{
System.out.println(paramAnonymousStudent.toString());
}
};
System.out.println(localObject);
((b)localObject).a(localStudent);
localObject = new a();
System.out.println(localObject);
((a)localObject).a(localStudent);
a(localStudent);
b();
}
public static <T> void a(T paramT)
{
System.out.println(paramT);
}
public static void b()
{
Object localObject1 = d();
Object localObject2;
if ((localObject1 != null) && (((List)localObject1).size() > 0))
{
localObject1 = ((List)localObject1).iterator();
while (((Iterator)localObject1).hasNext())
{
localObject2 = (Student)((Iterator)localObject1).next();
System.out.println(localObject2);
}
}
localObject1 = e();
if ((localObject1 != null) && (((HashMap)localObject1).size() > 0))
{
localObject1 = ((HashMap)localObject1).entrySet().iterator();
while (((Iterator)localObject1).hasNext())
{
Object localObject3 = (Map.Entry)((Iterator)localObject1).next();
localObject2 = (Integer)((Map.Entry)localObject3).getKey();
localObject3 = (Student)((Map.Entry)localObject3).getValue();
System.out.println(localObject2 + ";" + localObject3);
}
}
}
public static Student c()
{
Student localStudent = new Student();
localStudent.setName("Hello");
localStudent.setAge(30);
localStudent.setSex(false);
return localStudent;
}
public static List<Student> d()
{
ArrayList localArrayList = new ArrayList();
int i = 0;
while (i < 50)
{
Student localStudent = new Student();
localStudent.setName("Index" + i);
localStudent.setAge(i + 30);
localStudent.setSex(false);
localArrayList.add(localStudent);
i += 1;
}
return localArrayList;
}
public static HashMap<Integer, Student> e()
{
HashMap localHashMap = new HashMap();
int i = 0;
while (i < 50)
{
Student localStudent = new Student();
localStudent.setName("Index" + i);
localStudent.setAge(i + 30);
localStudent.setSex(false);
localHashMap.put(Integer.valueOf(i), localStudent);
i += 1;
}
return localHashMap;
}
@Keep
public static class Student
{
private int age;
private String name;
private boolean sex;
public int getAge()
{
return this.age;
}
public String getName()
{
return this.name;
}
public boolean isSex()
{
return this.sex;
}
public void setAge(int paramInt)
{
this.age = paramInt;
}
public void setName(String paramString)
{
this.name = paramString;
}
public void setSex(boolean paramBoolean)
{
this.sex = paramBoolean;
}
public String toString()
{
return "Student{name='" + this.name + '\'' + ", age=" + this.age + ", sex=" + this.sex + '}';
}
}
public static class a<T>
{
public void a(T paramT)
{
System.out.println(paramT);
}
}
public static abstract interface b<T>
{
public abstract void a(T paramT);
}
}
```
看一下代码是一样的,为什么要使用这个-keepattributes Signature
。
这是整个proguard文件:
``` #在此处添加项目特定的ProGuard规则。 #默认情况下,此文件中的标志将附加到指定的标志 #在D:\ develop \ AndroidSDK / tools / proguard / proguard-android.txt中 #您可以通过更改proguardFiles来编辑包含路径和顺序 build.gradle中的#directive。 # #有关详细信息,请参阅 #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 *;
}
# Uncomment this to preserve the line number information for
# debugging stack traces.
-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
-renamesourcefileattribute SourceFile
-keepattributes Signature
-dontwarn okhttp3.**
-keep class okhttp3.**{*;}
-keep interface okhttp3.** { *; }
#保留HomeFragment中所有变量的方法不被混淆
-keep class com.example.pc.myapplication.HomeFragment{
*;
}
#保留utils中未调用方法
-keepclassmembers class com.example.pc.myapplication.utils.FileUtils {
*;
}
#保留support包
-keep class android.support.**{
*;
}
```