Proguard enabled NoSuchMethodException



我没有遇到这个问题,如果我不像这样启用 Proguard:

buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

日志猫:

java.lang.RuntimeException: java.lang.NoSuchMethodException: com.example.NewUsers$f.<init> [class android.view.View]
at i.c.a.b.f.b(:147)
at androidx.recyclerview.widget.RecyclerView$g.a(:7078)
at androidx.recyclerview.widget.RecyclerView$v.a(:6235)
at androidx.recyclerview.widget.RecyclerView$v.b(:6118)
at androidx.recyclerview.widget.RecyclerView$v.d(:6114)
at androidx.recyclerview.widget.LinearLayoutManager$c.a(:2303)
at androidx.recyclerview.widget.LinearLayoutManager.a(:1627)
at androidx.recyclerview.widget.LinearLayoutManager.a(:1587)
at androidx.recyclerview.widget.LinearLayoutManager.c(:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(:3851)
at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(:1897)
at androidx.recyclerview.widget.RecyclerView$a.run(:414)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:996)
at android.view.Choreographer.doCallbacks(Choreographer.java:794)
at android.view.Choreographer.doFrame(Choreographer.java:725)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:981)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7807)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
Caused by: java.lang.NoSuchMethodException: com.example.NewUsers$f.<init> [class android.view.View]
at java.lang.Class.getConstructor0(Class.java:2332)
at java.lang.Class.getConstructor(Class.java:1728)
at i.c.a.b.f.b(:144)
at androidx.recyclerview.widget.RecyclerView$g.a(:7078) 
at androidx.recyclerview.widget.RecyclerView$v.a(:6235) 
at androidx.recyclerview.widget.RecyclerView$v.b(:6118) 
at androidx.recyclerview.widget.RecyclerView$v.d(:6114) 
at androidx.recyclerview.widget.LinearLayoutManager$c.a(:2303) 
at androidx.recyclerview.widget.LinearLayoutManager.a(:1627) 
at androidx.recyclerview.widget.LinearLayoutManager.a(:1587) 
at androidx.recyclerview.widget.LinearLayoutManager.c(:665) 
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(:4134) 
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(:3851) 
at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(:1897) 
at androidx.recyclerview.widget.RecyclerView$a.run(:414) 
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:996) 
at android.view.Choreographer.doCallbacks(Choreographer.java:794) 
at android.view.Choreographer.doFrame(Choreographer.java:725) 
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:981) 
at android.os.Handler.handleCallback(Handler.java:883) 
at android.os.Handler.dispatchMessage(Handler.java:100) 
at android.os.Looper.loop(Looper.java:237) 
at android.app.ActivityThread.main(ActivityThread.java:7807) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047) 

当前防护装置:

-keep class com.example.models.** { *; }

爪哇类:

public class NewUsers extends AppCompatActivity {
....
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_users);
....
mNewUsersList = findViewById(R.id.newUsers);
mNewUsersList.setHasFixedSize(true);
mNewUsersList.setLayoutManager(new LinearLayoutManager(this));
onStart();
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Users, UsersViewHolder> adapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
Users.class,
R.layout.users_list,
UsersViewHolder.class,
mDatabaseReference
) {
@Override
protected void populateViewHolder(UsersViewHolder usersViewHolder, Users users, int position) {
usersViewHolder.setName(users.getName());
}
};
mNewUsersList.setAdapter(adapter);
}
public static class UsersViewHolder extends RecyclerView.ViewHolder {
View mView;
public UsersViewHolder(@NonNull View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name) {
TextView mUserNameView = mView.findViewById(R.id.textViewUserName);
mUserNameView.setText(name);
}
}
}

有什么可能吗,我只想启用缩小和缩小发布

型:

public class Users {
String name;
public Users() {
}
public Users(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

检查此日志:

java.lang.RuntimeException: java.lang.NoSuchMethodException: com.example.NewUsers$f.<init> [class android.view.View]

正确的专业警卫规则是:

-keep class com.example.** { *; }

另外,请注意,这将排除com.example下的所有类,我建议将数据类移动到公共目录并混淆该完整目录。

你的类的包是com.example的,但 proguard 规则是针对com.example.models的。修改它应该可以解决问题。

解决方案 1:

将保留的类从com.example.model更改为com.example

更好的解决方案

如果您在使用它的文件中正确导入此类,并且不使用任何类型的反射,则 proguard 不应删除它。

尝试将其放置在可能不需要使用反射或间接方法来访问此类的公共位置

java.lang.RuntimeException: java.lang.NoSuchMethodException: com.example.NewUsers$f. [class android.view.View] 在I.C.A.B.F.B(:147(

找不到特定方法时引发的上述异常。这里由于保护。你应该尝试

-keep class com.example.** { *; }
-keepclassmembers class com.example.** { *; }

在整个包上添加@Keep使整个类保持原样。将其添加到方法或字段上将保持方法/字段(及其名称(以及类名不变。

OP 将是

public class HomeActivity extends AppCompatActivity { 
private TextView f2032b; 
private Button f2033c; 

仅供参考

您可以通过在项目的gradle.properties文件中包括以下内容来启用这些附加优化:

android.enableR8.fullMode=true

它可能需要您包含其他 ProGuard 规则以避免运行时问题。阅读有关Enable more aggressive optimizations的官方指南

相关内容

  • 没有找到相关文章

最新更新