Android的工作室打造的口味 - 如何在不同的口味相同的源文件 [英] Android Studio build flavors - How to have same source files in diverse flavors

查看:155
本文介绍了Android的工作室打造的口味 - 如何在不同的口味相同的源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要建立在Android Studio中的演示香精,用于应用程序。在我的应用程序的gradle级文件我已经创建演示另一个名为风味和全部课程的默认的味道。它看起来是这样的:

I need to create a demo flavor in android studio for an app. In my app level gradle file i have created another flavor called demo and the default flavor of full of course. It looks like this:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.uen229.myapplication"
        minSdkVersion 17
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }


    productFlavors {
        demo {
            applicationId "com.buildsystemexample.app.demo"
            versionName "1.0-demo"
        }
        full {
            applicationId "com.buildsystemexample.app.full"
            versionName "1.0-full"
        }
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
}

和这里是我在其中创建了一个演示味道目录我的项目结构的图片:

and here is a image of my project structure in which i have created a demo flavor directory:

在这里输入的形象描述

现在到这个问题。我有两个类叫Hello.java。两者都是有各自的口味和打印不同的东西。我现在就告诉你这两个文件:

Now onto the issue. I have two classes called Hello.java. Both are in there respective flavors and print different things. i'll show you both files now:

    import android.util.Log;

/** this is from demo flavor directory**/

public class Hello {

    Hello(){

        Log.v("","hello from demo");
    }

    public String getName();
        return "im from demo";

    };

}

和这里的其他您好:

    package com.example.uen229.myapplication;

import android.util.Log;


/** this is from full or main flavor directory**/
public class Hello {


    Hello(){

        Log.v("", "hello from main");
    }

    public String getName(){

        return "im from main";

    };
}

注意如何在第一hello.java没有包装,即使我有一个包中的IDE不会编译。看看这张照片:

notice how the first hello.java does not have package, even if i had a package the IDE wont compile. look at this photo:

在这里输入的形象描述

现在终于让我们看看mainActivity.java地看到,当我切换建立变种它只做了从主要的IM敬​​酒,但我需要它来演示打印IM如果我用demoDebug构建变​​量。如果i开关构建变种demoDebug它仍然打印IM从主谁能帮助:

Now finally lets look at mainActivity.java to see that when i switch build variants it only does a toast for "im from main" but i need it to print 'im from demo" if i use the demoDebug build variant. If i switch the build variant to demoDebug it still prints "im from main". can anyone help :

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Hello h = new Hello();
    Toast.makeText(this, h.getName(), Toast.LENGTH_LONG).show();

}

}

更新

从计算器它说:
如果你想有一个不同版本的同一类中的两味,你需要在这两个口味来创建它。

From stackoverflow it says: If you want to have a different version of the same class in the two flavor you'll need to create it in both flavors.

的src / flavor1 / JAVA / COM /富/ A.java
SRC / flavor2 / JAVA / COM /富/ A.java
然后在你的src code / main / java下可以做

src/flavor1/java/com/foo/A.java src/flavor2/java/com/foo/A.java And then your code in src/main/java can do

进口com.foo.A
取决于所选的香精,用于com.foo.A正确版本

import com.foo.A depending on the flavor selected, the right version of com.foo.A is used.

这是我想与Hello类实现什么

This is what I want to accomplish with the Hello class

推荐答案

我认为你不能在主味相同的类和您的其他味道。你应该建立另一种味道,然后从主味移动你的您好类新的味道。这条规则只为.java文件。我的意思是,你可以在主味的XML文件,并在自定义的味道另一个版本,但你不能用java文件做到这一点。

I think you can't have same class in main flavor and your other flavor. you should just create another flavor, then move your Hello class from main flavor to that new flavor. this rule is just for .java files. I mean you can have an xml file in main flavor and another version in your custom flavor but you can't do this with java files.

<一个href=\"http://stackoverflow.com/questions/16737006/using-build-flavors-structuring-source-folders-and-build-gradle-correctly\">here是进一步的解释有用的链接。

here is a useful link with further explanation.

这篇关于Android的工作室打造的口味 - 如何在不同的口味相同的源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