Java – Android, Gradle products flavor ; The package does not exist

Android, Gradle products flavor ; The package does not exist… here is a solution to the problem.

Android, Gradle products flavor ; The package does not exist

I looked everywhere for help on this issue but still couldn’t find anything.

I started building and applying with a new build system. I used two product flavors in this project, free and pro. The gradle.build file is as follows:

   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’ve built the src file structure like this and it works fine :

-Main.
-.java
-Memphis
-weak
-Game
– Java files

-Gratis.
-.java
-Memphis
-weak
-free
– Java files

-Kiss.
-.java
-Memphis
-weak
-kiss
– Java files

I would like to know how to reference a class from the style source file of the main activity. I used the following code in the main java file to try to reference the free package class file.

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

This works when building the free version because it can find packages; But the package reference is not found when building the pro version.

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

Does anyone else know of good practical examples of how to use new build concepts everywhere, since such concepts are not covered outside of Gradle builds?

Solution

As tbruyelle mentioned, the answer to my question was to keep the same package names for the various flavors throughout the build file so they don’t conflict during the build process.

Then change the build package name via the Grade.build file. This will make the deployment of each style on the device unique.

Related Problems and Solutions