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

前端 未结 2 1503
无人共我
无人共我 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 11:54

    Just to add a slight alternative to @BVB's answer (although heavily based on it) here's what I had to do to output a jar myapp-api.jar which was for a Java only project that dealt with rest API interaction. It's dependant on Android.jar hence the need to use apply plugin: 'com.android.application' rather than just apply plugin: 'java'

    Calling ./gradlew build jar from the myJavaAPIProject to build and generate the .jar to myJavaAPIProject/build/libs/myapp-api.jar

    build.gradle

    //Even though this is a Java project, we need to apply the android plugin otherwise it cannot find the SDK/android.jar and so cannot compile
    apply plugin: 'com.android.application'
    
    dependencies {
        //this ensures we have gson.jar and anything else in the /lib folder
        compile fileTree(dir: 'lib', include: '*.jar')
    }
    
    repositories {
        mavenCentral()
    }
    android{
        compileSdkVersion 21
        buildToolsVersion "21.0.1"
    
        defaultConfig {
            minSdkVersion 10
            targetSdkVersion 21
        }
    
        sourceSets {
            main {
                java {
                    //points to an empty manifest, needed just to get the build to work
                    manifest.srcFile 'AndroidManifest.xml'
                    //defined our src dir as it's not the default dir gradle looks for
                    java.srcDirs = ['src']
    
                }
            }
        }
    
        //enforce java 7
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7
        }
    }
    
    //Actually created the .jar file
    task jar(type: Jar) {
        //from android.sourceSets.main.java
        from 'build/intermediates/classes/release/'
        archiveName 'myapp-api.jar'
    }
    

    AndroidManifest.xml

    
    
    
    

提交回复
热议问题