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

AndroidStudio打Jar包流程

程序员文章站 2022-07-14 18:48:17
...

简介:安卓发开中,往往需要将部分功能或者核心功能运用到另一个APP中,这时候,SDK的开发模式就体现出来其优越性。我们可以将这部分功能打包成jar包或者arr包,直接运用到另一个项目中,下面讲Androidstudio 打包jar包的流程。

1. build/rebuild项目,获取 classes.jar

build/rebuild一下项目,在library Module下找到classes.jar
D:\york_android\WifiDemo\wifishare\build\intermediates\compile_library_classes\debug\classes.jar

2.修改 build.gredle内容

在 dependencies 之后 加入

def SDK_BASENAME = "WifiSdk";
def SDK_VERSION = "_V1.0";
def sdkDestinationPath = "build";
def zipFile = file('build/intermediates/compile_library_classes/debug/classes.jar')

task deleteBuild(type: Delete) {
    delete sdkDestinationPath + SDK_BASENAME + SDK_VERSION + ".jar"
}

task makeJar(type: Jar) {
    from zipTree(zipFile)
    from fileTree(dir: 'src/main',includes: ['assets/**'])//将assets目录打入jar包
//    from fileTree(dir: 'src/main',includes: ['res/**'])//将res目录打入jar包
    baseName = SDK_BASENAME + SDK_VERSION
    destinationDir = file(sdkDestinationPath)
}

makeJar.dependsOn(deleteBuild, build)

build.gredle 完整代码如下:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.1"
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.google.code.gson:gson:2.8.5'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

def SDK_BASENAME = "WifiSdk";
def SDK_VERSION = "_V1.0";
def sdkDestinationPath = "build";
def zipFile = file('build/intermediates/compile_library_classes/debug/classes.jar')

task deleteBuild(type: Delete) {
    delete sdkDestinationPath + SDK_BASENAME + SDK_VERSION + ".jar"
}

task makeJar(type: Jar) {
    from zipTree(zipFile)
    from fileTree(dir: 'src/main',includes: ['assets/**'])//将assets目录打入jar包
//    from fileTree(dir: 'src/main',includes: ['res/**'])//将res目录打入jar包
    baseName = SDK_BASENAME + SDK_VERSION
    destinationDir = file(sdkDestinationPath)
}

makeJar.dependsOn(deleteBuild, build)

3.在 gredle 窗口 运行 makejar

如图:先找到gredle 窗口,点击 other

AndroidStudio打Jar包流程

在other中,找到 makeJar,点击 makeJar 开始打包

看到 BUILD SUCCESSFUL 表示打包成功,否则会提示相关错误,那就需要纠错。

关键点在下图

AndroidStudio打Jar包流程

相关标签: Android