Android开发SDK教程

本文详细介绍SDK开发过程中的关键步骤,包括前期准备、代码混淆设置等内容,帮助开发者高效完成SDK开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于最近作者刚开发完一个SDK项目,所有现在整理一下在此期间碰到的坑,希望别人在做SDK的时候不用再重新踩一遍坑了。好了废话不多说,现在开始


一、前期准备


1.sdk说白了就是把一些方法函数封装起来,然后打成一个Jar包给第三方开发者来调用,以减少第三方开发者工作量和一些重复的内容


2.因为jar包有的逻辑算法是比较隐私和重要的,所以Jar一定是要做混淆的。

如果有人用过一些开源的项目库或者Jar的时候有可能会注意到,Jar混淆一些代码,

如:使用fastjson.jar

一款自动解析Json的一个Jar包,该Jar要添加的混淆是这样的

-dontwarn com.alibaba.fastjson.**
-keep class com.alibaba.fastjson.** { *; }
-keep class com.alibaba.fastjson.TypeReference.** { *; }


3.知道了有上面混淆的这个点之后,所以在开发之前要把包名给设计好,


4.剩下的就是进行封装代码了,根据各自业务自行封这个就不用多说了。


二、代码混淆设置


因为我们项目是用Android Studio进行开发的,所以在此只针对于使用Android Studio的开发者


以下是build.gradle 重要混淆代码

task buildJar(dependsOn: ['compileReleaseJavaWithJavac'], type: Jar) {
    appendix = "demo"
    baseName = "androidJar"
    version = "1.0.0"
    classifier = "release"
    //后缀名
    extension = "jar"
    //最终的 Jar 包名,如果没设置,默认为 [baseName]-[appendix]-[version]-[classifier].[extension]
    archiveName = "****.jar" //自行修改
    //需打包的资源所在的路径集
    def srcClassDir = [project.buildDir.absolutePath + "/intermediates/classes/release"];
    //初始化资源路径集
    from srcClassDir
    //去除路径集下部分的资源
//    exclude "org/chaos/demo/jar/MainActivity.class"
//    exclude "org/chaos/demo/jar/MainActivity\$*.class"
//    exclude "org/chaos/demo/jar/BuildConfig.class"
//    exclude "org/chaos/demo/jar/BuildConfig\$*.class"
//    exclude "**/R.class"
//    exclude "**/R\$*.class"
    //只导入资源路径集下的部分资源
    include "com/app/**/*.class"
    include "tv/danmaku/ijk/**/*.class"
    //注: exclude include 支持可变长参数
}

task proguardJar(dependsOn: ['buildJar'], type: ProGuardTask) {
    delete(project.buildDir.absolutePath)
    def mappingOutputDir = new File(project.buildDir.absolutePath + "/outputs/mapping/release/")
    if (!mappingOutputDir.exists()) {
        mappingOutputDir.mkdirs()
    }
    def mappingOutputFile = new File(mappingOutputDir, "mapping.txt")
    mappingOutputFile.createNewFile()

    //Android 默认的 proguard 文件
    configuration android.getDefaultProguardFile('proguard-android.txt')
    //manifest 注册的组件对应的 proguard 文件
    configuration project.buildDir.absolutePath + "/intermediates/proguard-rules/release/aapt_rules.txt"
    configuration 'proguard-rules.pro'
    String inJar = buildJar.archivePath.getAbsolutePath()
    //输入 jar
    injars inJar
    //输出 jar
    outjars 'build/releaseLibs/****.jar' //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) {
            //给 proguard 添加 runtime
            libraryjars(runtimeJar)
        }
    }

三、在proguard-rules.pro 自行添加一些需要混淆包的代码


# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in E:\androidDev\sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript inter
# class:
#-keepclassmembers class fqcn.of.javascript.inter.for.webview {
#   public *;
#}
-optimizationpasses 5
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose
-ignorewarnings
-dontusemixedcaseclassnames         # 是否使用大小写混合
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*,!code/allocation/variable

# 记录生成的日志数据,在mapping目录下
-printmapping build/outputs/mapping/release/mapping.txt

-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 com.android.vending.licensing.ILicensingService
-keep public class android.net.http.SslError

-dontwarn javax.annotation.**

-dontwarn android.app.**
-dontwarn android.support.**
-dontwarn android.view.**
-dontwarn android.widget.**

-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    void set*(***);
    *** get*();
}

-keepclasseswithmembernames class * {
     native <methods>;
}

# Keep native methods
-keepclassmembers class * {
    native <methods>;
}

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

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

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

-keepclassmembers class * extends android.app.Activity {
    public void *(android.view.View);
}

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

