ProductFlavours只更改资源

时间:2016-05-04 11:00:36

标签: android android-gradle build.gradle android-productflavors

我有一个完全开发的应用程序。 现在我必须创建相同的应用程序,仅在mipmap中进行最小的更改 文件夹和strings.xml

我已在build.gradle中创建了Product Flavors

    apply plugin: 'com.android.application'
    apply plugin: 'sonar'
    apply plugin: 'sonar-runner'

    android {
        useLibrary 'org.apache.http.legacy'
        compileSdkVersion 23
        buildToolsVersion "23.0.3"

        defaultConfig {
            applicationId "com.example"
            minSdkVersion 16
            targetSdkVersion 23
            versionCode 1
            versionName "16.1.1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        packagingOptions {
            exclude 'META-INF/services/javax.annotation.processing.Processor'
        }
        productFlavors {
            flavour1 {
                buildConfigField "String", "BASE_SERVER_URL", '"http://flavour1.com"'
                buildConfigField "String", "CITY_ID", "1"
            }
            flavour2 {
                buildConfigField "String", "BASE_SERVER_URL", '"http://flavour2.com"'
                buildConfigField "String", "CITY_ID", "2"
            }
        }
    }

    repositories {

    }

    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.3.0'
        compile 'com.android.support:recyclerview-v7:23.3.0'
        compile 'com.android.support:support-v4:23.3.0'
        compile 'com.android.support:cardview-v7:23.3.0'
        compile 'com.android.support:design:23.3.0'
        compile 'com.google.android.gms:play-services:8.4.0'
        compile 'com.squareup.retrofit:retrofit:1.9.0'
        compile 'de.greenrobot:eventbus:2.4.0'
        compile 'de.hdodenhof:circleimageview:2.0.0'
        compile 'com.eftimoff:androidplayer:1.0.3@aar'
        compile 'com.nineoldandroids:library:2.4.0'
        compile 'org.apache.commons:commons-lang3:3.4'
        compile 'com.google.code.gson:gson:2.4'
        compile 'com.viewpagerindicator:library:2.4.1@aar'
        compile 'se.emilsjolander:stickylistheaders:2.5.2'
        compile('org.simpleframework:simple-xml:2.7') {
            exclude module: 'stax'
            exclude module: 'stax-api'
            exclude module: 'xpp3'
        }
    compile files('libs/xmlutils.jar')
    compile 'cc.cloudist.acplibrary:library:1.2.1'
    compile 'com.github.castorflex.smoothprogressbar:library:1.1.0'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile files('libs/json-20140107.jar')
    compile 'uk.co.chrisjenx:calligraphy:2.1.0'
    compile 'de.greenrobot:eventbus:2.4.0'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile('com.mapbox.mapboxsdk:mapbox-android-sdk:4.0.0-rc.1@aar') {
        transitive = true
    }
    compile 'com.github.d-max:spots-dialog:0.4@aar'
    compile files('libs/aws-android-sdk-1.0.4-debug.jar')
}

并将我的文件夹结构修改为

Project
--/app
  --/src
    --/flavour1
      --/res
        --strings.xml
    --/flavour2
      --/res
        --strings.xml
    --/main
      --/java
      --/res
      --/AndroidManifest.xml

然后我得到2个错误

`1`

`error: incompatible types: int cannot be converted to String
    public static final String PAID_DETAILS = "p";
                  ^
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.`

`2`

Execution failed for task ':app:compileflavour1DebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

编辑:

如下所示更改文件夹结构以按预期工作

Project
--/app
  --/src
    --/flavour1
      --/res
        --/values
          --strings.xml
    --/flavour2
      --/res
        --/values               
          --strings.xml
    --/main
      --/java
      --/res
      --/AndroidManifest.xml

1 个答案:

答案 0 :(得分:2)

问题在于:

buildConfigField "String", "CITY_ID", "1"

它将在您的BuildConfig

中创建
public static final String CITY_ID = 1;

这是一个错误。

您必须使用

buildConfigField "String", "CITY_ID", "\"1\""

同样,您必须更改buildConfigField "String", "CITY_ID", "2"