How to make Gradle execute a simple makefile thing

my situation is this: I have a project that uses some generated code. In the generated code, a specific URI is hard-coded in almost all files.

So at some point I got to two generated codebases: one targeted DEVELOPMENT, another targeting STAGING.

I want to solve this via Gradle in the same way as via Makefiles.

So first I created a new module in the root of my project named SOAP-DEV and SOAP-STAGE, defined two new variables in gradle.properties called

# Make sure only one of these is set to 'true' and *do* set the other one to 'false'. Please.
USE_STAGING_SOAP=
USE_DEVELOPMENT_SOAP=1

      

I ended up using 1 and empty because true

both false

didn't seem to do the trick and both were considered true.

So once the building problem was done and only one of them seemed to be building (and in fact, but I was only getting one line using <dev/stage> soap pack

, I think it was ok and that the correct one was built.

then I hit another roadblock - it seems like my main project module - app

can't see files outside its folder, which makes sense.

Now I'm trying a different approach - so that SOAP-STAGE and SOAP-DEV are subfolders ProjectName/app

instead ProjectName

, so the #import line can actually see the sources that need to be seen in order to build correctly.

But that doesn't happen: /

What is the correct way to set up a custom selection of which build folder for only a portion of the project codebase, but so they can see the #import project in the classes?

I would happily go back to the old idea of ​​having both SOAP folders and modules in my project, if I could get the classes inside the project to see the classes of the related modules, namely the soap classes.

To better illustrate what I am trying to do, here is my main gradle.build file. I hope you can understand what I am trying to do with this.

import java.text.SimpleDateFormat

apply plugin: 'com.android.application'

android {
    signingConfigs {
        hockeyApp {
            keyAlias 'myKeyAlias'
            keyPassword 'myKeyAliasHA'
            storeFile file('../myKeystore.jks')
            storePassword 'myKeyAliasHA'
        }
    }
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
    defaultConfig {
        applicationId "com.my.application"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName getVersion()
    }
    buildTypes {
        hockeyApp {
            signingConfig signingConfigs.hockeyApp
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    android.enforceUniquePackageName=false
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'com.facebook.android:facebook-android-sdk:4.0.0'
    compile 'com.google.android.gms:play-services:7.0.0'
    compile 'com.android.support:recyclerview-v7:21.0.0'
    compile 'com.daimajia.swipelayout:library:1.1.9@aar'
    compile files('libs/dpdsoaplib.jar')
    compile project (':androidNeoReaderSDK')

    if(USE_STAGING_SOAP == 1)
    {
        compile {
            source = 'SOAP-STAGE/src/main/java'
        }
    }
    if(USE_DEVELOPMENT_SOAP == 1)
    {
        compile {
            source = 'SOAP-DEV/src/main/java'
        }
    }
    compile "com.android.support:support-v4:21.0.+"
    compile 'com.google.maps.android:android-maps-utils:0.3+'
}

def getVersion() {
    def Calendar cal = Calendar.getInstance();
    def SimpleDateFormat dateFormat = new SimpleDateFormat("MMddHHmmss");

    def StringBuilder sb = new StringBuilder("v");
    sb.append("0.2");
    sb.append(".");
    sb.append(dateFormat.format(cal.getTimeInMillis()));
    sb.append("a");

    return sb.toString();

}

      

However, if I can actually get both of these folders (SOAP-DEV and SOAP-STAGE) to be added as source folders to the project / module, I think this will solve the problem of not being able to import the appropriate classes, which was only caused by moving the classes from the ProjectName / app / src / java folder to the ProjectName / SOAP-DEV / src / java and ProjectName / SOAP-STAGE / src / java folders.

The two folders have the same package names and contain classes with the same name that only point to a different soap server.

So folks, any ideas? I thought this Gradle was supposed to introduce some build / makefile functionality to android studio, but this is not the case yet.

Right now I'm trying something in between - have both folders as modules in the project root, but also have one or the other code visible in my module import statements app

.

+3


source to share


2 answers


So the answer (at the end) got to this:

The structure was slightly changed and two new subfolders were added at the main module (main module = application) level.

ProjectRoot/app/src/main/my/package/name/...
ProjectRoot/app/src/soap_dev/my/package/name/...
ProjectRoot/app/src/soap_stage/my/package/name/...

      

Then the generated code moved from

ProjectRoot/app/src/main/my/package/name/backend/soap/generated

      



to

ProjectRoot/app/src/SOAP_DEV_/my/package/name/backend/soap/generated
ProjectRoot/app/src/SOAP_STAGE_/my/package/name/backend/soap/generated

      

Two new build styles have been added to the application module

   productFlavors {
       SOAP_DEV_ {
       }
       SOAP_STAGE_ {
       }
    }

      

And this has led to the tastes being applicable to all current build configurations available for the project.

0


source


Your initial idea of ​​having two modules in the root directory from both versions of the generated code is a good start. Now, using a variable to dynamically change the content of your application is not supported by gradle.

The philosophy behind this limitation is that it SHOULD be able to assess the need for a gradle task by simply looking at the inputs and outputs. (i.e. the input hasn't changed ==> no need to rerun the task).

Fortunately, gradle offers alternative ways to write flexible assemblies:

You can use flavors (i.e. variations of the same product).



Define 2 flavors:

android {
    ...
    productFlavors {
       dev{}
       staging{}
    }
}


dependencies {
    devCompile project(':SOAP-DEV')
    stagingCompile project(':SOAP-STAGE')
    ...
}

      

Note that the prefix in the dependencies is exactly the name of the fragrances.

This will create 2 apks: MyApp-dev.apk and MyApp-staging.apk

+1


source







All Articles