def copyResNoOverwrite(self, src, dst): if not os.path.exists(src): helper.error("src folder not exist: " + src) if not os.path.exists(dst): helper.error("dst folder not exist: " + dst) files = os.listdir(src) if not files: return for item in files: srcItem = src + '/' + item dstItem = dst + '/' + item if os.path.isdir(srcItem): if not os.path.exists(dstItem): os.mkdir(dstItem) buildLog("copy dir: " + srcItem + " to " + dstItem) self.copyResNoOverwrite(srcItem, dstItem) elif os.path.isfile(srcItem): if not os.path.exists(dstItem): helper.watchExecuteCommand('cp', srcItem + ' ' + dstItem) else: buildLog("skip exist item: " + dstItem) return
def proguard(self): inJars = pluginOutputDir + '/classes/' outJars = pluginOutputDir + '/obfuscated.jar' mappingFile = pluginOutputDir + '/mappings.txt' cmd = ' -jar ' + self.sdkDir + '/tools/proguard/lib/proguard.jar ' \ + ' -injars ' + inJars \ + ' -outjars ' + outJars \ + ' -printmapping ' + mappingFile \ + ' @' + self.sdkDir + '/tools/proguard/proguard-android.txt' \ + ' @proguard-rules.pro' \ + ' -dontnote ' \ + ' -keep class com.wenba.bangbang.' + pluginPackageName + '.LauncherPlugin' # library jars libJars = ' -libraryjars ' + self.androidJar cmd += libJars for cp in self.classPath: # if cp not in self.localJars: cmd += ' -libraryjars ' + cp helper.watchExecuteCommand('java', cmd, 'proguard') return
def buildClass(self): helper.watchExecuteCommand('mkdir -p', classesDir, 'mkdirs') javaFiles = self.localFiles + helper.collectFilesByPattern( genJavaDir, r'.*\.java$') buildArgs = '-g -bootclasspath ' + self.androidJar \ + ' -classpath ' + self.combineClassPath() \ + ' -source 1.7 -target 1.7' \ + ' -d ' + classesDir buildArgs += ' ' + ' '.join(javaFiles) helper.watchExecuteCommand('javac', buildArgs) return
def signApk(self): pwd = os.getcwd() os.chdir(pluginOutputDir) unalignedName = pluginName + '_unaligned.apk' helper.watchExecuteCommand('jarsigner', \ '-sigalg MD5withRSA -digestalg SHA1 -keystore ' \ + keyStoreFile \ + ' -storepass ' \ + keyStorePass \ + ' -keypass ' + keyAliasPass + ' ' \ + unalignedName + ' ' + keyAlias) self.zipalignApk(unalignedName) os.chdir(pwd) return
def doPackage(self): classPath = self.toolsDir + '/lib/sdklib.jar' buildArgs = ' -cp ' + classPath + ' com.android.sdklib.build.ApkBuilderMain ' \ + pluginOutputDir + '/' + pluginName + '_unaligned.apk ' \ + ' -u -z ' + resPakcageFile \ + ' -f ' + pluginOutputDir + '/classes.dex ' \ + ' -rf src/main/java ' if os.path.exists('libs'): buildArgs += ' -rj libs/' if os.path.exists('src/main/jniLibs/'): buildArgs += ' -nf src/main/jniLibs/' helper.watchExecuteCommand('java ', buildArgs, 'apkbuilder') return
def dexClass(self): pluginClassSrc = 'classes/' if doProguard: pluginClassSrc = 'obfuscated.jar' self.proguard() pwd = os.getcwd() os.chdir(pluginOutputDir) dexArgs = '--dex --output=classes.dex %s ' % pluginClassSrc if len(self.localJars) > 0: dexArgs += ' '.join(self.localJars) helper.watchExecuteCommand(self.dx, dexArgs, 'dex') os.chdir(pwd) return
def checkBuildEnv(self): # android environment self.repoDir = self.sdkDir + '/extras/android/m2repository' self.androidJar = self.sdkDir + '/platforms/android-' + self.target + '/android.jar' buildLog('androidJar path: ' + self.androidJar) if not os.path.isfile(self.androidJar): helper.error('Target: ' + self.target + ' is not installed') # build tools buildToolDir = self.sdkDir + '/build-tools/' + self.tools if not os.path.exists(buildToolDir): helper.error('no build tools find: ' + buildToolDir) print('build tools-dir:' + str(buildToolDir)) self.buildToolDir = buildToolDir self.toolsDir = self.sdkDir + '/tools' self.aapt = self.buildToolDir + '/aapt' self.dx = self.buildToolDir + '/dx' self.zipalign = self.buildToolDir + '/zipalign' if not os.path.isfile(self.aapt): helper.error('aapt is not exist: ' + self.aapt) if not os.path.isfile(self.dx): helper.error('dx is not exist: ' + self.dx) if not os.path.isfile(self.zipalign): helper.error('zipalign is not exist: ' + self.aapt) helper.watchExecuteCommand('which', 'javac') helper.watchExecuteCommand('which', 'jarsigner') return
def buildWithGradle(self): curDir = os.getcwd() os.chdir(rootDir) if pluginName == "p_live": helper.watchExecuteCommand('./gradlew', ' makeJar') helper.watchExecuteCommand('./gradlew', ' mkLiveSo') # helper.watchExecuteCommand('./gradlew', 'clean', 'gradle') helper.watchExecuteCommand( './gradlew', ':' + pluginName + ':generateDebugResources', 'gradle') os.chdir(curDir)
def genApkFile(self): self.doCrunch() # copy assets if self.assets: subFolder = '' if onlyAssetInPluginDir: subFolder = 'plugin' helper.watchExecuteCommand('mkdir -p', assetDir + '/' + subFolder) self.copyResNoOverwrite( self.assets, assetDir + '/' + subFolder, ) helper.watchExecuteCommand('mkdir', genJavaDir) commResOption = '' if commResModule: commResOption = ' -S ' + rootDir + '/' + commResModule + '/' + 'src/main/res' buildArgs = "package" \ + ' --no-crunch' \ + ' -f -0 apk' \ + ' --min-sdk-version ' + self.props['minSdkVersion'] \ + ' --target-sdk-version ' + self.props['targetSdkVersion'] \ + ' --auto-add-overlay ' \ + ' --generate-dependencies' \ + ' -G ' + pluginOutputDir + '/proguard.txt' \ + commResOption \ + " -S " + crunchDir \ + ' -S src/main/res' \ + " -I " + self.androidJar \ + " -M src/main/AndroidManifest.xml" if self.assets: buildArgs += ' -A ' + assetDir buildArgs += " -F " + resPakcageFile + " -m -J " + genJavaDir buildArgs += " -P " + genJavaDir + '/pub.xml' helper.watchExecuteCommand(self.aapt, buildArgs, 'aapt') return
#!/usr/bin/env python # -*- coding: UTF-8 -*- ''' Created on Aug 15, 2016 @author: lqp ''' import helper configFile = 'batch.prop' if __name__ == '__main__': configs = helper.readPropertyFile(configFile) plugins = configs['plugins'] pluginArray = plugins.split(',') pluginArray = map(lambda x: x.strip(), pluginArray) for item in pluginArray: helper.buildLog('start build plugin: ' + item) helper.watchExecuteCommand('./build_plugin.py', item, 'PluginBuilder') helper.buildLog('Successfully build: ' + '[' + ','.join(pluginArray) + ']')
def makeOutputDir(self): if os.path.exists(pluginOutputDir): helper.watchExecuteCommand('rm -r', pluginOutputDir) helper.watchExecuteCommand('mkdir', pluginOutputDir) return
def extractRepoLibs(self): if not self.repos: return helper.watchExecuteCommand('mkdir', repoCacheDir) allNeedRepos = [] for i in self.repos: items = i.split(':') if len(items) != 3: continue pkg = items[0].replace('.', '/') name = items[1] ver = items[2] repItem = RepoItem() repItem.groupId = pkg repItem.artifactId = name repItem.version = ver allNeedRepos += pom_parser.collectRepoAndDeps( repItem, self.repoDir) compactRepos = [] for item in allNeedRepos: if item not in compactRepos: compactRepos.append(item) for item in compactRepos: libPath = item.toLibFile(self.repoDir) if not os.path.exists(libPath): helper.error('repoLib not found: ' + libPath) if item.fileType != 'aar': # default jars, just copy helper.watchExecuteCommand( 'mkdir -p', os.path.dirname(repoCacheDir + '/' + item.toLibFile())) shutil.copy(libPath, repoCacheDir + '/' + item.toLibFile()) self.repoLibs.append(repoCacheDir + '/' + item.toLibFile()) continue import zipfile zFile = zipfile.ZipFile(libPath, 'r') if 'classes.jar' not in zFile.namelist(): helper.error('Bug: no classes.jar in aar lib') itemData = zFile.read('classes.jar') dstDirPath = repoCacheDir + '/' + item.toFolder() helper.watchExecuteCommand('mkdir -p', dstDirPath) dstFilePath = dstDirPath + '/classes.jar' dstFile = open(dstFilePath, 'w') dstFile.write(itemData) dstFile.close() self.repoLibs.append(dstFilePath) # libs if 'libs/' in zFile.namelist(): for name in zFile.namelist(): if name.startswith('libs/') and not name.endswith('/'): dstFilePath = dstDirPath + '/' + os.path.basename(name) # error('zipPath: ' + libPath) # error('dstFilePath: ' + dstFilePath) if os.path.exists(dstFilePath): continue itemData = zFile.read(name) dstFile = open(dstFilePath, 'w') dstFile.write(itemData) dstFile.close() self.repoLibs.append(dstFilePath) zFile.close() return
def zipalignApk(self, unalignedName): finalName = pluginName + '.apk' helper.watchExecuteCommand(self.zipalign, ' 4 ' + unalignedName + ' ' + finalName) return
def doCrunch(self): helper.watchExecuteCommand('mkdir', crunchDir) helper.watchExecuteCommand(self.aapt + ' crunch', '-v -S src/main/res -C ' + crunchDir, 'crunch png files') return
# pluginName= 'p_collect' if not pluginName.startswith('p_'): helper.error('plugin name should begin with p_') assetDir += '/' + pluginName pluginPackageName = pluginName[2:] buildProp = helper.readPropertyFile('build.prop') if not buildProp: helper.error('build.prop not found') print('Build start...') rootDir = os.path.abspath(rootDir) helper.projectRoot = rootDir os.chdir(rootDir) readGlobalConfig(buildProp) builder = PluginBuilder() builder.start() # copy if copyPluginToAppAssets: dstDir = rootDir + '/app/src/main/assets' if not os.path.exists(dstDir): helper.watchExecuteCommand('mkdir -p', dstDir) cmd = 'cp ' + pluginOutputDir + '/' + pluginName + '.apk ' + dstDir helper.watchExecuteCommand(cmd, cmdAlias='copy plugin to: ' + dstDir)