Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: Both Java and Android Subprojects on Same Project

TL;DR: IS the only way to develop both pure Java and Android applications, is on completely different Gradle projects?

I am developing a project which includes an Android application and a Java backend (along with some other common APIs).
As an IDE I am using IntelliJ Idea.
I would like to have a single Gradle project (which will be opened on a single Idea instance), that contains all the applications as subprojects.

My problem is that in order to allow the Android plugin, I need to set it in the buildScript section in build.gradle file:

dependencies {
    classpath 'com.android.tools.build:gradle:2.2.3'
}

This forces the android plugin in the entire Gradle build process, and causes errors on the pure Java subprojects (they are automatically set to be built with the Android SDK instead of the Java SDK).

A workaround I have thought of is creating two separate projects (one for Android and one for Java), export the common JARs into a local Maven repository and import it from there into the Android project.

Is there a better solution that will allow me to have all the code base in the same place?

like image 492
Guy Yafe Avatar asked Nov 20 '22 05:11

Guy Yafe


1 Answers

What you are asking for is possible: you just need to move the buildscript declaration from the project to the android module.

The PROJECT build.gradle may look like this:

buildscript {
    // nothing for the project
}

allprojects {
    repositories {
        jcenter() // you could also move this to each module
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

And the Android MODULE build.gradle may look like this:

apply plugin: 'com.android.application'
// other pluguins

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'
        // other dependencies for the build (apt, retrolambda...)
    }
}

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "my.android.app"
        // additional config...
    }
}

dependencies {
    // your app dependencies here
}

Other modules, such as backend or web, may declare their own buildscripts with their own pluguins, repositories and dependencies.

like image 97
ESala Avatar answered May 31 '23 18:05

ESala