2

I want to have a project with 3 submodules as follows:

proj:spring
proj:plainjava
proj:android
proj:android:app

Where plainjava is a jar that is installed in the local repository. spring + android:app are dependant on it.

Currently if I try running gradle from the root directory or import it to the ide's, it results in various errors.

Execution failed for task ':android:app:compileDebugJavaWithJavac'.

Running separate task for android builds fine.

As I have figured out I need the following tasks to execute for the whole build in the following order:

  1. proj:plainjava:publishMavenJavaPublicationToMavenLocal
  2. proj:spring:bootRepackage
  3. ??? Task To build android succesfully, currently builds in android studio explicitly.

For the development purposes I also need to execute each task on it's own. Running

proj:spring:bootRepackage

Somehow triggers dependency resolution for the android, app and plainjava

actual files:

proj settings.gradle

include 'commonobjects', 'server', 'android', 'android:app'

proj build.gradle, has specific custom tasks that allow ci to build artifacts.

task myCleanLibraries {}
myCleanLibraries.dependsOn ':commonobjects:clean'

task myPublishLibraries {}
myPublishLibraries.dependsOn ':commonobjects:publishToMavenLocal'

task myCleanRest {}
myCleanRest.dependsOn ':server:clean', ':android:app:clean'

task myBuildRest {}
myBuildRest.dependsOn ':server:build', ':android:app:assembleDebug'


task myLibraries {}
myLibraries.dependsOn 'myCleanLibraries', 'myPublishLibraries'
myPublishLibraries.mustRunAfter 'myCleanLibraries'

task myRest {}
myRest.dependsOn 'myCleanRest', 'myBuildRest'
myBuildRest.mustRunAfter 'myCleanRest'

task myAll {}
myAll.dependsOn 'myLibraries', 'myRest'
myRest.mustRunAfter 'myLibraries'

proj:spring build.gradle

group 'com.example'
version '0.0.VERSIONNUMBER'

apply plugin: 'java'
apply plugin: 'org.springframework.boot'

defaultTasks 'clean', 'build'

buildscript {
    ext {
        springBootVersion = '1.5.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

jar {
    baseName = 'server'
    version = '0.0.9999'
}

sourceCompatibility = 1.8

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-security'
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'org.springframework.boot:spring-boot-starter-logging'
    compile 'org.projectlombok:lombok'
    compile 'io.jsonwebtoken:jjwt:0.7.0'
    compile 'mysql:mysql-connector-java:6.0.5'
    compile 'com.example:commonobjects:0.0.9999'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
}

proj:commonobjects settings.gradle

rootProject.name = 'commonobjects'

proj:commonobjects build.gradle

group 'com.example'
version '0.0.9999'

apply plugin: 'java'
apply plugin: 'maven-publish'

jar {
    baseName = 'commonobjects'
    version = '0.0.9999'
}


repositories {
    mavenCentral()
}
publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
    repositories {
        mavenLocal()
    }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    testCompile 'junit:junit:4.11'
}

proj:android settings.gradle

include ':app'

proj:android build.gradle

buildscript {
    repositories {
            jcenter()
            mavenCentral()
        }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'
    }
}

proj:android:app build.gradle

def ANDROID_SUPPORT_DEPENDENCY_VERSION = '25.1.0'
def DAGGER_DEPENDENCY_VERSION = '2.10'
def OK_HTTP_DEPENDENCY_VERSION = '3.7.0'
def RETROFIT_DEPENDENCY_VERSION = '2.1.0'
def RETROFIT_JACKSON_DEPENDENCY_VERSION = '2.1.0'
def BUTTER_KNIFE_DEPENDENCY_VERSION = '8.5.1'
def JAVAX_ANNOTATION_JSR250_API_DEPENDENCY_VERSION = '1.0'
def GREEN_ROBOT_EVENT_BUS_DEPENDENCY_VERSION = '3.0.0'
def RX_JAVA_DEPENDENCY_VERSION = '2.0.5'
def RX_ANDROID_JAVA_DEPENDENCY_VERSION = '2.0.1'

defaultTasks 'clean', 'assembleDebug'

buildscript {
    repositories {
        mavenCentral()
        jcenter()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "net.ltgt.gradle:gradle-apt-plugin:0.9"
        classpath "com.android.tools.build:gradle:2.3.1"
    }
}

repositories {
    jcenter()
    mavenLocal()
    mavenCentral()
}

apply plugin: "com.android.application"
apply plugin: "idea"


android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "myandroidappid"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 9999
        versionName "0.0.9999"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {

//        debug {
//            buildConfigField "String", "PARSE_APPLICATION_ID", "\"1\""
//            buildConfigField "String", "PARSE_API_KEY", "\"1\""
//        }

        release {
            minifyEnabled true
            proguardFiles "android-proguard-android.txt", "proguard-rules.txt"

            // buildConfigField "String", "PARSE_APPLICATION_ID", "\"1\""
            // buildConfigField "String", "PARSE_API_KEY", "\"1\""
        }
    }

    packagingOptions {
        pickFirst 'META-INF/LICENSE'
    }
}

