ITPub博客

首页 > IT职业 > IT图书 > android sdk开发

android sdk开发

IT图书 作者:tong_sun 时间:2021-09-09 09:42:35 0 删除 编辑

一、前言

  前几天听到盆友在开发sdk,比较好奇,就试了下,这些是我的经验,望大家互相学习,有意见不能保留,这样大家才能互相学习啊

二、配置清单

1、AndroidManifest,若SDK在配置清单中申请权限,编译时就会报合并重复错误,只能去除主项目中原有的声明。因此,不建议在SDK内部声明,应改为在接入文档中说明,由主项目配置。

2、 标签,常见默认属性如:android:name,android:theme,android:lable... 如无切实必要,请去除这些属性,避免打包时与主项目冲突。

三、Gradle文件

打包编译重命名,抽取aar+版本号自动重命名并复制到指定目录下

apply plugin: 'com.android.library'static def releaseTime() {    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
android {
    compileSdkVersion 25
    buildToolsVersion '26.0.2'
    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 6
        versionName "5.1.0"
    }
    ...
    repositories {
        flatDir {
            dirs 'libs'
        }
    }
    libraryVariants.all { variant ->        if (variant.buildType.name == 'release') {
            variant.assemble.doLast {
                variant.outputs.each { output ->
                    def outputFile = output.outputFile                    if (outputFile != null && outputFile.name.endsWith('release.aar')) {
                        def fileName = "${project.name}-release-${android.defaultConfig.versionName}-${releaseTime()}"
                        def outputPath = "/build/aar"
                        copy {                            from outputFile
                            into outputPath
                        }
                        copy {                            from outputFile
                            into outputPath
                            rename { fileName + ".aar" }
                        }
                    }
                }
            }
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support:appcompat-v7:25.4.0'}

四、资源id命名规范

Values中的colors、strings、styles中的id命名应当注意保持一定的唯一性(如:命名统一加项目前缀),避免与主项目中的资源id冲突,造成SDK中的资源被覆盖,如theme、string等等。

当然,从另一个角度来看,这个特性也可以作为定制UI的一个思路,用主项目中定义的相同资源覆盖掉SDK中的资源属性,从而实现灵活改变SDK的样式。

五、避免创建Application对象

若SDK中定义了Application对象,而主项目也定义了Application或者应用了第三方Application,则需将android:name属性替换掉,才能正常编译,因为一个App只能指定一个Application,若直接替换,则会造成SDK中的Application无法初始化,引起一大波问题。

解决方案有二:

1、避免在SDK中创建Application对象,暴露出一个初始化方法,在主项目的Application相关方法如onCreate()中注入相关参数执行;

2、若无法避免,则可指定主项目的Application继承自SDK中的内置Application,问题可以解决。

/**
 * 
 *     author : fdm
 *     time   : 2018/03/09
 *     desc   : SDK初始化
 *     version: 1.0
 * 
 */public class MySDK {     private static Context sContext;    public MySDK() {     }    //提供给第三方调用,进行初始化     public static void initSDK(Context context) {         sContext = context;         initLog(context);     }    private static void initLog(Context context) {        new LogUtil.Builder(context)                 .setLogSwitch(true)                 .setGlobalTag("fdm")                 .setLogHeadSwitch(true)                 .setLogFilter(LogUtil.D);     }    public static Context getContext() {        return sContext;     } }

六、无法将第三方库打包进aar的问题

library打包出来的 AAR ,不会将依赖的第三方库打包进去。这个问题也是由来已久,详情可见: http://www.apkbus.com/blog-892197-78114.html


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4405/viewspace-2806359/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-12

  • 博文量
    89
  • 访问量
    791