Android 简单开发sdk教程一 接口写法和混淆规则

前言

之前一直打包的sdk都是给内部项目使用的,没有提供给别的客户使用过,所以一直以来都是简单的功能打包,也不混淆,等最后的项目再写混淆方法。

最近要求提供sdk给客户但又要混淆业务逻辑,只好摸索一下,百度和谷歌都没有找到很好的教程。只能根据用过的第三方sdk的经验写一下,以下记录一下自己的想法。不能保证是优秀的代码。毕竟赶鸭子上架,有相关的经验的大佬,希望也能提点建议。

新建model

由于我直接打包aar就好了,所以就不配置jar包的设置了。

新建接口

新建文件 ITestUtil

public interface ITestUtil {

    /**
     * 打印
     * @param msg
     */
    void doLog(String msg);
}

新建业务

新建文件 TestUtil

public class TestUtil implements ITestUtil {

    @Override
    public void doLog(String msg) {
        Log.e("TestUtil", "打印:" + msg);
    }
}

混淆

基本混淆代码

直接放进你的混淆文件即可 proguard-rules.pro

#表示混淆时不使用大小写混合类名
-dontusemixedcaseclassnames
#表示不跳过library中的非public的类
-dontskipnonpubliclibraryclasses
#打印混淆的详细信息
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
##表示不进行校验,这个校验作用 在java平台上的
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

# 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);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keepclassmembers class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator CREATOR;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**

# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep

-keep @android.support.annotation.Keep class * {*;}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}
#忽略警告
-ignorewarnings
#保证是独立的jar,没有任何项目引用,如果不写就会认为我们所有的代码是无用的,从而把所有的代码压缩掉,导出一个空的jar
-dontshrink
#保护泛型
-keepattributes Signature

自定义相关混淆

添加自己的相关混淆配置

# 不混淆某个类(使用者可以看到全部信息)
-keep class com.rdwl.testlib.ITestUtil {*;}
# 不混淆某个类(使用者可以看到类名)
-keep class com.rdwl.testlib.TestUtil

# 不混淆某个类中以 public 开始的方法(使用者可以看到该方法)
#-keepclassmembers class com.rdwl.testlib.TestUtil {
#    public *;
#}

开启混淆

打开 model 的 build.gradle 文件

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

混淆正式打包

点击右侧边栏的 Gradle,在窗口中找到你的 model,选择 Tasks -> build -> build,如果你打开 Gradle 窗口显示 Nothing to show,你可以看下我的这篇文章:Android studio右侧Gradle窗口显示 nothing to show

等待打包完成。你就能在model的build文件夹找到打包好的aar
在这里插入图片描述

完事

发布了103 篇原创文章 · 获赞 31 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/sinat_38184748/article/details/102954901
今日推荐