-->

Product flavour [Migrating from eclipse to android

2019-05-30 13:15发布

问题:

I have migrated my app from eclipse to android studio, and now I was attempting to run product flavours. I have been following the blog at http://blog.robustastudio.com/mobile-development/android/building-multiple-editions-of-android-app-gradle/

Following is my build structure when imported from eclipse.

I am able to see the variants in the build variants window.

Following is the build.gradle

apply plugin: 'com.android.application'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':volley')
    compile 'com.google.android.gms:play-services-gcm:7.5.0'
    compile 'com.android.support:cardview-v7:21.0.+'
    compile 'com.android.support:recyclerview-v7:21.0.+'
    compile 'com.android.support:appcompat-v7:21.0.+'
}

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.0"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }

    defaultConfig {
        multiDexEnabled true
    }

    buildTypes {

        release {
            minifyEnabled true;
            proguardFiles getDefaultProguardFile('proguard-project.txt');
        }
    }

    productFlavors {

        demo {
            applicationId "com.demo.app"
        }

    }

    lintOptions {
        checkReleaseBuilds false;
        abortOnError false;
    }
}

I want the Constants.java file to be available for different flavours as it has different values for the respective flavours.

Following is the build structure as seen in general studio projects

app/
|--libs/
|--src/
   |--vanilla/
   |  |--java/
   |     |--com/example/
   |        |--Flavor.java
   |--strawberry/
   |  |--java/
   |     |--com/example/
   |        |--Flavor.java
   |--main/
      |--java/
      |  |--...
      |--res/
      |  |--...
      |--AndroidManifest.xml

But the structure when migrated from eclipse seems to be totally different. I tried to make a directory in src [demo/com.demo.app]to check the flavour variant. But its not working. Can anyone guide me on this ? How do i go about including product flavour support for projects migrated from eclipse

回答1:

When you import a project from Eclipse, Android Studio keeps the same folder structure so your source doesn't move around too much.

This means that you won't be able to use the default source locations. This is why your build.gradle has the sourceSets closure:

sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
}

This block sets up the location for all of your default code (the main configuration).

If you don't want to change your project structure, you can add another block within sourceSets for your flavor. The following example will allow you to put all the code for your demo flavor under demo/src:

sourceSets {
        main {}
        demo {
            manifest.srcFile 'demo/AndroidManifest.xml'
            java.srcDirs = ['demo/src']
            resources.srcDirs = ['demo/src']
            aidl.srcDirs = ['demo/src']
            renderscript.srcDirs = ['demo/src']
            res.srcDirs = ['demo/res']
            assets.srcDirs = ['demo/assets']
        }
}

The other alternative is to remove the sourceSets closure completely and update your project structure to match the standard structure for Gradle-backed project.