本文介绍了Android Studio 构建风格 - 如何拥有不同风格的相同源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 android studio 中为应用创建一个演示风格.在我的应用程序级 gradle 文件中,我创建了另一种名为 demo 的风格,当然还有默认的 full 风格.它看起来像这样:

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 from main"做一个祝酒词,但如果我使用 demoDebug 构建变体,我需要它打印im from demo".如果我将构建变体切换到 demoDebug 它仍然打印im from main".任何人都可以帮忙:

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();

    }
}

更新

从stackoverflow它说:

UPDATE

From stackoverflow it says:

如果你想在两个相同的类中有不同的版本您需要以两种口味创建它.

src/flavor1/java/com/foo/A.java

src/flavor2/java/com/foo/A.java

然后你在 src/main/java 中的代码可以做:

And then your code in src/main/java can do:

import com.foo.A

根据选择的风格,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

推荐答案

我认为主口味和其他口味不能有相同的类.您应该创建另一种风味,然后将 Hello 类从主要风味移动到新风味.此规则仅适用于 .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.

这里是一个有用的链接进一步解释.

here is a useful link with further explanation.

这篇关于Android Studio 构建风格 - 如何拥有不同风格的相同源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 15:54