gpt4 book ai didi

java - ProGuard 混淆导致 ClassNotFoundException

转载 作者:行者123 更新时间:2023-12-01 22:38:23 25 4
gpt4 key购买 nike

我尝试通过 ProGuard 混淆我的代码,但是当我这样做时,它甚至找不到 AndroidManifest 中定义的初始 Activity (ClassNotFoundException)。

这就是 Activity 的样子:

public class LoadingActivity extends Activity
{
private static final String TAG = LoadingActivity.class.getSimpleName();
private int effectiveWidth;
private int effectiveHeight;
private AsyncTask<Void, Void, Void> bitmapLoaderTask;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_loading);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

initSpecs();
}

@Override
protected void onPause()
{
super.onPause();

if (bitmapLoaderTask != null)
{
bitmapLoaderTask.cancel(true);
}
}

@Override
protected void onStart()
{
super.onStart();

if (ResizedBitmapMapping.isStillHoldingAllImages())
{
Logger.log(TAG, "ResizedBitmapMapping still contains all images, skip loading...", LogController.isLoggingEnabled(), Log.DEBUG);

startMenu();
}
else
{
bitmapLoaderTask = new BitmapLoaderTask(getResources(), effectiveWidth, effectiveHeight, this);
bitmapLoaderTask.execute();
}
}

@SuppressWarnings("deprecation")
private void initSpecs()
{
PackageNameHolder.setPackageName(getPackageName());

WindowManager windowManager = (WindowManager)this.getSystemService(Context.WINDOW_SERVICE);
Point size = new Point();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
{
windowManager.getDefaultDisplay().getSize(size);

effectiveWidth = size.x;
effectiveHeight = size.y;
}
else
{
Display display = windowManager.getDefaultDisplay();

effectiveWidth = display.getWidth();
effectiveHeight = display.getHeight();
}
}

public void startMenu()
{
final Intent gameIntent = new Intent(this, MenuActivity.class);
startActivity(gameIntent);
overridePendingTransition(0, 0);
}

public void cancel()
{
finish();
}
}

我的 proguard.cfg 看起来像这样:

-dontoptimize 
-dontshrink
-verbose

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

-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}

-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
public static final *** NULL;
}

-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
@com.google.android.gms.common.annotation.KeepName *;
}

-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}

为了测试我添加了

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

然后,应用程序可以运行,但不会被混淆(这很清楚)。有什么想法如何正确混淆吗?其实这是 ProGuard 必须关心的事情,对吧?

这是启动应用程序时的错误消息:

10-25 00:29:04.600: E/AndroidRuntime(3299): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mydomain.myapp/com.mydomain.myapp.LoadingActivity}: java.lang.ClassNotFoundException: com.mydomain.myapp.LoadingActivity in loader dalvik.system.PathClassLoader[/data/app/com.mydomain.myapp-2.apk]
10-25 00:29:04.600: E/AndroidRuntime(3299): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1680)
10-25 00:29:04.600: E/AndroidRuntime(3299): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
10-25 00:29:04.600: E/AndroidRuntime(3299): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
10-25 00:29:04.600: E/AndroidRuntime(3299): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
10-25 00:29:04.600: E/AndroidRuntime(3299): at android.os.Handler.dispatchMessage(Handler.java:99)
10-25 00:29:04.600: E/AndroidRuntime(3299): at android.os.Looper.loop(Looper.java:130)
10-25 00:29:04.600: E/AndroidRuntime(3299): at android.app.ActivityThread.main(ActivityThread.java:3835)
10-25 00:29:04.600: E/AndroidRuntime(3299): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 00:29:04.600: E/AndroidRuntime(3299): at java.lang.reflect.Method.invoke(Method.java:507)
10-25 00:29:04.600: E/AndroidRuntime(3299): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
10-25 00:29:04.600: E/AndroidRuntime(3299): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
10-25 00:29:04.600: E/AndroidRuntime(3299): at dalvik.system.NativeStart.main(Native Method)
10-25 00:29:04.600: E/AndroidRuntime(3299): Caused by: java.lang.ClassNotFoundException: com.mydomain.myapp.LoadingActivity in loader dalvik.system.PathClassLoader[/data/app/com.mydomain.myapp-2.apk]
10-25 00:29:04.600: E/AndroidRuntime(3299): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
10-25 00:29:04.600: E/AndroidRuntime(3299): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
10-25 00:29:04.600: E/AndroidRuntime(3299): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
10-25 00:29:04.600: E/AndroidRuntime(3299): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-25 00:29:04.600: E/AndroidRuntime(3299): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1672)
10-25 00:29:04.600: E/AndroidRuntime(3299): ... 11 more

[更新]
如果我添加

-keep public class com.mydomain.myapp.LoadingActivity

它启动了,但随后找不到下一个 Activity,因为它在 AndroidManfest 中搜索混淆的 Activity?

10-25 13:20:16.159: E/AndroidRuntime(3195): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.mydomain.myapp/com.mydomain.myapp.G}; have you declared this activity in your AndroidManifest.xml?
10-25 13:20:16.159: E/AndroidRuntime(3195): at com.mydomain.myapp.LoadingActivity.a(Unknown Source)
10-25 13:20:16.159: E/AndroidRuntime(3195): at com.mydomain.myapp.e.a.a(Unknown Source)
10-25 13:20:16.159: E/AndroidRuntime(3195): at com.mydomain.myapp.e.a.onPostExecute(Unknown Source)

最佳答案

proguard.config 参数应该是

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

或者为了优化,

proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txt

这样必要的设置就包含在内,例如:

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}

关于java - ProGuard 混淆导致 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26557090/

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