gpt4 book ai didi

android - 是什么让某些 android 类 "must -keep"?

转载 作者:IT老高 更新时间:2023-10-28 22:26:14 26 4
gpt4 key购买 nike

我注意到"template"proguard.cfg 总是包含以下内容:

-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

为什么是这些特定的类而不是其他类?

这是不能被 ProGuard“优化”的类的完整列表吗?

最佳答案

简而言之,这些类在 proguard.cfg 模板中,因为这些类可以在 AndrodiManifest.xml 中声明。

考虑这个最小的应用程序:

CustomTextView.java:

package com.example.android;

import android.content.Context;
import android.widget.TextView;

public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
setText("The class name of this class is not important");
}
}

ExampleActivity.java:

package com.example.android;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class ExampleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new CustomTextView(this));
Log.i("ExampleActivity", "The class name of this class is important");
}
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android"
android:versionCode="1"
android:versionName="1.0">
<application>
<activity android:name=".ExampleActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
</application>
</manifest>

现在,没有线

-keep public class * extends android.app.Activity

在 proguard.cfg 文件中,proguard 可能会尝试将 ExampleActivity 重命名为 A虽然它对 AndroidManifest.xml 一无所知,所以它不会触及 list 。由于 Android 操作系统使用应用程序 list 中声明的​​类名来启动应用程序,因此 Android 操作系统将尝试实例化 ExampleActivity,但该类将不存在,因为 proguard 重命名了它!

对于 CustomTextView,proguard 可以将其重命名为 B,因为类的名称并不重要,因为它没有在 list ,仅由 proguard 在更改 CustomTextView 的类名时将更新的代码引用。

以某种方式,从模板 proguard.cfg 文件引用的所有类都可以在 list 中声明,因此 proguard 不得触及它们。

关于android - 是什么让某些 android 类 "must -keep"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8858375/

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