欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

将Android类库打包成.aar文件的新建

程序员文章站 2022-12-03 10:06:25
在需要打包的libary下新建一个maven-release-aar.gradle文件,文件内容如下: // 1.maven-插件 apply plugin: '...

在需要打包的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
//}

在AS的右侧的Gradle选项中选择:mylibs下的upload中的uploadArchives 选项生成的.aar文件在 /Users/xxxx/Desktop/mylibs 目录里面

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

本地引用:
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.**