android studio打jar包并添加三方包

记录android studio打包JAR并且添加第三方jar包。

示例:servicesdk是module需要打包成jar包项目。

android studio打jar包并添加三方包_第1张图片

libs中是三方包,我们需要将这些包添加到jar中。

接下来这里是关键代码,在我们项目中打开

android studio打jar包并添加三方包_第2张图片

这里就是我们打包JAR时将三方包添加,需要注意的是在,androidStudio3.0前“build/intermediates/bundles/release/libs/miit_mdid_1.0.10.aar”,3.0以后的一些版本为“build/intermediates/packaged-classes/release/libs/miit_mdid_1.0.10.aar'”,所有当项目中没有“bundles”或者packaged-classes时可以更换过来即可。

最后使用命令  gradlew makeJar 执行

结果:

android studio打jar包并添加三方包_第3张图片

 然后使用压缩软件或者其它可以打开jar查看:

android studio打jar包并添加三方包_第4张图片

都已添加到jar包中。

以下是build.gradle全部

apply plugin: 'com.android.library'
android {
    compileSdkVersion 27
    buildToolsVersion '28.0.3'
    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        dexOptions {
            preDexLibraries = false
        }
        ndk {
            abiFilters 'armeabi-v7a', 'x86'
        }
    }
    buildTypes {

        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    repositories {

        flatDir {
            dirs 'libs'
        }

    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    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'
    implementation files('libs/classes.jar')
    implementation files('libs/glide-3.7.0.jar')
    implementation files('libs/android-support-multidex.jar')
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    //移动安全联盟MSA sdk依赖
    implementation files('libs/miit_mdid_1.0.10.aar')
    implementation 'com.android.support:appcompat-v7:27.0.0'
    //rxJava3.x (如果你集成的其他开源框架已经引用rxJava2.x或rxJava3.x,则不需要再次引用)
    implementation 'io.reactivex.rxjava3:rxjava:3.0.0-RC3'
    //需要依赖的第三方lib

}
android {
    useLibrary 'org.apache.http.legacy'

}
//task to delete the old jar
task deleteOldJar(type: Delete) {
    delete 'release/AndroidPlugin.jar'
}


task makeJar(type:Jar){
    delete 'build/libs/librarysdk.jar'//删除之前的包

    archivesBaseName = "librarysdk"//打包后的jar名
    from(project.zipTree('build/intermediates/bundles/release/classes.jar'))//添加classes
    from(project.zipTree('build/intermediates/bundles/release/libs/okhttp-3.2.0.jar'))//添加三包
    from(project.zipTree('build/intermediates/bundles/release/libs/okio-1.6.0.jar'))
    from(project.zipTree('build/intermediates/bundles/release/libs/android-support-multidex.jar'))
    from(project.zipTree('build/intermediates/bundles/release/libs/glide-3.7.0.jar'))
    from(project.zipTree('build/intermediates/bundles/release/libs/gson-2.8.0.jar'))
    from(project.zipTree('build/intermediates/bundles/release/libs/miit_mdid_1.0.10.aar'))
    destinationDir = file('build/libs')
}
makeJar.dependsOn(build)

// 源代码一起打包
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.sourceFiles
}

artifacts {
    archives androidSourcesJar
}



                                                                                                                                                                              -END

你可能感兴趣的:(Android,android,studio,jar)