混淆.aar文件

时间:2015-03-31 09:28:24

标签: android android-studio proguard obfuscation

我使用

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

我通过设置minify == true

启用了混淆
buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

但是,当我使用minify enabled = true运行提到的gradle命令时,我得到java.io.IOException: The output jar is empty. Did you specify the proper '-keep' options?

此错误指向什么以及如何对库.aar文件进行模糊处理?

最好的问候

4 个答案:

答案 0 :(得分:3)

Proguard削减未使用的课程。库是独立的产品,并且有一些特定的入口点,不应该是混淆的。所以你需要添加规则来保持这个入口点。规则看起来像这样:

-keep class packagename {public *;}

答案 1 :(得分:1)

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

  

Proguard用于缩小,混淆,优化代码。 Proguard是   库删除未使用的代码并进行反向处理所必需的   工程学有点困难。库的Proguard规则是   与正常应用不同。如您所知,Proguard重命名   使用无意义名称的类,变量和方法。你会   喜欢保留这些方法和类的名称,因为   开发人员会致电。您将需要测试和验证混淆代码   从生成的AAR文件中获取。

库模块 您的图书馆的 build.gradle

# 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

在您的图书馆的 proguard-rules.pro 内部

HTML5

感谢...。参考

  

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

答案 2 :(得分:0)

两个建议:

  1. 请确保库模块中存在此文件: proguard-rules.pro。
  2. 请尝试运行&#34; ./ gradlew:mylib:assembleRelease&#34;随着 &#34; MYLIB&#34;替换为您的库模块名称。

答案 3 :(得分:0)

  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文件。

相关问题