如何以编程方式创建Android Test Project?

时间:2012-04-16 19:48:06

标签: android testing project

我尝试过这样的事情:

public boolean createTestProject(String projectName, IProject testedProject, int apiLevel){
        NewProjectWizardState mValues = new NewProjectWizardState(Mode.TEST);
        mValues.projectName = projectName;
        if(testedProject != null){
            mValues.testedProject = testedProject;
            mValues.testingSelf = false;
            mValues.useExisting = false;
        }
        else{
            mValues.testingSelf = true;
            mValues.useExisting = false;
        }
        mValues.minSdk = "1.5";
        mValues.minSdkModifiedByUser = true;
        mValues.useDefaultLocation = true;
        mValues.projectLocationModifiedByUser = false;
        mValues.createActivity = false;
        mValues.activityName = "sf";
        mValues.applicationName = "sf";
        mValues.packageName = "sf";
        NewProjectCreator creator = new NewProjectCreator(mValues, getContainer());

        return creator.createAndroidProjects();
    }

但它在com.android.ide.eclipse.adt.internal.wizards.newproject.NewProjectCreator.createEclipseProject(NewProjectCreator.java:561)中抛出NullPointerException。我已检查过该文件(可能是旧版本),此行为空。我错过了什么?

编辑: 好的,我解决了这个问题。我错过了一些事情。现在它正在工作,它甚至可以创建android项目(而不是测试):

public IProject createProject(String projectName, IProject testedProject,
    boolean testMode, int apiLevel, IWizardContainer container,
    Type type, String packName) throws Exception {
    NewProjectWizardState mValues;
    if (testMode) {
        mValues = new NewProjectWizardState(Mode.TEST);
    } else {
        mValues = new NewProjectWizardState(Mode.ANY);
    }
    mValues.projectName = projectName;
    mValues.projectNameModifiedByUser = true;
    mValues.applicationName = capitalize(mValues.projectName);
    mValues.testApplicationName = projectName;
    mValues.createActivity = false;
    mValues.testedProject = testedProject;
    mValues.minSdk = apiLevel;
    mValues.updateSdkTargetToMatchMinSdkVersion();
    String value = Platform.getLocation().append(mValues.projectName)
            .toString();
    value = TextProcessor.process(value);
    mValues.projectLocation = new File(value);
    mValues.packageName = "com.example.android.apis.test";
    if (testedProject != null) {
        mValues.testProjectName = projectName;
        mValues.packageName = String.format("%1$s.test",
            Helper.getPackageName(testedProject));
        mValues.createPairProject = false;
        mValues.testTargetPackageName = Helper
            .getPackageName(testedProject);
    }
    if (testedProject == null && testMode) {
        mValues.createPairProject = false;
        mValues.testProjectName = projectName;
        mValues.testTargetPackageName = "com.example.android.apis";
    }
    if (packName != null && packName.length > 0) {
        mValues.packageName = packName;
    }
    NewProjectCreator creator = new NewProjectCreator(mValues,
            container);

    creator.createAndroidProjects();
    return ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
}

0 个答案:

没有答案