Android Studio生成Jar包

app 模块(application应用)生成 jar 包

  • build 文件

1、apply plugin: 'com.android.application'----> apply plugin: 'com.android.library'
2、去掉applicationId "com.kingoit.yzt"
3、创建打包方法task makeJar(type: Copy)

makeJar只是一个方法名称可以随意取名,但是这几个地方的方法名必须一致。
1、方法名.dependsOn(build)
2、cmd打包命令:gradlew 方法名称

//apply plugin: 'com.android.application'
apply plugin: 'com.android.library'

android {
    compileSdkVersion 27
    buildToolsVersion "27.0.3"
    defaultConfig {
//        applicationId "com.kingoit.yzt"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        ndk {
            abiFilters 'armeabi','armeabi-v7a'//, 'arm64-v8a', 'x86', 'x86_64'
        }
        externalNativeBuild {
            cmake {
                arguments '-DANDROID_STL=stlport_static'
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path 'CMakeLists.txt'
        }
    }
    packagingOptions {
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

    lintOptions {
        abortOnError false
    }

    task makeJar(type: Copy) {
        //删除存在的
        delete 'build/libs/shapelibjava.jar'
        //设置拷贝的文件
        from('build/intermediates/bundles/debug/')
        //打进jar包后的文件目录
        into('build/libs/')
        //将classes.jar放入build/libs/目录下
        //include ,exclude参数来设置过滤
        //(我们只关心classes.jar这个文件)
        include('classes.jar')
        //重命名
        rename ('classes.jar', 'shapelibjava.jar')
    }

    makeJar.dependsOn(build)

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:27.1.1'
    testCompile 'junit:junit:4.12'
    compile 'com.esri.arcgis.android:arcgis-android:10.2.9'
}

  • 打包
    在Android studio Terminal 中使用gradlew 方法名进行打包

module 生成 jar 包

1、在module目录下的build文件中创建打包方法task makeJar(type: Copy)

apply plugin: 'com.android.library'

android {
    compileSdkVersion 26
    

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

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

    task makeJar(type: Copy) {
        //删除存在的
        delete 'build/libs/shapelibjava.jar'
        //设置拷贝的文件
        from('build/intermediates/bundles/debug/')
        //打进jar包后的文件目录
        into('build/libs/')
        //将classes.jar放入build/libs/目录下
        //include ,exclude参数来设置过滤
        //(我们只关心classes.jar这个文件)
        include('classes.jar')
        //重命名
        rename ('classes.jar', 'shapelibjava.jar')
    }

    makeJar.dependsOn(build)

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:26.1.0'
    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 'com.esri.arcgis.android:arcgis-android:10.2.9'
}

2、在Terminal 中使用gradlew 方法名进行打包

推荐阅读更多精彩内容