将Android类库打包成.aar文件

  1. 在需要打包的libary下新建一个maven-release-aar.gradle文件,文件内容如下:

     // 1.maven-插件
    apply plugin: 'maven'
    // 2.maven-信息
    ext {// ext is a gradle closure allowing the declaration of global properties
        PUBLISH_GROUP_ID = 'com.example'
        PUBLISH_ARTIFACT_ID = 'mylibs'
        PUBLISH_VERSION = 1.0
    }
    
    // 3.maven-输出路径
    uploadArchives {
        repositories.mavenDeployer {
            //这里就是最后输出地址,在自己电脑上新建个文件夹,把文件夹路径粘贴在此
            //注意”file://“ + 路径,有三个斜杠,别漏了
            repository(url: "file:///Users/xxxx/Desktop/mylibs")
    
            pom.project {
                groupId project.PUBLISH_GROUP_ID
                artifactId project.PUBLISH_ARTIFACT_ID
                version 1.0
            }
        }
    }
    
    以下代码会生成jar包源文件,如果是不开源码,请不要输入这段
    aar包内包含注释
    //task androidSourcesJar(type: Jar) {
    //    classifier = 'sources'
    //    from android.sourceSets.main.java.sourceFiles
    //}
    //
    //artifacts {
    //    archives androidSourcesJar
    //}
    
  2. 在AS的右侧的Gradle选项中选择:mylibs下的upload中的uploadArchives 选项生成的.aar文件在 /Users/xxxx/Desktop/mylibs 目录里面

  3. (可选) 上传到github上去。 a:在github中新建organization,再继续新建repository,再将/Users/xxxx/Desktop/mylibs文件夹内文件上传。

  4. 本地引用:
    a: build.gradle中添加引用:
     allprojects {
    repositories {
        google()
        jcenter()

        maven { url "file:///Users/xxxxx/Desktop/mylibs" }
    }
}

b: app模块下的build.gralde中添加类库引用:
dependencies {
compile('com.example:mylibs:1.0')
}

5. 网络引用:
a: build.gradle中添加引用:

     allprojects {
    repositories {
        google()
        jcenter()

        maven { url "https://github.com/xxxxxx/mylibs/raw/master" }
    }
}

b: app模块下的build.gralde中添加类库引用:
dependencies {
compile('com.example:mylibs:1.0')
}

6. 混淆:(默认的混淆文件,可根据自己需求定义)

     #指定压缩级别
    -optimizationpasses 5

    #不跳过非公共的库的类成员
    -dontskipnonpubliclibraryclassmembers

    #混淆时采用的算法
    -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

    #把混淆类中的方法名也混淆了
    -useuniqueclassmembernames

    #优化时允许访问并修改有修饰符的类和类的成员 
    -allowaccessmodification

    #将文件来源重命名为“SourceFile”字符串
    -renamesourcefileattribute SourceFile
    #保留行号
    -keepattributes SourceFile,LineNumberTable
    #保持泛型
    -keepattributes Signature

    #保持所有实现 Serializable 接口的类成员
    -keepclassmembers class * implements java.io.Serializable {
        static final long serialVersionUID;
        private 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();
    }

    #Fragment不需要在AndroidManifest.xml中注册,需要额外保护下
    -keep public class * extends android.support.v4.app.Fragment
    -keep public class * extends android.app.Fragment

    # 保持测试相关的代码
    -dontnote junit.framework.**
    -dontnote junit.runner.**
    -dontwarn android.test.**
    -dontwarn android.support.test.**
    -dontwarn org.junit.**
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值