commit 631582fbabf5039b5a95c6122d7762c4e0b4142b Author: xiawanli Date: Thu Feb 27 11:46:54 2020 +0800 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4b97423 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +.idea +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..9b7d367 --- /dev/null +++ b/build.gradle @@ -0,0 +1,41 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +allprojects { + apply plugin: 'maven' + apply plugin: 'idea' + apply plugin: 'eclipse' + version = '1.0.1' +} + +defaultTasks('clean','distZip') + +subprojects { + apply plugin: 'java' + apply plugin: 'maven' + sourceCompatibility = 1.8 + targetCompatibility = 1.8 + + repositories { + mavenCentral() + } + + [compileJava, compileTestJava]*.options.collect {options ->options.encoding = 'UTF-8'} + + dependencies { + compile fileTree(dir: 'libs', include: '*.jar') + } + + jar { + manifest { + attributes("Implementation-Title": project.name, + "Implementation-Version": project.version, + "Build-Time": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"), + "Build-Number": System.env.BUILD_NUMBER?System.env.BUILD_NUMBER:"-1", + ) + } + from (project.parent.projectDir) { + include 'NOTICE.txt' + include 'LICENSE.txt' + into('META-INF') + } + } +} diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..199d16e --- /dev/null +++ b/gradle.properties @@ -0,0 +1,20 @@ +# Project-wide Gradle settings. +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +org.gradle.jvmargs=-Xmx1536m +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true +# AndroidX package structure to make it clearer which packages are bundled with the +# Android operating system, and which are packaged with your app's APK +# https://developer.android.com/topic/libraries/support-library/androidx-rn +android.useAndroidX=true +# Automatically convert third-party libraries to use AndroidX +android.enableJetifier=true + diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..f6b961f Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..8cf710f --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Mon Jan 27 13:23:47 CST 2020 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.4-rc-1-all.zip diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..cccdd3d --- /dev/null +++ b/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..e95643d --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/lib/.gitignore b/lib/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/lib/.gitignore @@ -0,0 +1 @@ +/build diff --git a/lib/build.gradle b/lib/build.gradle new file mode 100644 index 0000000..45b030d --- /dev/null +++ b/lib/build.gradle @@ -0,0 +1,31 @@ +apply plugin: 'java-library' + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) +} + +jar{ + baseName = "ManifestEditor" + manifest { + attributes 'Main-Class': 'com.wind.meditor.ManifestEditorMain' + } + //添加将引用的jar的源码打入最终的jar + from { + (configurations.runtime).collect { + it.isDirectory() ? it : zipTree(it) + } + } + + from fileTree(dir:'src/main', includes: ['assets/**']) + + //排除引用的jar中的签名信息 + exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.MF' +} + +//添加源码中引入的非代码文件,例如资源等 +sourceSets.main.resources { + srcDirs = [ + "src/main/java", + ]; + include "**/*.*" +} diff --git a/lib/src/main/assets/new_keystore b/lib/src/main/assets/new_keystore new file mode 100644 index 0000000..0ddfbbf Binary files /dev/null and b/lib/src/main/assets/new_keystore differ diff --git a/lib/src/main/assets/public.xml b/lib/src/main/assets/public.xml new file mode 100644 index 0000000..997575f --- /dev/null +++ b/lib/src/main/assets/public.xml @@ -0,0 +1,2925 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/src/main/java/com/wind/meditor/ManifestEditorMain.java b/lib/src/main/java/com/wind/meditor/ManifestEditorMain.java new file mode 100644 index 0000000..8d7f81e --- /dev/null +++ b/lib/src/main/java/com/wind/meditor/ManifestEditorMain.java @@ -0,0 +1,243 @@ +package com.wind.meditor; + +import com.wind.meditor.base.BaseCommand; +import com.wind.meditor.core.ApkSigner; +import com.wind.meditor.core.FileProcesser; +import com.wind.meditor.property.AttributeItem; +import com.wind.meditor.property.ModificationProperty; +import com.wind.meditor.utils.FileTypeUtils; +import com.wind.meditor.utils.Log; +import com.wind.meditor.utils.NodeValue; +import com.wind.meditor.utils.Utils; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * @author Windysha + */ +public class ManifestEditorMain extends BaseCommand { + + private static final String MULTI_NAME_SEPERATER = ":"; + private static final String ANDROID_NAMESPACE = "android-"; + + @Opt(opt = "o", longOpt = "output", description = "output modified xml or apk file, default is " + + "$source_apk_dir/[file-name]-new.xml or [file-name]-new-unsigned.apk", argName = "output-file") + private String output; // the output file path + + @Opt(opt = "f", longOpt = "force", hasArg = false, description = "force overwrite") + private boolean forceOverwrite = false; + + @Opt(opt = "s", longOpt = "signApk", hasArg = false, description = "use jarsigner to sign the output apk file") + private boolean needSignApk = false; + + @Opt(opt = "pkg", longOpt = "packageName", description = "set the android manifest package name", + argName = "new-package-name") + private String packageName; + + @Opt(opt = "vc", longOpt = "versionCode", description = "set the app version code", + argName = "new-version-code") + private int versionCode; + + @Opt(opt = "vn", longOpt = "versionName", description = "set the app version name", + argName = "new-version-name") + private String versionName; + + @Opt(opt = "d", longOpt = "debuggable", description = "set 1 to make the app debuggable = true, " + + "set 0 to make the app debuggable = false", argName = "0 or 1") + private int debuggable = -1; + + @Opt(opt = "an", longOpt = "applicationName", description = "set the app entry application name", + argName = "new-application-name") + private String applicationName; + + @Opt(opt = "up", longOpt = "usesPermission", description = "add the app uses permission " + + "name to the manifest file, multi option is supported", argName = "uses-permission-name") + private List usesPermissionList = new ArrayList<>(); + + @Opt(opt = "ma", longOpt = "manifestAttribute", description = "set the app manifest attribute, " + + " name and value should be separated by " + MULTI_NAME_SEPERATER + + " , if name is in android namespace, prefix \"" + ANDROID_NAMESPACE + "\" should be set" + + ", multi option is supported", argName = "manifest-attribute-name-value") + private List manifestAttributeList = new ArrayList<>(); + + @Opt(opt = "aa", longOpt = "applicationAttribute", description = "set the application attribute, " + + " name and value should be separated by " + MULTI_NAME_SEPERATER + + " , if name is in android namespace, prefix \"" + ANDROID_NAMESPACE + "\" should be set" + + ", multi option is supported", argName = "application-attribute-name-value") + private List applicationAttributeList = new ArrayList<>(); + + public static void main(String... args) { + new ManifestEditorMain().doMain(args); + } + + @Override + protected void doCommandLine() throws Exception { + if (remainingArgs.length != 1) { + if (remainingArgs.length == 0) { + Log.e("Please choose one xml or apk file you want to process. "); + } + if (remainingArgs.length > 1) { + Log.e("This tool can only used with one xml or apk file."); + } + usage(); + return; + } + + String srcFilePath = remainingArgs[0]; + File srcFile = new File(srcFilePath); + + if (!srcFile.exists()) { + Log.e(String.format("input file %s do not exist, please check it!", srcFilePath)); + usage(); + return; + } + + boolean isMainfestFile = FileTypeUtils.isAndroidManifestFile(srcFilePath); + boolean isApkFile = false; + if (!isMainfestFile) { + isApkFile = FileTypeUtils.isApkFile(srcFilePath); + } + + if (!isMainfestFile && !isApkFile) { + Log.e("input file should be manifest file or apk file !!!"); + usage(); + return; + } + + String signedApkPath = ""; + + if (output == null || output.length() == 0) { + if (isMainfestFile) { + output = getBaseName(srcFilePath) + "-new.xml"; + } + if (isApkFile) { + output = getBaseName(srcFilePath) + "-unsigned.apk"; + if (needSignApk) { + signedApkPath = getBaseName(srcFilePath) + "-signed.apk"; + } + } + } else { + if (isApkFile && needSignApk) { + signedApkPath = getBaseName(output) + "-signed.apk"; + } + } + + File outputFile = new File(output); + if (outputFile.exists() && !forceOverwrite) { + Log.e(output + " exists, use --force to overwrite the output file"); + usage(); + return; + } + + Log.i("output file path --> " + output); + + ModificationProperty modificationProperty = composeProperty(); + + if (isMainfestFile) { + Log.i("Start to process manifest file "); + FileProcesser.processManifestFile(srcFilePath, output, modificationProperty); + } else if (isApkFile) { + Log.i("Start to process apk."); + FileProcesser.processApkFile(srcFilePath, output, modificationProperty); + + if (needSignApk) { + Log.i("Start to sign the apk."); + + String parentPath = null; + String keyStoreFilePath = null; + File parentFile = new File(output).getParentFile(); + + if (parentFile != null) { + parentPath = parentFile.getAbsolutePath(); + keyStoreFilePath = parentPath + File.separator + "keystore"; + } else { + // 当前命令行所在的目录 + keyStoreFilePath = "keystore"; + } + Log.d(" parentPath = " + parentPath + " keyStoreFilePath = " + keyStoreFilePath); + + Log.i(" output unsigned apk path = " + output); + Log.i(" output signed apk path = " + signedApkPath); + + + // cannot use File.separator to seperate assets/new_keystore,or IOException is thrown on window os + Utils.copyFileFromJar("assets/new_keystore", keyStoreFilePath); + + ApkSigner.signApk(output, keyStoreFilePath, signedApkPath); + + // delete the keystore file finally + File keyStoreFile = new File(keyStoreFilePath); + if (keyStoreFile.exists()) { + keyStoreFile.delete(); + } + } + } + } + + private ModificationProperty composeProperty() { + ModificationProperty property = new ModificationProperty(); + + if (!Utils.isNullOrEmpty(packageName)) { + property.addManifestAttribute(new AttributeItem(NodeValue.Manifest.PACKAGE, packageName).setNamespace(null)); + } + + if (versionCode > 0) { + property.addManifestAttribute(new AttributeItem(NodeValue.Manifest.VERSION_CODE, versionCode)); + } + + if (!Utils.isNullOrEmpty(versionName)) { + property.addManifestAttribute(new AttributeItem(NodeValue.Manifest.VERSION_NAME, versionName)); + } + + if (debuggable >= 0) { + property.addApplicationAttribute(new AttributeItem(NodeValue.Application.DEBUGGABLE, debuggable != 0)); + } + + if (!Utils.isNullOrEmpty(applicationName)) { + property.addApplicationAttribute(new AttributeItem(NodeValue.Application.NAME, applicationName)); + } + + for (String permission : usesPermissionList) { + property.addUsesPermission(permission); + } + + for (String manfestAttr : manifestAttributeList) { + String[] nameValue = manfestAttr.split(MULTI_NAME_SEPERATER); + if (nameValue.length == 2) { + if (nameValue[0].trim().startsWith(ANDROID_NAMESPACE)) { + property.addManifestAttribute(new AttributeItem( + nameValue[0].trim().substring(ANDROID_NAMESPACE.length()), nameValue[1].trim())); + } else { + property.addManifestAttribute(new AttributeItem(nameValue[0].trim(), nameValue[1].trim()).setNamespace(null)); + } + } + } + + for (String applicationAttr : applicationAttributeList) { + String[] nameValue = applicationAttr.split(MULTI_NAME_SEPERATER); + if (nameValue.length == 2) { + if (nameValue[0].trim().startsWith(ANDROID_NAMESPACE)) { + property.addApplicationAttribute(new AttributeItem( + nameValue[0].trim().substring(ANDROID_NAMESPACE.length()), nameValue[1].trim())); + } else { + property.addApplicationAttribute(new AttributeItem(nameValue[0].trim(), nameValue[1].trim()).setNamespace(null)); + } + } + } + +// property.addManifestAttribute(new AttributeItem(NodeValue.Manifest.PACKAGE, "wind.new.pkg.name111").setNamespace(null)) +// .addManifestAttribute(new AttributeItem(NodeValue.Manifest.VERSION_CODE, 1)) +// .addManifestAttribute(new AttributeItem(NodeValue.Manifest.VERSION_NAME, "1123")) +// .addUsesPermission("android.permission.READ_EXTERNAL_STORAGE") +// .addUsesPermission("android.permission.WRITE_EXTERNAL_STORAGE") +// .addMetaData(new ModificationProperty.MetaData("aa", "11")) +// .addMetaData(new ModificationProperty.MetaData("aa", "22")) +// .addApplicationAttribute(new AttributeItem(NodeValue.Application.DEBUGGABLE, false)) +// .addApplicationAttribute(new AttributeItem(NodeValue.Application.NAME, "my.app.name.MyTestApplication")) +// .addApplicationAttribute(new AttributeItem("appComponentFactory", "my.app.name.MyTestApplication111")); + + return property; + } +} diff --git a/lib/src/main/java/com/wind/meditor/base/BaseCommand.java b/lib/src/main/java/com/wind/meditor/base/BaseCommand.java new file mode 100644 index 0000000..bbeb500 --- /dev/null +++ b/lib/src/main/java/com/wind/meditor/base/BaseCommand.java @@ -0,0 +1,477 @@ +package com.wind.meditor.base; + +import com.wind.meditor.utils.Log; + +import java.io.File; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +public abstract class BaseCommand { + + private String onlineHelp; + + protected Map optMap = new HashMap(); + + @Opt(opt = "h", longOpt = "help", hasArg = false, description = "Print this help message") + private boolean printHelp = false; + + protected String remainingArgs[]; + protected String orginalArgs[]; + + @Retention(value = RetentionPolicy.RUNTIME) + @Target(value = { ElementType.FIELD }) + static public @interface Opt { + String argName() default ""; + + String description() default ""; + + boolean hasArg() default true; + + String longOpt() default ""; + + String opt() default ""; + + boolean required() default false; + } + + static protected class Option implements Comparable