dependencies {
    // Add jars supplied
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // Test related
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'

    androidTestCompile 'com.google.code.findbugs:jsr305:3.0.1'
    androidTestCompile "com.android.support:support-annotations:${ANDROID_SUPPORT_DEPENDENCY_VERSION}"
    androidTestCompile "com.android.support.test:runner:0.5"

    // MYOSCA dependencies
     compile('com.example:commonobjects:0.0.9999')

    // Android support libraries
    compile "com.android.support:appcompat-v7:${ANDROID_SUPPORT_DEPENDENCY_VERSION}"
    compile "com.android.support:design:${ANDROID_SUPPORT_DEPENDENCY_VERSION}"
    compile "com.android.support:support-annotations:${ANDROID_SUPPORT_DEPENDENCY_VERSION}"

    // An HTTP & HTTP/2 client for Android and Java applications
    compile "com.squareup.okhttp3:okhttp:${OK_HTTP_DEPENDENCY_VERSION}"

    // Retrofit: A type-safe HTTP client for Android and Java
    compile "com.squareup.retrofit2:retrofit:${RETROFIT_DEPENDENCY_VERSION}"
    compile "com.squareup.retrofit2:converter-jackson:${RETROFIT_JACKSON_DEPENDENCY_VERSION}"

    // Butterknife: Field and method binding for Android views
    compile "com.jakewharton:butterknife:${BUTTER_KNIFE_DEPENDENCY_VERSION}"
    annotationProcessor "com.jakewharton:butterknife-compiler:${BUTTER_KNIFE_DEPENDENCY_VERSION}"

    // Dagger DI
    compile "com.google.dagger:dagger:${DAGGER_DEPENDENCY_VERSION}"
    annotationProcessor "com.google.dagger:dagger-compiler:${DAGGER_DEPENDENCY_VERSION}"
    compile "com.google.dagger:dagger-android:${DAGGER_DEPENDENCY_VERSION}"
    annotationProcessor "com.google.dagger:dagger-android-processor:${DAGGER_DEPENDENCY_VERSION}"
    compile "com.google.dagger:dagger-android-support:${DAGGER_DEPENDENCY_VERSION}"

    compile "com.google.dagger:dagger:${DAGGER_DEPENDENCY_VERSION}"
    annotationProcessor "com.google.dagger:dagger-compiler:${DAGGER_DEPENDENCY_VERSION}"
    provided "javax.annotation:jsr250-api:${JAVAX_ANNOTATION_JSR250_API_DEPENDENCY_VERSION}"

    // Event bus
    compile "org.greenrobot:eventbus:${GREEN_ROBOT_EVENT_BUS_DEPENDENCY_VERSION}"

    // RxJava a library for composing asynchronous and event-based programs by using observable sequences.
    compile "io.reactivex.rxjava2:rxjava:${RX_JAVA_DEPENDENCY_VERSION}"
    compile "io.reactivex.rxjava2:rxandroid:${RX_ANDROID_JAVA_DEPENDENCY_VERSION}"

    // Google Guava for preconditions
    compile 'com.google.guava:guava:20.0'
}
Bato-Bair Tsyrenov
  • 1,174
  • 3
  • 17
  • 40

1 Answers1

1

Full solution available at: repo

Thanks for posting your build.gradle files. I believe your `settings.gradle is where you should start.

Since you want/have these modules in your current setup:

proj:spring
proj:plainjava
proj:android
proj:android:app

Let's map them to gradle modules:

proj/
  |-spring/
     |-build.gradle
  |-plain-java/        <-- better naming conventions by separating names
     |-build.gradle
  |-android/
     |-build.gradle
  |-build.gradle       <-- make sure to have root build.gradle
  |-settings.gradle    <-- should be root folder(proj)

Lets do the following:

  • rename the folder "plainjava" to "plain-java"
  • lets merge the "android/app" folder into just an "android"
  • this let's us consolidate everything down to just 3 projects

New settings.gradle:

rootProject.name = "proj"

include ":spring"
include ":plain-java"
include ":android"

Last thing, in your root directory, make move your buildscript dependencies in there so you can build from that directory.

Bato-Bair Tsyrenov
  • 1,174
  • 3
  • 17
  • 40
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • @Bato-BairTsyrenov Let me know when you have refactored your project like this. You should be good to go. Let me know if I can help further. – Jared Burrows Apr 22 '17 at 17:00
  • What about Android project itself, I am missing knowledge about how it works but I don't think it is possible to merge android:app and android into one – Bato-Bair Tsyrenov Apr 22 '17 at 17:07
  • Am I right? If you look at this answer http://stackoverflow.com/a/23241888/3571652. I have actually missused gradle then since I got confused by the two level android project set up. To put it the easy way, if i have started with android project i should then have created sub projects of android? If this wording makes sense – Bato-Bair Tsyrenov Apr 22 '17 at 17:11
  • @Bato-BairTsyrenov Its just 2 separate folders. Look here: https://github.com/jaredsburrows/android-gif-example. Merge both of those folders, remove the `settings.gradle` from the "android/" folder. – Jared Burrows Apr 22 '17 at 17:13
  • what about local.properties inside the android folder? https://gist.github.com/anonymous/87f434b6fff646fb928f9b85e360d948 – Bato-Bair Tsyrenov Apr 22 '17 at 17:15
  • That shouldn't exist as it should be automatically generated. – Jared Burrows Apr 22 '17 at 17:15
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142353/discussion-between-bato-bair-tsyrenov-and-jared-burrows). – Bato-Bair Tsyrenov Apr 22 '17 at 17:16