How to create an Android Library Jar with gradle without publicly revealing source code?

前端 未结 2 1504
无人共我
无人共我 2020-11-27 11:14

I would like to create a Jar out of an Android library project. It is set up the following way:

ProjectName
    \\- lib
    |   \\- lib
    |       \\- armea         


        
2条回答
  •  一个人的身影
    2020-11-27 12:03

    Note: The answer has been edited. Please see the 07/28/2014 update below.

    Here is a solution I ended up coming up with. There may be a better way available, but I have not found it yet.

    android {
        compileSdkVersion 18
        buildToolsVersion "18.0.1"
    
        defaultConfig {
            minSdkVersion 10
            targetSdkVersion 18
        }
    
        sourceSets {
            main {
                java {
                    srcDir 'src/main/java'
                }
                resources {
                    srcDir 'src/../lib'
                }
            }
        }
    }
    
    task clearJar(type: Delete) {
        delete 'build/libs/ProjectName.jar'
    }
    
    task makeJar(type: Copy) {
        from('build/bundles/release/')
        into('build/libs/')
        include('classes.jar')
        rename ('classes.jar', 'ProjectName.jar')
    }
    
    makeJar.dependsOn(clearJar, build)
    

    Running gradlew makeJar creates a ProjectName.jar in the build/libs directory. The structure of this jar is as follows:

    ProjectName.jar
        \- lib
        |   \- armeabi
        |       \- libNativeFirst.so
        |       \- libNativeSecond.so
        \- com
            \- package
                \- sdk
                    \- PackageSDK.class
    

    This is the exact result I needed. I am now able to use ProjectName.jar successfully in other projects.

    EDIT: While I am able to use the resulting jar in projects within Android Studio, I cannot do so in projects created in ADT due to a warning about native code being present inside a jar file. Supposedly there is a flag to turn off this check in settings, but it does not function correctly. Thus, if you want to create a library that uses native code, those using ADT will have to manually copy the armeabi directory into libs/.

    07/28/2014 Update:

    As of Android Studio 0.8.0, Gradle output directories have been changed and the configuration outlined above will not work. I have changed my configuration to the following:

    task clearJar(type: Delete) {
        delete 'build/outputs/ProjectName.jar'
    }
    
    task makeJar(type: Copy) {
        from('build/intermediates/bundles/release/')
        into('build/outputs/')
        include('classes.jar')
        rename ('classes.jar', 'ProjectName.jar')
    }
    

    IMPORTANT: Please note that ProjectName.jar will now be placed into build/outputs/ and NOT into build/libs/.

提交回复
热议问题