com.android.builder.model.Variant Java Examples

The following examples show how to use com.android.builder.model.Variant. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LintGradleProject.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a {@link LintGradleProject} from
 * the given {@link com.android.builder.model.AndroidProject} definition for
 * a given {@link com.android.builder.model.Variant}, and returns it along with
 * a set of lint custom rule jars applicable for the given model project.
 *
 * @param client        the client
 * @param project       the model project
 * @param variant       the variant
 * @param gradleProject the gradle project
 * @return a pair of new project and list of custom rule jars
 */
@NonNull
public static Pair<LintGradleProject, List<File>> create(
        @NonNull LintGradleClient client,
        @NonNull AndroidProject project,
        @NonNull Variant variant,
        @NonNull org.gradle.api.Project gradleProject) {
    File dir = gradleProject.getProjectDir();
    AppGradleProject lintProject = new AppGradleProject(client, dir,
            dir, project, variant);

    List<File> customRules = Lists.newArrayList();
    File appLintJar = new File(gradleProject.getBuildDir(),
            "lint" + separatorChar + "lint.jar");
    if (appLintJar.exists()) {
        customRules.add(appLintJar);
    }

    Set<AndroidLibrary> libraries = Sets.newHashSet();
    Dependencies dependencies = variant.getMainArtifact().getDependencies();
    for (AndroidLibrary library : dependencies.getLibraries()) {
        lintProject.addDirectLibrary(createLibrary(client, library, libraries, customRules));
    }

    return Pair.<LintGradleProject, List<File>>of(lintProject, customRules);
}
 
Example #2
Source File: RTools.java    From NBANDROID-V2 with Apache License 2.0 6 votes vote down vote up
public static  PluginVersionResult handlePluginVersion(AndroidProject androidProject,Variant variant, FileObject prjDir) {
    if (androidProject!=null && variant!=null) {
        File buildFolder = androidProject.getBuildFolder();
        String modelVersion = androidProject.getModelVersion();
        PluginVersion pluginVersion = pluginVersionFromString(modelVersion);
        if (pluginVersion.compareTo(new PluginVersion(3, 3, 0)) >= 0) {
            String variantName = variant.getName();
            String rFolderPath = buildFolder.getAbsolutePath() + File.separator + "generated" + File.separator + "not_namespaced_r_class_sources" + File.separator + variantName + File.separator + "process" + StringUtils.capitalize(variantName) + "Resources" + File.separator + "r";
            File rFolder = new File(rFolderPath);
            if (rFolder.exists() && rFolder.isDirectory()) {
                FileObject src = FileUtil.toFileObject(rFolder);
                String srcName = null;
                if (prjDir != null) {
                    srcName = FileUtil.isParentOf(prjDir, src)
                            ? FileUtil.getRelativePath(prjDir, src)
                            : rFolder.getAbsolutePath();
                } else {
                    srcName = rFolder.getAbsolutePath();
                }
                return new PluginVersionResult(src, srcName);
            }
        }
    }
    return null;
}
 
Example #3
Source File: LintGradleRequest.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
private static Variant findVariant(@NonNull AndroidProject project,
                                   @Nullable String variantName) {
    if (variantName != null) {
        for (Variant variant : project.getVariants()) {
            if (variantName.equals(variant.getName())) {
                return variant;
            }
        }
    }

    if (!project.getVariants().isEmpty()) {
        return project.getVariants().iterator().next();
    }

    return null;
}
 
Example #4
Source File: LintGradleRequest.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Nullable
@Override
public Collection<Project> getProjects() {
    if (mProjects == null) {
        Variant variant = findVariant(mModelProject, mVariantName);
        if (variant == null) {
            mProjects = Collections.emptyList();
            return mProjects;
        }
        Pair<LintGradleProject, List<File>> result = LintGradleProject.create(
                mLintClient, mModelProject, variant, mGradleProject);
        mProjects = Collections.<Project>singletonList(result.getFirst());
        mLintClient.setCustomRules(result.getSecond());
    }

    return mProjects;
}
 
Example #5
Source File: TranslationDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Nullable
private static List<String> getResConfigLanguages(@NonNull Project project) {
    if (project.isGradleProject() && project.getGradleProjectModel() != null &&
            project.getCurrentVariant() != null) {
        Set<String> relevantDensities = Sets.newHashSet();
        Variant variant = project.getCurrentVariant();
        List<String> variantFlavors = variant.getProductFlavors();
        AndroidProject gradleProjectModel = project.getGradleProjectModel();

        addResConfigsFromFlavor(relevantDensities, null,
                project.getGradleProjectModel().getDefaultConfig());
        for (ProductFlavorContainer container : gradleProjectModel.getProductFlavors()) {
            addResConfigsFromFlavor(relevantDensities, variantFlavors, container);
        }
        if (!relevantDensities.isEmpty()) {
            ArrayList<String> strings = Lists.newArrayList(relevantDensities);
            Collections.sort(strings);
            return strings;
        }
    }

    return null;
}
 
Example #6
Source File: ResourceVisibilityLookup.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Looks up a (possibly cached) {@link ResourceVisibilityLookup} for the given {@link
 * AndroidArtifact}
 *
 * @param project the project
 * @return the corresponding {@link ResourceVisibilityLookup}
 */
@NonNull
public ResourceVisibilityLookup get(
        @NonNull AndroidProject project,
        @NonNull Variant variant) {
    ResourceVisibilityLookup visibility = mInstances.get(variant);
    if (visibility == null) {
        if (isVisibilityAwareModel(project)) {
            AndroidArtifact artifact = variant.getMainArtifact();
            visibility = get(artifact);
        } else {
            visibility = NONE;
        }
        mInstances.put(variant, visibility);
    }
    return visibility;
}
 
Example #7
Source File: AndroidClassPathProvider.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
private ClassPath createExecute(ClassPath compile) {
    if (androidProjectModel != null) {
        Variant variant = buildConfig.getCurrentVariant();
        if (variant != null) {
            return ClassPathSupport.createProxyClassPath(compile, ClassPathSupport.createClassPath(
                    FileUtil.urlForArchiveOrDir(FileUtil.normalizeFile(variant.getMainArtifact().getClassesFolder()))));
        }
    }
    return compile;
}
 
Example #8
Source File: AndroidSupport.java    From meghanada-server with GNU General Public License v3.0 5 votes vote down vote up
private void parseExtraJavaArtifacts(Set<ProjectDependency> dependencies, Variant variant) {
  String buildType = variant.getBuildType();
  boolean debugBuild = buildType.equals(DEBUG_BUILD);
  Collection<JavaArtifact> extraJavaArtifacts = variant.getExtraJavaArtifacts();
  for (JavaArtifact javaArtifact : extraJavaArtifacts) {
    if (debugBuild) {
      Collection<File> generatedSourceFolders = javaArtifact.getGeneratedSourceFolders();
      for (File src : generatedSourceFolders) {
        this.project.getSources().add(src);
      }
    }

    Dependencies compileDependencies = javaArtifact.getDependencies();
    Collection<AndroidLibrary> libraries = compileDependencies.getLibraries();
    for (AndroidLibrary androidLibrary : libraries) {
      Collection<File> localJars = androidLibrary.getLocalJars();
      for (File jar : localJars) {
        addDependencies(dependencies, jar);
      }
    }
    Collection<JavaLibrary> javaLibraries = compileDependencies.getJavaLibraries();
    for (JavaLibrary javaLibrary : javaLibraries) {
      File file = javaLibrary.getJarFile();
      addDependencies(dependencies, file);
    }
  }
}
 
Example #9
Source File: AndroidBuildVariants.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
@Nullable
public static Variant findVariantByName(Iterable<Variant> variants, final String name) {
    return variants == null || name == null
            ? null
            : Iterables.find(
                    variants,
                    new Predicate<Variant>() {

                @Override
                public boolean apply(Variant t) {
                    return name.equals(t.getName());
                }
            },
                    null);
}
 
Example #10
Source File: BuildVariant.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
public BuildTypeContainer getCurrentBuildTypeContainer() {
    synchronized (lock) {
        Variant v = getCurrentVariant();
        return v != null && androidProjectModel != null
                ? BuildTypes.findBuildTypeByName(androidProjectModel.getBuildTypes(), v.getBuildType())
                : null;
    }
}
 
Example #11
Source File: BuildVariant.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
public Iterable<String> getAllVariantNames() {
    synchronized (lock) {
        return androidProjectModel != null
                ? Iterables.transform(
                        androidProjectModel.getVariants(),
                        new Function<Variant, String>() {
                    @Override
                    public String apply(Variant f) {
                        return f.getName();
                    }
                })
                : Collections.<String>emptyList();
    }
}
 
Example #12
Source File: BuildVariant.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
@Nullable
public String getVariantName() {
    synchronized (lock) {
        if (androidProjectModel == null) {
            return null;
        }
        if (AndroidBuildVariants.findVariantByName(androidProjectModel.getVariants(), variant) != null) {
            return variant;
        }
        // first fallback is 'debug'
        if (AndroidBuildVariants.findVariantByName(androidProjectModel.getVariants(), "debug") != null) {
            return "debug";
        }
        // or use first flavored debug variant
        List<String> debugVariants = Lists.newArrayList(
                Iterables.filter(
                        Iterables.transform(
                                androidProjectModel.getVariants(),
                                new Function<Variant, String>() {
                            @Override
                            public String apply(Variant f) {
                                return f.getName();
                            }
                        }),
                        new Predicate<String>() {

                    @Override
                    public boolean apply(String t) {
                        return t.endsWith("debug") || t.endsWith("Debug");
                    }
                }));
        Collections.sort(debugVariants);
        return debugVariants.isEmpty() ? null : debugVariants.get(0);
    }
}
 
Example #13
Source File: AndroidClassPathProvider.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<? extends File> get() {
    Variant variant = buildConfig.getCurrentVariant();
    if (variant != null) {
        AndroidArtifact testArtifact = AndroidBuildVariants.instrumentTestArtifact(variant.getExtraAndroidArtifacts());
        Iterable<File> testCompileCPEntries
                = Collections.<File>singleton(variant.getMainArtifact().getClassesFolder());
        if (testArtifact != null) {
            List<File> javaLibs = new ArrayList<>();
            for (JavaLibrary lib : testArtifact.getDependencies().getJavaLibraries()) {
                collectJavaLibraries(javaLibs, lib);
            }
            testCompileCPEntries = Iterables.concat(
                    testCompileCPEntries,
                    Iterables.transform(
                            testArtifact.getDependencies().getLibraries(),
                            new Function<AndroidLibrary, File>() {
                        @Override
                        public File apply(AndroidLibrary f) {
                            return f.getJarFile();
                        }
                    }),
                    javaLibs);
        }
        return testCompileCPEntries;
    }
    return Collections.<File>emptyList();
}
 
Example #14
Source File: GradleSourceForBinaryQuery.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
private List<FileObject> update() {
    Result r = findAarLibraryRoots(binaryRoot);
    if (r != null) {
        return Collections.EMPTY_LIST;
    }
    if (androidProjectModel == null) {
        return Collections.EMPTY_LIST;
    }
    final File binRootDir = FileUtil.archiveOrDirForURL(binaryRoot);
    if (binRootDir == null) {
        return null;
    }

    Variant variant = Iterables.find(
            androidProjectModel.getVariants(),
            new Predicate<Variant>() {
        @Override
        public boolean apply(Variant input) {
            return binRootDir.equals(input.getMainArtifact().getClassesFolder());
        }
    },
            null);
    if (variant != null) {
        Iterable<FileObject> srcRoots = Iterables.filter(
                Iterables.transform(
                        sourceRootsForVariant(variant),
                        new Function<File, FileObject>() {
                    @Override
                    public FileObject apply(File f) {
                        return FileUtil.toFileObject(f);
                    }
                }),
                Predicates.notNull());
        return Lists.newArrayList(srcRoots);
    }
    return Collections.EMPTY_LIST;
}
 
Example #15
Source File: GradleSourceForBinaryQuery.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
private Result findAarLibraryRoots(final URL binaryRoot) {
        // FileUtil.getArchiveFile(binaryRoot);
        AndroidLibrary aLib = null;
        Variant variant = buildConfig.getCurrentVariant();
        if (variant != null) {
            aLib = Iterables.find(
                    variant.getMainArtifact().getDependencies().getLibraries(),
                    new Predicate<AndroidLibrary>() {

                @Override
                public boolean apply(AndroidLibrary lib) {
                    URL libUrl = FileUtil.urlForArchiveOrDir(FileUtil.normalizeFile(lib.getJarFile()));
                    return binaryRoot.equals(libUrl);
                }
            },
                    null);
        }
        if (aLib == null) {
            return null;
        }
//    if (aLib instanceof AndroidLibraryProject) {
//      AndroidLibraryProject libPrj = (AndroidLibraryProject) aLib;
//      LOG.log(Level.FINE, "Found binary from AndroidLibrary {0}", libPrj.getProjectPath());
//    } else {
        LOG.log(Level.FINE, "Found unknown binary from AndroidLibrary {0}", aLib.getJarFile());
//    }
        return null;
    }
 
Example #16
Source File: Project.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a shared {@link ResourceVisibilityLookup}
 *
 * @return a shared provider for looking up resource visibility
 */
@NonNull
public ResourceVisibilityLookup getResourceVisibility() {
    if (mResourceVisibility == null) {
        if (isGradleProject()) {
            AndroidProject project = getGradleProjectModel();
            Variant variant = getCurrentVariant();
            if (project != null && variant != null) {
                mResourceVisibility = mClient.getResourceVisibilityProvider().get(project,
                        variant);

            } else if (getGradleLibraryModel() != null) {
                try {
                    mResourceVisibility = mClient.getResourceVisibilityProvider()
                            .get(getGradleLibraryModel());
                } catch (Exception ignore) {
                    // Handle talking to older Gradle plugins (where we don't
                    // have access to the model version to check up front
                }
            }
        }
        if (mResourceVisibility == null) {
            mResourceVisibility = ResourceVisibilityLookup.NONE;
        }
    }

    return mResourceVisibility;
}
 
Example #17
Source File: LintGradleProject.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private AppGradleProject(
        @NonNull LintGradleClient client,
        @NonNull File dir,
        @NonNull File referenceDir,
        @NonNull AndroidProject project,
        @NonNull Variant variant) {
    //TODO FIXME: handle multi-apk
    super(client, dir, referenceDir,
            variant.getMainArtifact().getOutputs().iterator().next().getGeneratedManifest());

    mProject = project;
    mVariant = variant;
}
 
Example #18
Source File: BuildVariant.java    From NBANDROID-V2 with Apache License 2.0 4 votes vote down vote up
public Variant getCurrentVariant() {
    synchronized (lock) {
        return androidProjectModel != null ? AndroidBuildVariants.findVariantByName(androidProjectModel.getVariants(), getVariantName()) : null;
    }
}
 
Example #19
Source File: Project.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Returns the set of applicable densities for this project. If null, there are no density
 * restrictions and all densities apply.
 *
 * @return the list of specific densities that apply in this project, or null if all densities
 * apply
 */
@Nullable
public List<String> getApplicableDensities() {
    if (mCachedApplicableDensities == null) {
        // Use the gradle API to set up relevant densities. For example, if the
        // build.gradle file contains this:
        // android {
        //     defaultConfig {
        //         resConfigs "nodpi", "hdpi"
        //     }
        // }
        // ...then we should only enforce hdpi densities, not all these others!
        if (isGradleProject() && getGradleProjectModel() != null &&
                getCurrentVariant() != null) {
            Set<String> relevantDensities = Sets.newHashSet();
            Variant variant = getCurrentVariant();
            List<String> variantFlavors = variant.getProductFlavors();
            AndroidProject gradleProjectModel = getGradleProjectModel();

            addResConfigsFromFlavor(relevantDensities, null,
                    getGradleProjectModel().getDefaultConfig());
            for (ProductFlavorContainer container : gradleProjectModel.getProductFlavors()) {
                addResConfigsFromFlavor(relevantDensities, variantFlavors, container);
            }

            // Are there any splits that specify densities?
            if (relevantDensities.isEmpty()) {
                AndroidArtifact mainArtifact = variant.getMainArtifact();
                Collection<AndroidArtifactOutput> outputs = mainArtifact.getOutputs();
                for (AndroidArtifactOutput output : outputs) {
                    for (OutputFile file : output.getOutputs()) {
                        final String DENSITY_NAME = OutputFile.FilterType.DENSITY.name();
                        if (file.getFilterTypes().contains(DENSITY_NAME)) {
                            for (FilterData data : file.getFilters()) {
                                if (DENSITY_NAME.equals(data.getFilterType())) {
                                    relevantDensities.add(data.getIdentifier());
                                }
                            }
                        }
                    }
                }
            }

            if (!relevantDensities.isEmpty()) {
                mCachedApplicableDensities = Lists.newArrayListWithExpectedSize(10);
                for (String density : relevantDensities) {
                    String folder = ResourceFolderType.DRAWABLE.getName() + '-' + density;
                    mCachedApplicableDensities.add(folder);
                }
                Collections.sort(mCachedApplicableDensities);
            } else {
                mCachedApplicableDensities = Collections.emptyList();
            }
        } else {
            mCachedApplicableDensities = Collections.emptyList();
        }
    }

    return mCachedApplicableDensities.isEmpty() ? null : mCachedApplicableDensities;
}
 
Example #20
Source File: AndroidBuildVariants.java    From NBANDROID-V2 with Apache License 2.0 4 votes vote down vote up
@Nullable
public static Variant findDebugVariant(Iterable<Variant> variants) {
    return findVariantByName(variants, "debug");
}
 
Example #21
Source File: FileWizardIterator.java    From NBANDROID-V2 with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(WizardDescriptor wizard) {
    this.wizard = wizard;
    String displayName = (String) wizard.getProperty("NewFileWizard_Title");
    type = Type.find(displayName);
    index = 0;
    panels = createPanels();
    // Make sure list of steps is accurate.
    steps = createSteps();
    wizard.putProperty(WizardDescriptor.PROP_CONTENT_SELECTED_INDEX, 0);
    for (int i = 0; i < panels.size(); i++) {
        Component c = panels.get(i).getComponent();
        if (c instanceof JComponent) { // assume Swing components
            JComponent jc = (JComponent) c;
            // Step #.
            // TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_*:
            jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i));
            // Step name (actually the whole list for reference).
            jc.putClientProperty("WizardPanel_contentData", steps.toArray(new String[steps.size()]));
        }
    }
    NewFileWizard fileWizard = (NewFileWizard) wizard;
    try {
        Project project = (Project) wizard.getProperty("project");
        if (AndroidProjects.isAndroidMavenProject(project) && (project instanceof NbAndroidProjectImpl)) {
            NbAndroidProjectImpl gradleProject = (NbAndroidProjectImpl) project;
            File projectDirectory = gradleProject.getProjectDirectoryAsFile();
            DataFolder targetFolder = fileWizard.getTargetFolder();
            String targetPath = targetFolder.getPrimaryFile().getPath();
            BuildVariant buildVariant = project.getLookup().lookup(BuildVariant.class);
            AndroidSources androidSources = project.getLookup().lookup(AndroidSources.class);
            SourceGroup[] sourceGroups = androidSources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
            FileObject srcRootFolder = null;
            for (SourceGroup sourceGroup : sourceGroups) {
                if (sourceGroup.contains(targetFolder.getPrimaryFile())) {
                    srcRootFolder = sourceGroup.getRootFolder();
                }
            }
            sourceGroups = androidSources.getSourceGroups(AndroidConstants.ANDROID_MANIFEST_XML);
            FileObject manifestRootFolder = null;
            if (sourceGroups.length == 1) {
                manifestRootFolder = sourceGroups[0].getRootFolder();
            }
            wizard.putProperty(MANIFEST_ROOT_FOLDER, FileUtil.toFile(manifestRootFolder));
            Variant variant = buildVariant.getCurrentVariant();
            wizard.putProperty(BUILD_VARIANT, variant);
            if (srcRootFolder != null) {
                String packageName = targetPath.replace(srcRootFolder.getPath(), "");
                packageName = packageName.replace("/", ".").replace("\\", ".");
                packageName = packageName.substring(1);
                wizard.putProperty(PROP_PROJECT_PACKAGE, packageName);
            }
            AndroidPlatformInfo projectPlatform = AndroidProjects.projectPlatform(project);
            if (projectPlatform != null) {
                AndroidSdk sdk = projectPlatform.getSdk();
                wizard.putProperty(PROP_PROJECT_SDK, sdk);
                wizard.putProperty(PROP_PLATFORM, new PlatformDecorator(projectPlatform));
            }
            Project rootProject = AndroidProjects.findRootProject(project);
            if (rootProject != null) {
                wizard.putProperty(AndroidProjectTemplatePanelVisualBasicSettings.PROP_PROJECT_DIR, FileUtil.toFile(rootProject.getProjectDirectory()));
                String folderName = project.getProjectDirectory().getPath().replace(rootProject.getProjectDirectory().getPath(), "").substring(1);
                if (!folderName.contains("/") && !folderName.contains("\\")) {
                    wizard.putProperty(PROP_SUB_PROJECT_FOLDER, folderName);
                }
            }
            AndroidProject aPrj = project.getLookup().lookup(AndroidProject.class);
            if (aPrj != null) {
                String buildToolsVersion = aPrj.getBuildToolsVersion();
                wizard.putProperty(BUILD_TOOL_VERSION, buildToolsVersion);
            }
            wizard.putProperty(ERROR_NO_ANDROID, false);
        } else {
            wizard.putProperty(ERROR_NO_ANDROID, true);
        }

    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example #22
Source File: MobileActivityWizardIterator.java    From NBANDROID-V2 with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(WizardDescriptor wizard) {
    this.wizard = wizard;
    index = 0;
    panels = createPanels();
    // Make sure list of steps is accurate.
    steps = createSteps();
    wizard.putProperty(WizardDescriptor.PROP_CONTENT_SELECTED_INDEX, 0);
    for (int i = 0; i < panels.size(); i++) {
        Component c = panels.get(i).getComponent();
        if (c instanceof JComponent) { // assume Swing components
            JComponent jc = (JComponent) c;
            // Step #.
            // TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_*:
            jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i));
            // Step name (actually the whole list for reference).
            jc.putClientProperty("WizardPanel_contentData", steps.toArray(new String[steps.size()]));
        }
    }
    NewFileWizard fileWizard = (NewFileWizard) wizard;
    try {
        Project project = (Project) wizard.getProperty("project");
        if (AndroidProjects.isAndroidMavenProject(project) && (project instanceof NbAndroidProjectImpl)) {
            NbAndroidProjectImpl gradleProject = (NbAndroidProjectImpl) project;
            File projectDirectory = gradleProject.getProjectDirectoryAsFile();
            DataFolder targetFolder = fileWizard.getTargetFolder();
            String targetPath = targetFolder.getPrimaryFile().getPath();
            BuildVariant buildVariant = project.getLookup().lookup(BuildVariant.class);
            AndroidSources androidSources = project.getLookup().lookup(AndroidSources.class);
            SourceGroup[] sourceGroups = androidSources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
            FileObject srcRootFolder = null;
            for (SourceGroup sourceGroup : sourceGroups) {
                if (sourceGroup.contains(targetFolder.getPrimaryFile())) {
                    srcRootFolder = sourceGroup.getRootFolder();
                }
            }
            sourceGroups = androidSources.getSourceGroups(AndroidConstants.ANDROID_MANIFEST_XML);
            FileObject manifestRootFolder = null;
            if (sourceGroups.length == 1) {
                manifestRootFolder = sourceGroups[0].getRootFolder();
            }
            wizard.putProperty(MANIFEST_ROOT_FOLDER, FileUtil.toFile(manifestRootFolder));
            Variant variant = buildVariant.getCurrentVariant();
            wizard.putProperty(BUILD_VARIANT, variant);
            if (srcRootFolder != null) {
                try {
                    String packageName = targetPath.replace(srcRootFolder.getPath(), "");
                    packageName = packageName.replace("/", ".").replace("\\", ".");
                    packageName = packageName.substring(1);
                    wizard.putProperty(PROP_PROJECT_PACKAGE, packageName);
                } catch (Exception e) {
                }
            }
            PlatformDecorator projectPlatform = new PlatformDecorator(AndroidProjects.projectPlatform(project));
            if (projectPlatform != null) {
                AndroidSdk sdk = projectPlatform.getSdk();
                wizard.putProperty(PROP_PROJECT_SDK, sdk);
                wizard.putProperty(PROP_PHONE_TABLET_PLATFORM, projectPlatform);
                wizard.putProperty(PROP_PHONE_TABLET_ENABLED, true);
            }
            Project rootProject = AndroidProjects.findRootProject(project);
            if (rootProject != null) {
                wizard.putProperty(AndroidProjectTemplatePanelVisualBasicSettings.PROP_PROJECT_DIR, FileUtil.toFile(rootProject.getProjectDirectory()));
                String folderName = project.getProjectDirectory().getPath().replace(rootProject.getProjectDirectory().getPath(), "").substring(1);
                if (!folderName.contains("/") && !folderName.contains("\\")) {
                    wizard.putProperty(PROP_PHONE_TABLET_FOLDER, folderName);
                }
            }
            AndroidProject aPrj = project.getLookup().lookup(AndroidProject.class);
            if (aPrj != null) {
                String buildToolsVersion = aPrj.getBuildToolsVersion();
                wizard.putProperty(BUILD_TOOL_VERSION, buildToolsVersion);
            }
            wizard.putProperty(ERROR_NO_ANDROID, false);
        } else {
            wizard.putProperty(ERROR_NO_ANDROID, true);
        }

    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example #23
Source File: ProjectVisualSummaryPanel.java    From NBANDROID-V2 with Apache License 2.0 4 votes vote down vote up
void read(WizardDescriptor wizardDescriptor) {
    Object property = wizardDescriptor.getProperty(AndroidProjectTemplatePanelVisualBasicSettings.PROP_PROJECT_DIR);
    if (property instanceof File) {
        rootProjectPath.setText(((File) property).getAbsolutePath());
    }
    property = wizardDescriptor.getProperty(AndroidProjectTemplatePanelVisualBasicSettings.PROP_PROJECT_PACKAGE);
    if (property instanceof String) {
        packageName.setText(((String) property));
    }
    property = wizardDescriptor.getProperty(MANIFEST_ROOT_FOLDER);
    if (property instanceof File) {
        manifestFolder.setText(((File) property).getAbsolutePath());
    }
    property = wizardDescriptor.getProperty(PROP_PROJECT_SDK);
    if (property instanceof AndroidSdk) {
        androidSdk.setText(((AndroidSdk) property).getDisplayName());
        androidSdk.setToolTipText(((AndroidSdk) property).getSdkPath());
    }
    switch (type) {
        case MOBILE:
            property = wizardDescriptor.getProperty(PROP_PHONE_TABLET_PLATFORM);
            break;
        case WEAR:
            property = wizardDescriptor.getProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_WEAR_PLATFORM);
            break;
        case TV:
            property = wizardDescriptor.getProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_TV_PLATFORM);
            break;
        case FILE:
            property = wizardDescriptor.getProperty(PROP_PLATFORM);

            break;
        default:
            throw new AssertionError(type.name());
    }
    if (property instanceof PlatformDecorator) {
        platform.setText(SdkVersionInfo.getCodeName(((PlatformDecorator) property).getAndroidVersion().getApiLevel()) + " " + SdkVersionInfo.getVersionString(((PlatformDecorator) property).getAndroidVersion().getApiLevel()));
        platform.setToolTipText(((PlatformDecorator) property).getAndroidVersion().getApiString());
    }
    switch (type) {
        case MOBILE:
            property = wizardDescriptor.getProperty(PROP_PHONE_TABLET_FOLDER);
            break;
        case WEAR:
            property = wizardDescriptor.getProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_WEAR_FOLDER);
            break;
        case TV:
            property = wizardDescriptor.getProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_TV_FOLDER);
            break;
        case FILE:
            property = wizardDescriptor.getProperty(PROP_SUB_PROJECT_FOLDER);

            break;
        default:
            throw new AssertionError(type.name());
    }
    if (property instanceof String) {
        projectFolder.setText((String) property);
    }
    property = wizardDescriptor.getProperty(MobileActivityWizardIterator.BUILD_VARIANT);
    if (property instanceof Variant) {
        Variant variant = (Variant) property;
        ApiVersion minSdk = variant.getMergedFlavor().getMinSdkVersion();
        minSdkVersion.setText(SdkVersionInfo.getCodeName(minSdk.getApiLevel()) + " " + SdkVersionInfo.getVersionString(minSdk.getApiLevel()));
        minSdkVersion.setToolTipText(minSdk.getApiString());
        ApiVersion targetSdk = variant.getMergedFlavor().getTargetSdkVersion();
        targetSdkVersion.setText(SdkVersionInfo.getCodeName(targetSdk.getApiLevel()) + " " + SdkVersionInfo.getVersionString(targetSdk.getApiLevel()));
        targetSdkVersion.setToolTipText(targetSdk.getApiString());
        wizardDescriptor.putProperty(PROP_MAX_BUILD_LEVEL, targetSdk.getApiLevel());
        switch (type) {
            case MOBILE:
                wizardDescriptor.putProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_PHONE_TABLET_ENABLED, true);
                wizardDescriptor.putProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_WEAR_ENABLED, false);
                wizardDescriptor.putProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_TV_ENABLED, false);
                break;
            case WEAR:
                wizardDescriptor.putProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_PHONE_TABLET_ENABLED, false);
                wizardDescriptor.putProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_WEAR_ENABLED, true);
                wizardDescriptor.putProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_TV_ENABLED, false);
                break;
            case TV:
                wizardDescriptor.putProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_PHONE_TABLET_ENABLED, false);
                wizardDescriptor.putProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_WEAR_ENABLED, false);
                wizardDescriptor.putProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_TV_ENABLED, true);
                break;
            case FILE:
                break;
            default:
                throw new AssertionError(type.name());

        }

    }
    property = wizardDescriptor.getProperty(BUILD_TOOL_VERSION);
    if (property instanceof String) {
        buildToolVersion.setText(((String) property));
    }
}
 
Example #24
Source File: ProjectVisualSummaryPanel.java    From NBANDROID-V2 with Apache License 2.0 4 votes vote down vote up
boolean valid(WizardDescriptor wizardDescriptor) {
    Object property = wizardDescriptor.getProperty(ERROR_NO_ANDROID);
    if ((property instanceof Boolean) && ((Boolean) property)) {
        wizardDescriptor.putProperty("WizardPanel_errorMessage",
                "Unsupported project type.");
        return false;
    }
    property = wizardDescriptor.getProperty(AndroidProjectTemplatePanelVisualBasicSettings.PROP_PROJECT_PACKAGE);
    if (!(property instanceof String) || ((String) property).isEmpty()) {
        wizardDescriptor.putProperty("WizardPanel_errorMessage",
                "Bad package name.");
        return false;
    }
    property = wizardDescriptor.getProperty(MANIFEST_ROOT_FOLDER);
    if (!(property instanceof File)) {
        wizardDescriptor.putProperty("WizardPanel_errorMessage",
                "Android manifest was not found.");
        return false;
    }
    property = wizardDescriptor.getProperty(PROP_PROJECT_SDK);
    if (!(property instanceof AndroidSdk)) {
        wizardDescriptor.putProperty("WizardPanel_errorMessage",
                "Android SDK was not found.");
        return false;
    }
    switch (type) {
        case MOBILE:
            property = wizardDescriptor.getProperty(PROP_PHONE_TABLET_PLATFORM);
            break;
        case WEAR:
            property = wizardDescriptor.getProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_WEAR_PLATFORM);
            break;
        case TV:
            property = wizardDescriptor.getProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_TV_PLATFORM);
            break;
        case FILE:
            property = wizardDescriptor.getProperty(PROP_PLATFORM);

            break;
        default:
            throw new AssertionError(type.name());
    }
    if (!(property instanceof PlatformDecorator)) {
        wizardDescriptor.putProperty("WizardPanel_errorMessage",
                "Android Platform was not found.");
        return false;
    }
    property = wizardDescriptor.getProperty(MobileActivityWizardIterator.BUILD_VARIANT);
    if (!(property instanceof Variant)) {
        wizardDescriptor.putProperty("WizardPanel_errorMessage",
                "Android Build variant was not found.");
        return false;
    }
    switch (type) {
        case MOBILE:
            property = wizardDescriptor.getProperty(PROP_PHONE_TABLET_FOLDER);
            break;
        case WEAR:
            property = wizardDescriptor.getProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_WEAR_FOLDER);
            break;
        case TV:
            property = wizardDescriptor.getProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_TV_FOLDER);
            break;
        case FILE:
            property = wizardDescriptor.getProperty(PROP_SUB_PROJECT_FOLDER);

            break;
        default:
            throw new AssertionError(type.name());
    }
    if (!(property instanceof String) || ((String) property).isEmpty()) {
        wizardDescriptor.putProperty("WizardPanel_errorMessage",
                "Unsupported project structure.");
        return false;
    }
    return true;
}
 
Example #25
Source File: WearActivityWizardIterator.java    From NBANDROID-V2 with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(WizardDescriptor wizard) {
    this.wizard = wizard;
    index = 0;
    panels = createPanels();
    // Make sure list of steps is accurate.
    steps = createSteps();
    wizard.putProperty(WizardDescriptor.PROP_CONTENT_SELECTED_INDEX, 0);
    for (int i = 0; i < panels.size(); i++) {
        Component c = panels.get(i).getComponent();
        if (c instanceof JComponent) { // assume Swing components
            JComponent jc = (JComponent) c;
            // Step #.
            // TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_*:
            jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i));
            // Step name (actually the whole list for reference).
            jc.putClientProperty("WizardPanel_contentData", steps.toArray(new String[steps.size()]));
        }
    }
    NewFileWizard fileWizard = (NewFileWizard) wizard;
    try {
        Project project = (Project) wizard.getProperty("project");
        if (AndroidProjects.isAndroidMavenProject(project) && (project instanceof NbAndroidProjectImpl)) {
            NbAndroidProjectImpl gradleProject = (NbAndroidProjectImpl) project;
            File projectDirectory = gradleProject.getProjectDirectoryAsFile();
            DataFolder targetFolder = fileWizard.getTargetFolder();
            String targetPath = targetFolder.getPrimaryFile().getPath();
            BuildVariant buildVariant = project.getLookup().lookup(BuildVariant.class);
            AndroidSources androidSources = project.getLookup().lookup(AndroidSources.class);
            SourceGroup[] sourceGroups = androidSources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
            FileObject srcRootFolder = null;
            for (SourceGroup sourceGroup : sourceGroups) {
                if (sourceGroup.contains(targetFolder.getPrimaryFile())) {
                    srcRootFolder = sourceGroup.getRootFolder();
                }
            }
            sourceGroups = androidSources.getSourceGroups(AndroidConstants.ANDROID_MANIFEST_XML);
            FileObject manifestRootFolder = null;
            if (sourceGroups.length == 1) {
                manifestRootFolder = sourceGroups[0].getRootFolder();
            }
            wizard.putProperty(MANIFEST_ROOT_FOLDER, FileUtil.toFile(manifestRootFolder));
            Variant variant = buildVariant.getCurrentVariant();
            wizard.putProperty(BUILD_VARIANT, variant);
            if (srcRootFolder != null) {
                String packageName = targetPath.replace(srcRootFolder.getPath(), "");
                packageName = packageName.replace("/", ".").replace("\\", ".");
                packageName = packageName.substring(1);
                wizard.putProperty(PROP_PROJECT_PACKAGE, packageName);
            }
            AndroidPlatformInfo projectPlatform = AndroidProjects.projectPlatform(project);
            if (projectPlatform != null) {
                AndroidSdk sdk = projectPlatform.getSdk();
                wizard.putProperty(PROP_PROJECT_SDK, sdk);
                wizard.putProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_WEAR_PLATFORM, new PlatformDecorator(projectPlatform));
                wizard.putProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_WEAR_ENABLED, true);
            }
            Project rootProject = AndroidProjects.findRootProject(project);
            if (rootProject != null) {
                wizard.putProperty(AndroidProjectTemplatePanelVisualBasicSettings.PROP_PROJECT_DIR, FileUtil.toFile(rootProject.getProjectDirectory()));
                String folderName = project.getProjectDirectory().getPath().replace(rootProject.getProjectDirectory().getPath(), "").substring(1);
                if (!folderName.contains("/") && !folderName.contains("\\")) {
                    wizard.putProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_WEAR_FOLDER, folderName);
                }
            }
            AndroidProject aPrj = project.getLookup().lookup(AndroidProject.class);
            if (aPrj != null) {
                String buildToolsVersion = aPrj.getBuildToolsVersion();
                wizard.putProperty(BUILD_TOOL_VERSION, buildToolsVersion);
            }
            wizard.putProperty(ERROR_NO_ANDROID, false);
        } else {
            wizard.putProperty(ERROR_NO_ANDROID, true);
        }

    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example #26
Source File: TvActivityWizardIterator.java    From NBANDROID-V2 with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(WizardDescriptor wizard) {
    this.wizard = wizard;
    index = 0;
    panels = createPanels();
    // Make sure list of steps is accurate.
    steps = createSteps();
    wizard.putProperty(WizardDescriptor.PROP_CONTENT_SELECTED_INDEX, 0);
    for (int i = 0; i < panels.size(); i++) {
        Component c = panels.get(i).getComponent();
        if (c instanceof JComponent) { // assume Swing components
            JComponent jc = (JComponent) c;
            // Step #.
            // TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_*:
            jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i));
            // Step name (actually the whole list for reference).
            jc.putClientProperty("WizardPanel_contentData", steps.toArray(new String[steps.size()]));
        }
    }
    NewFileWizard fileWizard = (NewFileWizard) wizard;
    try {
        Project project = (Project) wizard.getProperty("project");
        if (AndroidProjects.isAndroidMavenProject(project) && (project instanceof NbAndroidProjectImpl)) {
            NbAndroidProjectImpl gradleProject = (NbAndroidProjectImpl) project;
            File projectDirectory = gradleProject.getProjectDirectoryAsFile();
            DataFolder targetFolder = fileWizard.getTargetFolder();
            String targetPath = targetFolder.getPrimaryFile().getPath();
            BuildVariant buildVariant = project.getLookup().lookup(BuildVariant.class);
            AndroidSources androidSources = project.getLookup().lookup(AndroidSources.class);
            SourceGroup[] sourceGroups = androidSources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
            FileObject srcRootFolder = null;
            for (SourceGroup sourceGroup : sourceGroups) {
                if (sourceGroup.contains(targetFolder.getPrimaryFile())) {
                    srcRootFolder = sourceGroup.getRootFolder();
                }
            }
            sourceGroups = androidSources.getSourceGroups(AndroidConstants.ANDROID_MANIFEST_XML);
            FileObject manifestRootFolder = null;
            if (sourceGroups.length == 1) {
                manifestRootFolder = sourceGroups[0].getRootFolder();
            }
            wizard.putProperty(MANIFEST_ROOT_FOLDER, FileUtil.toFile(manifestRootFolder));
            Variant variant = buildVariant.getCurrentVariant();
            wizard.putProperty(BUILD_VARIANT, variant);
            if (srcRootFolder != null) {
                String packageName = targetPath.replace(srcRootFolder.getPath(), "");
                packageName = packageName.replace("/", ".").replace("\\", ".");
                packageName = packageName.substring(1);
                wizard.putProperty(PROP_PROJECT_PACKAGE, packageName);
            }
            AndroidPlatformInfo projectPlatform = AndroidProjects.projectPlatform(project);
            if (projectPlatform != null) {
                AndroidSdk sdk = projectPlatform.getSdk();
                wizard.putProperty(PROP_PROJECT_SDK, sdk);
                wizard.putProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_TV_PLATFORM, new PlatformDecorator(projectPlatform));
                wizard.putProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_TV_ENABLED, true);
            }
            Project rootProject = AndroidProjects.findRootProject(project);
            if (rootProject != null) {
                wizard.putProperty(AndroidProjectTemplatePanelVisualBasicSettings.PROP_PROJECT_DIR, FileUtil.toFile(rootProject.getProjectDirectory()));
                String folderName = project.getProjectDirectory().getPath().replace(rootProject.getProjectDirectory().getPath(), "").substring(1);
                if (!folderName.contains("/") && !folderName.contains("\\")) {
                    wizard.putProperty(AndroidProjectTemplatePanelVisualAndroidSettings.PROP_TV_FOLDER, folderName);
                }
            }
            AndroidProject aPrj = project.getLookup().lookup(AndroidProject.class);
            if (aPrj != null) {
                String buildToolsVersion = aPrj.getBuildToolsVersion();
                wizard.putProperty(BUILD_TOOL_VERSION, buildToolsVersion);
            }
            wizard.putProperty(ERROR_NO_ANDROID, false);
        } else {
            wizard.putProperty(ERROR_NO_ANDROID, true);
        }

    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example #27
Source File: GradleSourceForBinaryQuery.java    From NBANDROID-V2 with Apache License 2.0 4 votes vote down vote up
private Iterable<? extends File> sourceRootsForVariant(Variant variant) {
    Collection<File> javaDirs = androidProjectModel != null
            ? androidProjectModel.getDefaultConfig().getSourceProvider().getJavaDirectories()
            : Collections.<File>emptySet();
    BuildTypeContainer buildTypeContainer = buildConfig.getCurrentBuildTypeContainer();
    Collection<File> typeJavaDirs = buildTypeContainer != null
            ? buildTypeContainer.getSourceProvider().getJavaDirectories()
            : Collections.<File>emptySet();
    Iterable<File> variantJavaDirs = variant != null
            ? Iterables.concat(
                    Iterables.transform(
                            variant.getProductFlavors(),
                            new Function<String, Collection<File>>() {
                        @Override
                        public Collection<File> apply(String f) {
                            if (androidProjectModel == null) {
                                return Collections.<File>emptySet();
                            }
                            final ProductFlavorContainer flavor
                                    = ProductFlavors.findFlavorByName(androidProjectModel.getProductFlavors(), f);
                            if (flavor == null) {
                                return Collections.<File>emptySet();
                            }
                            return flavor.getSourceProvider().getJavaDirectories();
                        }
                    }))
            : Collections.<File>emptySet();
    Collection<File> generatedJavaDirs = variant != null
            ? variant.getMainArtifact().getGeneratedSourceFolders()
            : Collections.<File>emptyList();
    if (variant!=null) {
        RTools.PluginVersionResult result = RTools.handlePluginVersion(androidProjectModel, variant, null);
        if(result!=null){
            generatedJavaDirs.add(FileUtil.toFile(result.getSrc()));
        }
    }
    return Iterables.concat(
            javaDirs,
            typeJavaDirs,
            variantJavaDirs,
            generatedJavaDirs);
}
 
Example #28
Source File: LintGradleProject.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Variant getCurrentVariant() {
    return mVariant;
}
 
Example #29
Source File: LintGradleProject.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Variant getCurrentVariant() {
    return null;
}
 
Example #30
Source File: DefaultAndroidProject.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
@NonNull
public Collection<Variant> getVariants() {
    return variants;
}