Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have separate debug and release versions of gradle project that pull in different versions of the same class?

I have two versions of the same Java class (same name / methods). Since it's Java, both .java files have the same name. I want to configure gradle in such a way that I can build a "debug" version of my application that pulls in one of these files, and a "production" version of my application that pulls in the other one. How would I go about doing this?

This class has only static methods. I don't ever want to make an instance of this class. I additionally don't want to add the overhead of an if statement in each of the methods on this class to check which version I'm in.

like image 266
John Avatar asked Jul 23 '20 17:07

John


People also ask

Can I have multiple build gradle?

Hello , can i create multiple build. gradle for one project? Yes. You can have multiple build files in one project.

How do I specify the latest version of gradle?

You can specify the Gradle version in either the File > Project Structure > Project menu in Android Studio, or update your Gradle version using the command line.

What is Dependencymanagement in gradle?

In most cases, a project relies on reusable functionality in the form of libraries or is broken up into individual components to compose a modularized system. Dependency management is a technique for declaring, resolving and using dependencies required by the project in an automated fashion.


4 Answers

Following @JFabianMeier's answer you could use 4 projects:

  1. with the production version class
  2. with the debug version class
  3. with code that uses either of the two, parameterized according to Migrating Maven profiles ... → Example 6. Mimicking the behavior of Maven profiles in Gradle. (I'm also a Maven guy and therefore can't tell you exactly how to do it in Gradle.)
  4. a multi-project with 1./2./3. as sub[-]projects for building all of them in one go, maybe parameterized to build just 1.+ 3. or 2.+ 3.
like image 93
Gerold Broser Avatar answered Oct 18 '22 07:10

Gerold Broser


Have you tried creating production and debug source directories/sets? According to the docs you can use multiple directories when specifying source directories in your source set. Try dropping the different versions of your class in their respective production/debug source directories.

I haven't tested myself (not sure about the syntax), but this is based on the Gradle's Java compilation documentation.

sourceSets {
    // Could also name this production
    main {
        java {
            srcDirs ['src/main/java', 'src/prod/java']
        }
    }
    debug {
        java {
            srcDirs ['src/main/java', 'src/debug/java']
        }
    }
}
like image 20
Paul T. Avatar answered Oct 18 '22 05:10

Paul T.


You could do the following:

  • Put the class into a separate project (so generate a separate jar from it)
  • Then you can have two different jars, for production and debugging
  • Then you can pull either one or the other jar in gradle depending on a parameter.

Alternatively, you could look into template engines like Velocity which allow you to generate source code during the build depending on variables and then compile it.

like image 28
J Fabian Meier Avatar answered Oct 18 '22 06:10

J Fabian Meier


Android has a neat feature called Product Flavors. It lets you swap classes at compile time effortlessly and keep your project clean.

This post is very good to get a taste of it: https://android-developers.googleblog.com/2015/12/leveraging-product-flavors-in-android.html

And here is the full documentation: https://developer.android.com/studio/build/build-variants#product-flavors

like image 1
Eddie Lopez Avatar answered Oct 18 '22 05:10

Eddie Lopez