【问题标题】:Obfuscating the .aar files混淆 .aar 文件
【发布时间】:2015-06-04 14:05:29
【问题描述】:

我使用

创建了一个 Android 库项目的 .aar 文件(包含资源和可绘制对象)
./gradlew assemble

我通过设置 minify == true 启用了混淆

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

但是,当我在启用 minify = true 的情况下运行上述 gradle 命令时,我得到 java.io.IOException: The output jar is empty. Did you specify the proper '-keep' options?

此错误指向什么以及如何混淆库 .aar 文件?

最好的问候

【问题讨论】:

  • 在没有 proguard 和 minifyEnabled 的情况下是否可以工作?

标签: android android-studio proguard obfuscation


【解决方案1】:

使用 Proguard 对我来说就像是一种魅力!

Proguard 用于缩小、混淆、优化代码。 Proguard 是 您的库有必要删除未使用的代码并进行反向操作 工程难度不大。图书馆的 Proguard 规则是 不同于一般的应用程序。如您所知,Proguard 重命名 使用无意义名称的类、变量和方法。你会 喜欢保留那些方法和类的名称 开发商会打电话。您将需要测试和验证混淆代码 来自生成的 AAR 文件。

库模块 build.gradle 你的库

buildTypes {
    release {
        // Enables code shrinking, obfuscation, and optimization for only
        // your project's release build type.
        minifyEnabled true


        // Includes the default ProGuard rules files that are packaged with
        // the Android Gradle plugin. To learn more, go to the section about
        // R8 configuration files.
        proguardFiles getDefaultProguardFile(
                'proguard-android-optimize.txt'),
                'proguard-rules.pro'

       }
}

在你的图书馆 proguard-rules.pro

# Save the obfuscation mapping to a file, so we can de-obfuscate any stack
# traces later on. Keep a fixed source file attribute and all line number
# tables to get line numbers in the stack traces.
# You can comment this out if you're not interested in stack traces.

-printmapping out.map
-keepparameternames
-renamesourcefileattribute SourceFile
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,EnclosingMethod

# Preserve all annotations.

-keepattributes *Annotation*

# Preserve all public classes, and their public and protected fields and
# methods.

-keep public class * {
    public protected *;
}

# Preserve all .class method names.

-keepclassmembernames class * {
    java.lang.Class class$(java.lang.String);
    java.lang.Class class$(java.lang.String, boolean);
}

# Preserve all native method names and the names of their classes.

-keepclasseswithmembernames class * {
    native <methods>;
}

# Preserve the special static methods that are required in all enumeration
# classes.

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

# Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
# You can comment this out if your library doesn't use serialization.
# If your code contains serializable classes that have to be backward
# compatible, please refer to the manual.

-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

# The library may contain more items that need to be preserved;
# typically classes that are dynamically created using Class.forName:

# -keep public class mypackage.MyClass
# -keep public interface mypackage.MyInterface
# -keep public class * implements mypackage.MyInterface

感谢....参考

https://dev.to/mohitrajput987/develop--publish-your-own-sdk-in-android---part-2getting-started-with-sdk-development-3159

【讨论】:

    【解决方案2】:

    Proguard 会削减未使用的类。库是独立的产品,并且有一些特定的入口点,不应被混淆。所以你需要添加规则来保持这个入口点。规则如下所示:

    -keep class packagename {public *;}
    

    【讨论】:

    • 这样做的问题是它不会混淆或优化代码。为此,您必须在“-keep”之后添加“allowobfuscation”和“allowoptimization”:-keep,allowoptimization,allowobfuscation public class yourpackage。**
    【解决方案3】:

    两个建议:

    1. 请确保此文件存在于库模块中: proguard-rules.pro.
    2. 请尝试运行“./gradlew :mylib:assembleRelease” “mylib” 替换为您的库模块名称。

    【讨论】:

      【解决方案4】:
      1. 将 library.pro 文件从该位置复制到您的库项目:
        ...\android-sdk\tools\proguard\examples
      2. 在从 Android Studio 构建时注释这些行,从命令行构建时应该保留/更新:
        -injars  in.jar
        -outjars out.jar
        -libraryjars  /lib/rt.jar
      3. 更新您的库项目build.gradle 文件以使用library.pro

        release {
          minifyEnabled true
          proguardFiles getDefaultProguardFile('proguard-android.txt'), 'library.pro'
        }
      4. 同步并构建项目,它现在应该会生成一个混淆的 AAR 文件。

      【讨论】:

        猜你喜欢
        • 2019-07-16
        • 2015-04-05
        • 1970-01-01
        • 1970-01-01
        • 2017-10-29
        • 2020-12-07
        • 2017-07-01
        • 1970-01-01
        • 2013-02-19
        相关资源
        最近更新 更多