Home | History | Annotate | Download | only in java
      1 // Copyright 2015 Google Inc. All rights reserved.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package java
     16 
     17 // This file generates the final rules for compiling all Java.  All properties related to
     18 // compiling should have been translated into javaBuilderFlags or another argument to the Transform*
     19 // functions.
     20 
     21 import (
     22 	"strings"
     23 
     24 	"github.com/google/blueprint"
     25 
     26 	"android/soong/android"
     27 )
     28 
     29 var (
     30 	aaptCreateResourceJavaFile = pctx.AndroidStaticRule("aaptCreateResourceJavaFile",
     31 		blueprint.RuleParams{
     32 			Command: `rm -rf "$javaDir" && mkdir -p "$javaDir" && ` +
     33 				`$aaptCmd package -m $aaptFlags -P $publicResourcesFile -G $proguardOptionsFile ` +
     34 				`-J $javaDir || ( rm -rf "$javaDir/*"; exit 41 ) && ` +
     35 				`find $javaDir -name "*.java" > $javaFileList`,
     36 			CommandDeps: []string{"$aaptCmd"},
     37 			Description: "aapt create R.java $out",
     38 		},
     39 		"aaptFlags", "publicResourcesFile", "proguardOptionsFile", "javaDir", "javaFileList")
     40 
     41 	aaptCreateAssetsPackage = pctx.AndroidStaticRule("aaptCreateAssetsPackage",
     42 		blueprint.RuleParams{
     43 			Command:     `rm -f $out && $aaptCmd package $aaptFlags -F $out`,
     44 			CommandDeps: []string{"$aaptCmd"},
     45 			Description: "aapt export package $out",
     46 		},
     47 		"aaptFlags", "publicResourcesFile", "proguardOptionsFile", "javaDir", "javaFileList")
     48 
     49 	aaptAddResources = pctx.AndroidStaticRule("aaptAddResources",
     50 		blueprint.RuleParams{
     51 			// TODO: add-jni-shared-libs-to-package
     52 			Command:     `cp -f $in $out.tmp && $aaptCmd package -u $aaptFlags -F $out.tmp && mv $out.tmp $out`,
     53 			CommandDeps: []string{"$aaptCmd"},
     54 			Description: "aapt package $out",
     55 		},
     56 		"aaptFlags")
     57 
     58 	signapk = pctx.AndroidStaticRule("signapk",
     59 		blueprint.RuleParams{
     60 			Command:     `java -jar $signapkCmd $certificates $in $out`,
     61 			CommandDeps: []string{"$signapkCmd"},
     62 			Description: "signapk $out",
     63 		},
     64 		"certificates")
     65 
     66 	androidManifestMerger = pctx.AndroidStaticRule("androidManifestMerger",
     67 		blueprint.RuleParams{
     68 			Command: "java -classpath $androidManifestMergerCmd com.android.manifmerger.Main merge " +
     69 				"--main $in --libs $libsManifests --out $out",
     70 			CommandDeps: []string{"$androidManifestMergerCmd"},
     71 			Description: "merge manifest files $out",
     72 		},
     73 		"libsManifests")
     74 )
     75 
     76 func init() {
     77 	pctx.SourcePathVariable("androidManifestMergerCmd", "prebuilts/devtools/tools/lib/manifest-merger.jar")
     78 	pctx.HostBinToolVariable("aaptCmd", "aapt")
     79 	pctx.HostJavaToolVariable("signapkCmd", "signapk.jar")
     80 }
     81 
     82 func CreateResourceJavaFiles(ctx android.ModuleContext, flags []string,
     83 	deps android.Paths) (android.Path, android.Path, android.Path) {
     84 	javaDir := android.PathForModuleGen(ctx, "R")
     85 	javaFileList := android.PathForModuleOut(ctx, "R.filelist")
     86 	publicResourcesFile := android.PathForModuleOut(ctx, "public_resources.xml")
     87 	proguardOptionsFile := android.PathForModuleOut(ctx, "proguard.options")
     88 
     89 	ctx.ModuleBuild(pctx, android.ModuleBuildParams{
     90 		Rule:      aaptCreateResourceJavaFile,
     91 		Outputs:   android.WritablePaths{publicResourcesFile, proguardOptionsFile, javaFileList},
     92 		Implicits: deps,
     93 		Args: map[string]string{
     94 			"aaptFlags":           strings.Join(flags, " "),
     95 			"publicResourcesFile": publicResourcesFile.String(),
     96 			"proguardOptionsFile": proguardOptionsFile.String(),
     97 			"javaDir":             javaDir.String(),
     98 			"javaFileList":        javaFileList.String(),
     99 		},
    100 	})
    101 
    102 	return publicResourcesFile, proguardOptionsFile, javaFileList
    103 }
    104 
    105 func CreateExportPackage(ctx android.ModuleContext, flags []string, deps android.Paths) android.ModuleOutPath {
    106 	outputFile := android.PathForModuleOut(ctx, "package-export.apk")
    107 
    108 	ctx.ModuleBuild(pctx, android.ModuleBuildParams{
    109 		Rule:      aaptCreateAssetsPackage,
    110 		Output:    outputFile,
    111 		Implicits: deps,
    112 		Args: map[string]string{
    113 			"aaptFlags": strings.Join(flags, " "),
    114 		},
    115 	})
    116 
    117 	return outputFile
    118 }
    119 
    120 func CreateAppPackage(ctx android.ModuleContext, flags []string, jarFile android.Path,
    121 	certificates []string) android.Path {
    122 
    123 	resourceApk := android.PathForModuleOut(ctx, "resources.apk")
    124 
    125 	ctx.ModuleBuild(pctx, android.ModuleBuildParams{
    126 		Rule:   aaptAddResources,
    127 		Output: resourceApk,
    128 		Input:  jarFile,
    129 		Args: map[string]string{
    130 			"aaptFlags": strings.Join(flags, " "),
    131 		},
    132 	})
    133 
    134 	outputFile := android.PathForModuleOut(ctx, "package.apk")
    135 
    136 	var certificateArgs []string
    137 	for _, c := range certificates {
    138 		certificateArgs = append(certificateArgs, c+".x509.pem", c+".pk8")
    139 	}
    140 
    141 	ctx.ModuleBuild(pctx, android.ModuleBuildParams{
    142 		Rule:   signapk,
    143 		Output: outputFile,
    144 		Input:  resourceApk,
    145 		Args: map[string]string{
    146 			"certificates": strings.Join(certificateArgs, " "),
    147 		},
    148 	})
    149 
    150 	return outputFile
    151 }
    152