为什么Proguard重命名Activity的onCreate()方法

时间:2015-11-27 08:19:23

标签: android

我正在尝试从一个包含一些Activitys,Services的库项目构建一个Jar。当我不使用proguard时没关系,但是当我运行proguardJar时,Activity的onCreate()方法将被重命名为a,b,c ...但是Service的onCreate()是可以的。

通常,proGuard应该保留onCreate()。 Why does ProGuard keep the onCreate() method?

这是gradle文件。

import com.android.build.gradle.AppPlugin
import com.android.build.gradle.LibraryPlugin
import proguard.gradle.ProGuardTask

apply plugin: 'com.android.library'

android {
    compileSdkVersion 22
    buildToolsVersion "23.0.1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:support-v4:22.2.0'
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.google.code.gson:gson:2.4'
}

task buildJar(dependsOn: ['build'], type: Jar) {

    appendix = "demo"
    baseName = "androidJar"
    version = "1.0.0"
    classifier = "release"

    extension = "jar"
    archiveName = "AndroidJarDemo.jar"

    def srcClassDir = [project.buildDir.absolutePath + "/intermediates/classes/release"];
    from srcClassDir

    exclude "**/BuildConfig.class"
    exclude "**/BuildConfig\$*.class"
    exclude "**/R.class"
    exclude "**/R\$*.class"
}

task proguardJar(dependsOn: ['buildJar'], type: ProGuardTask) {
    configuration android.getDefaultProguardFile('proguard-android.txt')
    configuration 'proguard-rules.pro'

    String inJar = buildJar.archivePath.getAbsolutePath()

    injars inJar
    outjars inJar.substring(0, inJar.lastIndexOf('/')) + "/proguard-${buildJar.archiveName}"

    dontshrink

    Plugin plugin = getPlugins().hasPlugin(AppPlugin) ?
            getPlugins().findPlugin(AppPlugin) :
            getPlugins().findPlugin(LibraryPlugin)
    if (plugin != null) {
        List<String> runtimeJarList
        if (plugin.getMetaClass().getMetaMethod("getRuntimeJarList")) {
            runtimeJarList = plugin.getRuntimeJarList()
        } else if (android.getMetaClass().getMetaMethod("getBootClasspath")) {
            runtimeJarList = android.getBootClasspath()
        } else {
            runtimeJarList = plugin.getBootClasspath()
        }

        for (String runtimeJar : runtimeJarList) {
            libraryjars(runtimeJar)
        }
    }
}

以下是活动的一部分

    public class BaseActivity extends AppCompatActivity {
        ......
        @Override
            protected void onCreate(@Nullable Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                MyService.getInstance().addListener(mBaseCallback);
            }
        ......
        }

onCreate方法将重命名为。

protected void a(@Nullable Bundle var1) {
        super.onCreate(var1);
        e.a().a(this.c);
    }

这是proguard-rules.pro。

#sdk
-dontwarn **


-keep class com.lib.activity.**
-keep class com.lib.fragment.**

#keep native
-keepclasseswithmembernames class * {
    native <methods>;
}

-keep class android.support.design.widget.** { *; }
-keep interface android.support.design.widget.** { *; }
-dontwarn android.support.design.**

-dontwarn org.apache.commons.codec.binary.Base64
-dontwarn org.apache.commons.codec.binary.StringUtils
-dontwarn org.slf4j.impl.StaticLoggerBinder
-dontwarn org.slf4j.impl.StaticMarkerBinder
-dontwarn org.slf4j.impl.StaticMDCBinder

2 个答案:

答案 0 :(得分:0)

如果您不想重命名任何项目,则需要将该项目添加到列表中,保留在proguard-rules.pro文件中:

以下是对proguard-rules.pro文件的推荐:

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-dontnote
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-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

#keep all classes that might be used in XML layouts
-keep public class * extends android.os.Bundle
-keep public class * extends android.view.View
-keep public class * extends android.app.Fragment
-keep public class * extends android.support.v4.Fragment

#keep all public and protected methods that could be used by java reflection
-keepclassmembernames class * {
    public protected <methods>;
}

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}


-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
    public static final android.os.Parcelable$Creator *;
}
-renamesourcefileattribute ProGuard
-keepattributes SourceFile,LineNumberTable
-printmapping outputfile.txt

答案 1 :(得分:0)

Proguard运行3个阶段 - 它剥离未使用的或它认为未使用的代码,优化和混淆代码(不一定按此顺序)。

您所看到的是混淆步骤。防止这种情况的一种方法是将-dontobfuscate添加到您的proguard配置中。

默认情况下启用模糊处理,应用-dontobfuscate将禁用整个步骤。我不完全确定你想要这个,所以请重视方法。可能还有另一种方法可以保持我不知道的班级名称。