-keep class * implements android.os.Parcelable {
    public static final android.os.Parcelable$Creator *;
}
#==============用到注解相关混淆配置=================
-keepattributes Exceptions
-keepattributes InnerClasses
-keepattributes Signature
#-keepattributes Deprecated
#-keepattributes SourceFile
#-keepattributes LineNumberTable
#-keepattributes LocalVariable*Table
#-keepattributes Synthetic

-allowaccessmodification
-keepattributes *Annotation*
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-repackageclasses ''

#==============用到反射相关混淆配置=================
-keepattributes Signature
-keepattributes EnclosingMethod

#==================gson==========================
-dontwarn com.google.**
-keep class com.google.gson.** {*;}

#==================protobuf======================
-dontwarn com.google.**
-keep class com.google.protobuf.** {*;}

#fastjson.jar 的类路径 com.alibaba.fastjson
-dontwarn com.alibaba.fastjson.**
-keep class com.alibaba.fastjson.** { *; }
-keep class com.alibaba.fastjson.TypeReference.** { *; }

#httpmime.jar 的类路径 org.apache.http.entity.mime
-dontwarn org.apache.http.**
-keep class org.apache.http.** { *; }

#日志模块 slf4j 和 logback 配置
-keep class ch.qos.** { *; }
-keep class org.slf4j.** { *; }
#-keepattributes *Annotation*
-dontwarn ch.qos.logback.**
-dontwarn ch.qos.logback.core.net.*
-keep class ch.qos.logback.** { *; }

#项目里所有的module不需要混淆

-dontwarn com.demo.sdk.**
-keep class com.demo.sdk.** { *; }

-dontwarn tv.danmaku.ijk.**
-keep class tv.danmaku.ijk.** { *; }

-keepclasseswithmembers class tv.danmaku.ijk.media.player.IjkMediaPlayer {
    <fields>;
    <methods>;
}

-keep class * implements java.io.Serializable { *; }

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

-keepclassmembers class * {
   public <init>(org.json.JSONObject);
}

#-dontwarn okio.**
#-keep class okio.** {*;}

#-dontwarn okhttp3.**
#-keep class okhttp3.** {*;}

-dontwarn javax.annotation.**
-dontwarn com.android.volley.toolbox.**

四、小结

好了以上就是开发sdk所涉及的一些知识,如果阅读了该文章的小伙伴还有什么疑问,可以随时评论,我会及时反馈的。

[14本经典Android开发教程]-3-Android SDK 中文开发文档 什么是 Android? Android 是一个专门针对移动设备的软件集,它包括一个操作系统,中间件和一些重要的应用程序。Beta版的 Android SDK 提供了在Android 平台上使用JaVa语言进行Android应用开发必须的工具和API接口。 特性 · 应用程序框架 支持组件的重用与替换 · Dalvik 虚拟机 专为移动设备优化 · 集成的浏览器 基于开源的WebKit 引擎 · 优化的图形库 包括定制的2D图形库,3D图形库基于OpenGL ES 1.0 (硬件加速可选) · SQLite 用作结构化的数据存储 · 多媒体支持 包括常见的音频、视频和静态图像格式 (如 MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, GIF) · GSM 电话技术 (依赖于硬件) · 蓝牙Bluetooth, EDGE, 3G, 和 WiFi (依赖于硬件) · 照相机,GPS,指南针,和加速度计(accelerometer) (依赖于硬件) · 丰富的开发环境 包括设备模拟器,调试工具,内存及性能分析图表,和Eclipse集成开发环境插件 Android 架构 下图显示的是Android操作系统的主要组件。每一部分将会在下面具体描述。 已上传2本: [14本经典Android开发教程]-1-Android开发从入门到精通http://download.csdn.net/detail/cleopard/8355245 [14本经典Android开发教程]-2-Android开发手册—API函数详解 http://download.csdn.net/detail/cleopard/8374487 剩余11本稍后上传!@或直接从这里寻找@ http://download.csdn.net/user/cleopard/album @更多@ http://cleopard.download.csdn.net/ 福利 http://xuemeilaile.com 17份软件测试文档 http://download.csdn.net/album/detail/1425 13份WPF经典开发教程 http://download.csdn.net/album/detail/1115 C#资料合辑二[C#桌面编程入门篇] http://download.csdn.net/album/detail/957 C#资料合辑一[C#入门篇] http://download.csdn.net/album/detail/669 [Csharp高级编程(第6版)](共8压缩卷) http://download.csdn.net/album/detail/667 10个[精品资源]Java学习资料合辑[一] http://download.csdn.net/album/detail/663 10个C#Socket编程代码示例 http://download.csdn.net/album/detail/631 6份GDI+程序设计资源整合[全零分] http://download.csdn.net/album/detail/625 2014年移动游戏行业数据分析 http://download.csdn.net/detail/cleopard/8340331 一文读懂2014年全球互联网广告新生态 http://download.csdn.net/detail/cleopard/8340303
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序邦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值