简体   繁体   English

隐藏库类 - Android

[英]Hide classes of library - Android

I have 3 projects that are used as libraries within a 4th (main project). 我有3个项目在第4个(主项目)中用作库。

The 3 projects are complied within each other as follows (build.gradle): 这三个项目相互编译如下(build.gradle):

Library Project: 图书馆项目:

  • Project A 项目A.

     compile project(":projectA") compile project(":projectB") 
  • Project B 项目B.

     compile project(':projectC') 

Main Project: 主要项目:

compile(name: 'projectA', ext: 'aar')
compile(name: 'projectB', ext: 'aar')
compile(name: 'projectC', ext: 'aar')

I would like to do something to the "Library Project", so that from within the Main Project, if I click on any class from within the Library project, I should either not be able to see the code, or it should be encrypted. 我想对“图书馆计划”做点什么,所以在主项目中,如果我点击图书馆项目中的任何一个类,我应该要么看不到代码,要么加密。

So for example if there is InterfaceA in ProjectA, and the main activity of the Main Project implements that interface, if I "Ctrl-Click" into the interface, the result should be similar to what I specified above. 因此,例如,如果ProjectA中有InterfaceA ,并且Main Project的主要活动实现了该接口,如果我“Ctrl-Click”进入接口,结果应该类似于我上面指定的。

I understand Proguard does something similar, but that is only if you are building a release .apk, I need the same result for compiled libraries. 我理解Proguard做了类似的事情,但只有在你构建一个版本.apk时,我需要为编译库提供相同的结果。

Many projects use ProGuard to achieve this protection. 许多项目使用ProGuard来实现这种保护。

However there are some limitations: 但是有一些限制:

  • ProGuard's main use is is removing as much debug information, line numbers and names as possible from the bytecode without changing what the bytecode actually does. ProGuard的主要用途是从字节码中删除尽可能多的调试信息,行号和名称,而不改变字节码实际执行的操作。 It replaces the names of members and arguments, non-public classes with meaningless ones, for example vehicleLicensePlate might become _a . 它取代了成员和参数的名称,非公共类的名称,例如vehicleLicensePlate可能变为_a As any code maintainer will relate, bad member and variable names make maintenance really hard. 任何代码维护者都会关联,坏成员和变量名称使维护变得非常困难。
  • ProGuard can (slightly) modify bytecode by optimising as much as possible (computing constants defined as expressions, playing around with inlining, etc. The optimisations are listed here: http://proguard.sourceforge.net/FAQ.html#optimization ) ProGuard可以(略微)通过尽可能优化来修改字节码(计算常量定义为表达式,玩内联等等。优化在此处列出: http//proguard.sourceforge.net/FAQ.html#optimization
  • ProGuard does not encrypt the bytecode - the JVM needs to see the actual bytecode otherwise it could not run the program. ProGuard不加密字节码 - JVM需要查看实际的字节码,否则它无法运行程序。

So, obfuscation only makes it harder to reverse-engineer and understand a library, it cannot make this task impossible. 因此,混淆只会使反向工程和理解库变得更加困难,它无法使这项任务变得不可能。

One last pointer: ProGuard dumps a file containing a list of what it has changed, in particular the line numbers. 最后一个指针:ProGuard转储一个文件,其中包含已更改内容的列表,尤其是行号。 When you get stack traces back from your customers (or through online tools like Crashlytics) you can revert the obfuscation so you can debug. 当您从客户(或通过Crashlytics等在线工具)获得堆栈跟踪时,您可以恢复混淆,以便进行调试。 In any release-build process, you need to find a way to save this file. 在任何发布 - 构建过程中,您都需要找到保存此文件的方法。

This file is also needed when you make incremental releases of your library so the obfuscation is consistent to the previously released version. 在创建库的增量版本时也需要此文件,以使模糊处理与先前发布的版本一致。 If you don't, the customer cannot drop-in replace your library and will have to do a complete rebuild (and link) of their app. 如果不这样做,客户就无法直接替换您的库,并且必须对其应用程序进行完整的重建(和链接)。

While ProGuard is a free-n-easy option which just works, there are other free and paid-for obfuscators. 虽然ProGuard是一个免费的n-easy选项,但它还有其他免费和付费的混淆器。 Some offer a few more features, but they are fundamentally the same, and the compatibility of ProGuard with IDEs, tools and services is excellent. 有些提供了更多功能,但它们基本相同,ProGuard与IDE,工具和服务的兼容性非常好。

You could set all the methods you don't want to be public to default, so they can't be used outside of the original project. 您可以将所有不希望公开的方法设置为默认值,因此不能在原始项目之外使用它们。 And also, you should separate the libraries from the app project, compile them, and use them as external dependencies. 此外,您应该将库与应用程序项目分开,编译它们,并将它们用作外部依赖项。 If you don't want the source code of the library published, just don't add it to the compilation options. 如果您不希望发布库的源代码,请不要将其添加到编译选项中。 If somebody else than you needs to use your library, publish it using bintray, or just add the compiled aar/jar files to the app project. 如果除了你之外的其他人需要使用你的库,使用bintray发布它,或者只是将编译的aar / jar文件添加到app项目中。

Here's a guide for the whole process: https://inthecheesefactory.com/blog/how-to-upload-library-to-jcenter-maven-central-as-dependency/en 以下是整个过程的指南: https//inthecheesefactory.com/blog/how-to-upload-library-to-jcenter-maven-central-as-dependency/en

Alternatively, you can build library projects using maven (I find it a lot easier than using gradle), take a look here for an example: https://github.com/simpligility/android-maven-plugin/tree/master/src/test/projects/libraryprojects 或者,您可以使用maven构建库项目(我发现它比使用gradle容易得多),请看这里的示例: https//github.com/simpligility/android-maven-plugin/tree/master/src /测试/项目/ libraryprojects

and a concrete project: https://github.com/fcopardo/BaseViews-Android 和一个具体的项目: https//github.com/fcopardo/BaseViews-Android

2 steps: 2个步骤:

  1. add your libraries to local maven repo 将您的库添加到本地maven仓库

  2. use maven dependences instead of project dependences. 使用maven依赖而不是项目依赖。

You cannot do that. 你不能这样做。 A compiled library has .class files (bytecode), which can be de-compiled and viewed using various de-compilers like JD-GUI etc. Android Studio has a built-in de-compiler which makes it more easy for someone to just ctrl-click and view the .class file. 已编译的库具有.class文件(字节码),可以使用各种反编译器(如JD-GUI等)对其进行反编译和查看.Android Studio具有内置的反编译器,使得人们更容易进行ctrl - 单击并查看.class文件。 The best option you have is to obfuscate your code. 您拥有的最佳选择是对代码进行模糊处理。 Here are some obfuscators you can use. 这里有一些你可以使用的混淆器。 But always keep in mind that it's never impossible to reverse-engineer something. 但请始终牢记,逆向工程是绝对不可能的 Everything is hackable. 一切都是可以破解的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM