/** * Generates an empty manifest for a rule that does not directly specify resources. * * <p><strong>Note:</strong> This generated manifest can then be used as the primary manifest when * merging with dependencies. * * @return the generated ApplicationManifest */ public static ApplicationManifest generatedManifest(RuleContext ruleContext) { Artifact generatedManifest = ruleContext.getUniqueDirectoryArtifact( ruleContext.getRule().getName() + "_generated", new PathFragment("AndroidManifest.xml"), ruleContext.getBinOrGenfilesDirectory()); String manifestPackage; if (ruleContext.attributes().isAttributeValueExplicitlySpecified("custom_package")) { manifestPackage = ruleContext.attributes().get("custom_package", Type.STRING); } else { manifestPackage = JavaUtil.getJavaFullClassname(ruleContext.getRule().getPackage().getNameFragment()); } String contents = Joiner.on("\n") .join( "<?xml version=\"1.0\" encoding=\"utf-8\"?>", "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"", " package=\"" + manifestPackage + "\">", " <application>", " </application>", "</manifest>"); ruleContext .getAnalysisEnvironment() .registerAction( new FileWriteAction( ruleContext.getActionOwner(), generatedManifest, contents, false /* makeExecutable */)); return new ApplicationManifest(generatedManifest); }
public static String getJavaPackage(RuleContext ruleContext) { if (ruleContext.attributes().isAttributeValueExplicitlySpecified("custom_package")) { return ruleContext.attributes().get("custom_package", Type.STRING); } else { PathFragment nameFragment = ruleContext.getRule().getPackage().getNameFragment(); String packageName = JavaUtil.getJavaFullClassname(nameFragment); if (packageName != null) { return packageName; } else { // This is a workaround for libraries that don't follow the standard Bazel package format return nameFragment.getPathString().replace('/', '.'); } } }