Pregunta

I have searched high and low for help on this issue and still can't find anything.

I am starting to build and application using the new build system. I am using two product flavors in this project, free and pro. The gradle.build file is as:

   buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.5.+'
        }
    }
    apply plugin: 'android'

    repositories {
        mavenCentral()
    }

    android {
        compileSdkVersion 17
        buildToolsVersion "17.0.0"

        defaultConfig {
            minSdkVersion 7
            targetSdkVersion 17
        }

        productFlavors {
            free {
                packageName "com.example.free"
                buildConfig "public static final Boolean versionPro = true;"
            }

            pro {
                packageName "com.example.pro"
                buildConfig "public static final Boolean versionPro = false;"
            }
        }
    }

    dependencies {
        compile 'com.android.support:support-v4:13.0.+'
    }

I have built the src file structure like so, which works fine:

-main -java -memphic -dodger -game -java file

-free -java -memphic -dodger -free -java file

-pro -java -memphic -dodger -pro -java file

I would like to know how to reference a class from a flavor source file from the main activity. I have used the below code in the main java file to try reference the free package class file.

com.example.free.ClassFile.addObject();

This works when building the free version as it can find the package; however when building the pro version it can't find the package reference.

Is there a way I can reference in the class file, to another package dynamically? So when compiling the pro and free version the package can be found dynamically. I can't think of a way to do it.

Also does anyone know anywhere that the is good practical examples of how to use the new build concepts as concepts like this are not covered outside of the Gradle build?

¿Fue útil?

Solución

Answer to my question as tbruyelle mentioned, is to keep the same package name throughout the build files for the flavours so they don't conflict during build.

Then change the build package name through the Grade.build file. This will keep each flavour unique for deployment on devices.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top