├── app
├── .gitignore
├── test.keystore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ └── activity_main.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── tlz
│ │ │ │ └── androidreinforceplugin
│ │ │ │ └── example
│ │ │ │ └── MainActivity.kt
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── tlz
│ │ │ └── androidreinforceplugin
│ │ │ └── example
│ │ │ └── ExampleUnitTest.kt
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── tlz
│ │ └── androidreinforceplugin
│ │ └── example
│ │ └── ExampleInstrumentedTest.kt
├── mulpkg_config.txt
├── proguard-rules.pro
└── build.gradle
├── plugin
├── .gitignore
├── libs
│ └── ms-shield.jar
├── src
│ └── main
│ │ ├── resources
│ │ └── META-INF
│ │ │ └── gradle-plugins
│ │ │ └── com.tlz.androidreinforceplugin.properties
│ │ └── kotlin
│ │ └── com
│ │ └── tlz
│ │ └── androidreinforceplugin
│ │ ├── Loggers.kt
│ │ ├── Plugin.kt
│ │ ├── AndroidReinforceExtension.kt
│ │ ├── Constants.kt
│ │ ├── Extensions.kt
│ │ └── tasks
│ │ └── AndroidReinforceTask.kt
└── build.gradle
├── settings.gradle
├── README.md
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── .gitignore
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/plugin/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':plugin'
2 |
--------------------------------------------------------------------------------
/app/test.keystore:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomlezen/AndroidReinforcePlugin/HEAD/app/test.keystore
--------------------------------------------------------------------------------
/plugin/libs/ms-shield.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomlezen/AndroidReinforcePlugin/HEAD/plugin/libs/ms-shield.jar
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 一个支持360、乐固加固的Gradle插件
2 | ### 具体配置使用[请看这里](http://blog.tomlezen.com/2019/01/22/android-reinforce-use-manual/#more)
3 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | AndroidReinforcePlugin
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomlezen/AndroidReinforcePlugin/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/plugin/src/main/resources/META-INF/gradle-plugins/com.tlz.androidreinforceplugin.properties:
--------------------------------------------------------------------------------
1 | implementation-class=com.tlz.androidreinforceplugin.Plugin
--------------------------------------------------------------------------------
/app/mulpkg_config.txt:
--------------------------------------------------------------------------------
1 | UMENG_CHANNEL 360应用平台 1
2 | UMENG_CHANNEL 谷歌市场 2
3 | UMENG_CHANNEL 91手机商城 3
4 | UMENG_CHANNEL 豌豆荚 4
5 | UMENG_CHANNEL 安卓市场 5
6 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomlezen/AndroidReinforcePlugin/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomlezen/AndroidReinforcePlugin/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomlezen/AndroidReinforcePlugin/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomlezen/AndroidReinforcePlugin/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomlezen/AndroidReinforcePlugin/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomlezen/AndroidReinforcePlugin/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomlezen/AndroidReinforcePlugin/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomlezen/AndroidReinforcePlugin/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomlezen/AndroidReinforcePlugin/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tomlezen/AndroidReinforcePlugin/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Jul 17 14:57:16 CST 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/tlz/androidreinforceplugin/example/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.tlz.androidreinforceplugin.example
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/plugin/src/main/kotlin/com/tlz/androidreinforceplugin/Loggers.kt:
--------------------------------------------------------------------------------
1 | package com.tlz.androidreinforceplugin
2 |
3 | import org.gradle.api.DefaultTask
4 | import org.gradle.api.Project
5 | import org.gradle.api.logging.LogLevel
6 |
7 | /**
8 | * Created by Tomlezen.
9 | * Date: 2019/1/20.
10 | * Time: 6:51 PM.
11 | */
12 |
13 | fun DefaultTask.log(message: String) = project.log(message)
14 | fun DefaultTask.log(e: Throwable, message: String) = project.log(e, message)
15 |
16 | fun Project.log(message: String) = logger.log(LogLevel.ERROR, message)
17 | fun Project.log(e: Throwable, message: String) = logger.log(LogLevel.ERROR, message, e)
--------------------------------------------------------------------------------
/app/src/main/java/com/tlz/androidreinforceplugin/example/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.tlz.androidreinforceplugin.example
2 |
3 | import android.content.pm.PackageManager
4 | import android.os.Bundle
5 | import androidx.appcompat.app.AppCompatActivity
6 | import kotlinx.android.synthetic.main.activity_main.*
7 |
8 | class MainActivity : AppCompatActivity() {
9 |
10 | override fun onCreate(savedInstanceState: Bundle?) {
11 | super.onCreate(savedInstanceState)
12 | setContentView(R.layout.activity_main)
13 |
14 | tv_hello.text = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)?.metaData?.getString("UMENG_CHANNEL").toString()
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/tlz/androidreinforceplugin/example/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.tlz.androidreinforceplugin.example
2 |
3 | import androidx.test.InstrumentationRegistry
4 | import androidx.test.runner.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getTargetContext()
22 | assertEquals("com.tlz.androidreinforceplugin.example", appContext.packageName)
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 | # Kotlin code style for this project: "official" or "obsolete":
21 | kotlin.code.style=official
22 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | # Built application files
15 | *.apk
16 | *.ap_
17 |
18 | # Files for the ART/Dalvik VM
19 | *.dex
20 |
21 | # Java class files
22 | *.class
23 |
24 | # Generated files
25 | bin/
26 | gen/
27 | out/
28 |
29 | # Gradle files
30 | .gradle/
31 | build/
32 |
33 | # Local configuration file (sdk path, etc)
34 | local.properties
35 |
36 | # Proguard folder generated by Eclipse
37 | proguard/
38 |
39 | # Log Files
40 | *.log
41 |
42 | # Android Studio Navigation editor temp files
43 | .navigation/
44 |
45 | # Android Studio captures folder
46 | captures/
47 |
48 | # Intellij
49 | *.iml
50 | .idea/
51 | .idea/workspace.xml
52 | .idea/tasks.xml
53 | .idea/gradle.xml
54 | .idea/dictionaries
55 | .idea/libraries
56 |
57 | # Keystore files
58 | *.jks
59 |
60 | # External native build folder generated in Android Studio 2.2 and later
61 | .externalNativeBuild
62 |
63 | # Google Services (e.g. APIs or Firebase)
64 | google-services.json
65 |
66 | # Freeline
67 | freeline.py
68 | freeline/
69 | freeline_project_description.json
70 |
71 | .cache/
72 |
73 | jiagu.db
74 | jiagu/
75 | # ms-shield.jar
76 | *.sh
--------------------------------------------------------------------------------
/plugin/src/main/kotlin/com/tlz/androidreinforceplugin/Plugin.kt:
--------------------------------------------------------------------------------
1 | package com.tlz.androidreinforceplugin
2 |
3 | import com.tlz.androidreinforceplugin.tasks.AndroidReinforceTask
4 | import org.gradle.api.Plugin
5 | import org.gradle.api.Project
6 |
7 | /**
8 | * Created by Tomlezen.
9 | * Date: 2019/1/20.
10 | * Time: 11:31 AM.
11 | */
12 | class Plugin : Plugin {
13 | override fun apply(target: Project) {
14 | // 检查是否是Android工程
15 | if (target.plugins.hasPlugin("com.android.application")) {
16 | createExtensions(target)
17 | createTasks(target)
18 |
19 | target.afterEvaluate {
20 | applyTask(target)
21 | }
22 | } else {
23 | throw IllegalArgumentException("AndroidReinforcePlugin requires the Android plugin to be configured")
24 | }
25 | }
26 |
27 | /**
28 | * 创建扩展.
29 | * @param target Project
30 | */
31 | private fun createExtensions(target: Project) {
32 | target.extensions.create("AndroidReinForce", AndroidReinforceExtension::class.java)
33 | }
34 |
35 | /**
36 | * 创建任务.
37 | * @param target Project
38 | */
39 | private fun createTasks(target: Project) {
40 | target.tasks.create("androidReinforce", AndroidReinforceTask::class.java)
41 | }
42 |
43 | /**
44 | * 应用任务.
45 | * @param target Project
46 | */
47 | private fun applyTask(target: Project) {
48 | // 遍历任务
49 | target.tasks.filter { it.name.contains("assemble(.*)Release".toRegex()) }
50 | .forEach {
51 | it.finalizedBy("androidReinforce")
52 | }
53 | }
54 | }
--------------------------------------------------------------------------------
/plugin/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'groovy'
2 | apply plugin: 'kotlin'
3 | apply plugin: 'maven'
4 | apply plugin: 'com.novoda.bintray-release'
5 |
6 | group = "com.tlz.androidreinforce"
7 | version = "1.0.1"
8 |
9 | dependencies {
10 | implementation fileTree(include: ['*.jar'], dir: 'libs')
11 | implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
12 | implementation 'com.android.tools.build:gradle:3.3.0'
13 | implementation gradleApi()
14 | compile files('libs/ms-shield.jar')
15 | }
16 |
17 | sourceCompatibility = "7"
18 | targetCompatibility = "7"
19 |
20 | jar {
21 | //删除缓存
22 | new File(project.jar.archivePath as String).delete()
23 |
24 | from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
25 | exclude 'META-INF/MANIFEST.MF'
26 | exclude 'META-INF/LICENSE'
27 | exclude 'META-INF/*.SF'
28 | exclude 'META-INF/*.DSA'
29 | exclude 'META-INF/*.RSA'
30 |
31 | manifest {
32 | attributes 'Main-Class': 'biz.Legu',
33 | 'Debugger-Version': project.version
34 | }
35 | }
36 |
37 | uploadArchives{
38 | repositories {
39 | mavenDeployer {
40 | repository(url: uri("$buildDir/repo"))
41 | }
42 | }
43 | }
44 |
45 | buildscript{
46 | tasks.withType(Javadoc) {
47 | failOnError false
48 | options{
49 | encoding "UTF-8"
50 | charSet 'UTF-8'
51 | links "http://docs.oracle.com/javase/7/docs/api"
52 | }
53 | }
54 | }
55 |
56 | publish {
57 | userOrg = parent.ext.userOrg
58 | artifactId = parent.ext.artifactId
59 | autoPublish = parent.ext.autoPublish
60 | desc = parent.ext.desc
61 | groupId = parent.ext.groupId
62 | publishVersion = parent.ext.publishVersion
63 | uploadName = parent.ext.uploadName
64 | website = parent.ext.website
65 | repoName = parent.ext.repoName
66 | }
67 |
--------------------------------------------------------------------------------
/plugin/src/main/kotlin/com/tlz/androidreinforceplugin/AndroidReinforceExtension.kt:
--------------------------------------------------------------------------------
1 | package com.tlz.androidreinforceplugin
2 |
3 | import org.gradle.api.Action
4 |
5 | /**
6 | * 此配置的优先级关系是如下:
7 | * extension > 环境配置 > 任务执行参数
8 | * Created by Tomlezen.
9 | * Date: 2019/1/20.
10 | * Time: 11:43 AM.
11 | */
12 | open class AndroidReinforceExtension {
13 |
14 | /** 360加固配置. */
15 | var reinforce360: Reinforce360Extension = Reinforce360Extension()
16 |
17 | /** 腾讯加固配置. */
18 | var reinforceLe: ReinforceLeExtension = ReinforceLeExtension()
19 |
20 | /** 加固类型,默认没有任何加固类型 */
21 | var reinforceType = ""
22 |
23 | fun reinforce360(action: Action) {
24 | action.execute(reinforce360)
25 | }
26 |
27 | fun reinforceLe(action: Action) {
28 | action.execute(reinforceLe)
29 | }
30 |
31 | open class Reinforce360Extension : ReinforceExtensions() {
32 | /** 用户. */
33 | var user = ""
34 | /** 密码. */
35 | var pass = ""
36 |
37 | /** 多渠道. */
38 | var mulpkg = false
39 | /** 多渠道配置路径. */
40 | var mulpkgConfigPath = ""
41 | /** 增值配置 默认配置只支持x86架构. 其它 -data -crashlog -vmp */
42 | var addConfig = "-x86"
43 | }
44 |
45 | open class ReinforceLeExtension : ReinforceExtensions() {
46 | /** 密钥id。 */
47 | var secretId = ""
48 | /** 密钥值. */
49 | var secretKey = ""
50 | }
51 |
52 | open class ReinforceExtensions {
53 | /** 自动签名 默认开启自动签名. */
54 | var autoSign = true
55 | /** 路径. */
56 | var path = ""
57 | /** 是否支持加固 默认都是不支持状态. */
58 | var support = false
59 | // /** 加固文件输出路径. 为空则使用默认路径即原来apk路径下的reinforce_[reinforceType]. */
60 | // var outputPath = ""
61 | // /** 加固文件输出文件名. 为空则使用默认文件名: app-[flavors]-[buildType]-[reinforceType]-signed */
62 | // var outputFileName = ""
63 | }
64 |
65 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | apply plugin: 'kotlin-android'
4 |
5 | apply plugin: 'kotlin-android-extensions'
6 |
7 | apply plugin: 'com.tlz.androidreinforceplugin'
8 |
9 | android {
10 | compileSdkVersion 28
11 | defaultConfig {
12 | applicationId "com.tlz.androidreinforceplugin.example"
13 | minSdkVersion 21
14 | targetSdkVersion 28
15 | versionCode 1
16 | versionName "1.0"
17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
18 |
19 | manifestPlaceholders = [UMENG_CHANNEL: "默认"]
20 | }
21 |
22 | signingConfigs {
23 | release {
24 | storeFile file('test.keystore')
25 | storePassword '123456'
26 | keyAlias 'test'
27 | keyPassword '123456'
28 | }
29 | }
30 |
31 | buildTypes {
32 | release {
33 | minifyEnabled false
34 | signingConfig signingConfigs.release
35 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
36 | }
37 | }
38 |
39 | flavorDimensions "version"
40 | productFlavors {
41 | dev {
42 |
43 | }
44 | }
45 | }
46 |
47 | dependencies {
48 | implementation fileTree(include: ['*.jar'], dir: 'libs')
49 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
50 | implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
51 | implementation 'androidx.core:core-ktx:1.1.0-alpha03'
52 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
53 | testImplementation 'junit:junit:4.12'
54 | androidTestImplementation 'androidx.test:runner:1.1.1'
55 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
56 | }
57 |
58 | //加载properties 这里只是加载我本地配置的账号 不能对外公开 嘿嘿
59 | def properties = new Properties()
60 | def inputStream = project.rootProject.file('local.properties').newDataInputStream()
61 | properties.load(inputStream)
62 |
63 | AndroidReinForce {
64 | //加固类型 四种方式 360 le all query
65 | reinforceType = "le"
66 |
67 | reinforce360 {
68 | // 支持该加固
69 | support = true
70 | // 配置自己的360账号
71 | user = properties.getProperty("USER_360", "")
72 | // 配置自己的360账号密码
73 | pass = properties.getProperty("PASS_360", "")
74 | // 配置自己的360加固文件路径
75 | path = file("jiagu/jiagu.jar").absolutePath
76 | // 配置支持多渠道
77 | mulpkg = true
78 | // 多渠道路径
79 | mulpkgConfigPath = file("mulpkg_config.txt").absolutePath
80 | }
81 | reinforceLe {
82 | // 支持该加固
83 | support = true
84 | // 配置自己的乐固加固文件路径 可以选择不配置使用默认内置的
85 | // path = file("ms-shield.jar").absolutePath
86 | // 配置自己的乐固secretId
87 | secretId = properties.getProperty("SECRET_ID", "")
88 | // 配置自己的乐固secretKey
89 | secretKey = properties.getProperty("SECRET_KEY", "")
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/plugin/src/main/kotlin/com/tlz/androidreinforceplugin/Constants.kt:
--------------------------------------------------------------------------------
1 | package com.tlz.androidreinforceplugin
2 |
3 | /**
4 | * Created by Tomlezen.
5 | * Date: 2019/1/20.
6 | * Time: 6:26 PM.
7 | */
8 | object Constants {
9 | /** 签名文件路径. */
10 | const val ENV_STORE_PATH = "STORE_PATH"
11 | /** 签名文件密码. */
12 | const val ENV_STORE_PASS = "STORE_PASS"
13 | /** 签名文件别名. */
14 | const val ENV_KEY_ALIAS = "KEY_ALIAS"
15 | /** 签名文件别名密码. */
16 | const val ENV_KEY_PASS = "KEY_PASS"
17 |
18 | /** 安卓sdk路径环境变量. */
19 | const val ENV_ANDROID_HOME = "ANDROID_HOME"
20 | /** 360账户环境变量. */
21 | const val ENV_360_USER = "360_USER"
22 | /** 360密码环境变量. */
23 | const val ENV_360_PASS = "360_PASS"
24 | /** 360加固文件路径环境变量. */
25 | const val ENV_360_PATH = "360_PATH"
26 | /** 乐固密钥id环境变量. */
27 | const val ENV_LE_SECRET_ID = "LE_SECRET_ID"
28 | /** 乐固密钥key环境变量. */
29 | const val ENV_LE_SECRET_KEY = "LE_SECRET_KEY"
30 | /** 乐固加固文件路径环境变量. */
31 | const val ENV_LE_PATH = "LE_PATH"
32 |
33 | /** 360加固shell文件名. */
34 | const val SHELL_FILE_NAME_360 = "reinforce_360.sh"
35 | /** 乐固加固shell文件名. */
36 | const val SHELL_FILE_NAME_LE = "reinforce_le.sh"
37 |
38 | object Params {
39 | /** 是否支持360加固. */
40 | const val P_SUPPORT_360 = "support_360"
41 | /** 是否支持乐固. */
42 | const val P_SUPPORT_LE = "support_le"
43 |
44 | /** 加固类型. */
45 | const val P_REINFORCE_TYPE = "reinforce_type"
46 |
47 | /** 360平台账户. */
48 | const val P_360_USER = "360_user"
49 | /** 360平台账户密码. */
50 | const val P_360_PASS = "360_pass"
51 | /** 360平加固文件路径. */
52 | const val P_360_PATH = "360_path"
53 | /** 是否开启多渠道打包. */
54 | const val P_360_MULPKG = "360_mulpkg"
55 | /** 多渠道配置文件路径. */
56 | const val P_360_MULPKG_CONFIG_PATH = "360_mulpkg_config_path"
57 | /** 增值服务配置. */
58 | const val P_360_ADD_CONFIG = "360_add_config"
59 | /** 是否自动签名. */
60 | const val P_360_AUTO_SIGN = "360_auto_sign"
61 |
62 | /** 乐固密钥id. */
63 | const val P_LE_SECRET_ID = "le_secret_id"
64 | /** 乐固密钥key. */
65 | const val P_LE_SECRET_KEY = "le_secret_key"
66 | /** 乐固加固文件路径. */
67 | const val P_LE_PATH = "le_path"
68 | /** 是否自动签名. */
69 | const val P_LE_AUTO_SIGN = "le_auto_sign"
70 |
71 | /** 签名文件路径. */
72 | const val P_STORE_PATH = "store_path"
73 | /** 签名文件密码. */
74 | const val P_STORE_PASS = "store_pass"
75 | /** 签名文件别名. */
76 | const val P_STORE_KEY_ALIAS = "store_key_alias"
77 | /** 签名文件别名密码. */
78 | const val P_STORE_KEY_PASS = "store_key_pass"
79 | }
80 |
81 | /**
82 | * 加固类型.
83 | */
84 | object ReinforceType {
85 | /** 360类型. */
86 | const val TYPE_360 = "360"
87 | /** 乐固类型. */
88 | const val TYPE_LE = "le"
89 | /** 所有类型 每个包都进行所有类型的加固. */
90 | const val TYPE_ALL = "all"
91 | /** 查询类型 就是根据flavorName来选择加固类型 例如果含有360字样 就采用360加固. */
92 | const val TYPE_QUERY = "query"
93 | }
94 |
95 | }
--------------------------------------------------------------------------------
/plugin/src/main/kotlin/com/tlz/androidreinforceplugin/Extensions.kt:
--------------------------------------------------------------------------------
1 | package com.tlz.androidreinforceplugin
2 |
3 | import com.android.build.gradle.AppExtension
4 | import com.android.build.gradle.api.ApplicationVariant
5 | import com.tlz.androidreinforceplugin.tasks.AndroidReinforceTask
6 | import org.gradle.api.DefaultTask
7 | import org.gradle.api.Project
8 | import java.io.BufferedReader
9 | import java.io.File
10 | import java.io.InputStreamReader
11 | import java.util.*
12 |
13 | /**
14 | * Created by Tomlezen.
15 | * Date: 2019/1/20.
16 | * Time: 6:56 PM.
17 | */
18 |
19 | /**
20 | * 获取系统属性.
21 | * @param key String
22 | * @param default String?
23 | * @return String?
24 | */
25 | fun getProperty(key: String, default: String? = ""): String? = System.getProperty(key, default)
26 |
27 | /**
28 | * 获取系统环境变量.
29 | * @param key String
30 | * @param default String
31 | * @return String
32 | */
33 | fun getEnv(key: String, default: String = ""): String = System.getenv(key) ?: default
34 |
35 |
36 | /**
37 | * 获取所有应用变体.
38 | */
39 | val DefaultTask.applicationVariants
40 | get() = project.applicationVariants
41 |
42 | /**
43 | * 获取所有应用变体.
44 | */
45 | val Project.applicationVariants
46 | get() = project.extensions
47 | .findByType(AppExtension::class.java)
48 | ?.applicationVariants
49 | ?.filter { it.buildType.name == "release" }
50 |
51 | /**
52 | * 执行shell文件.
53 | * @receiver File
54 | */
55 | fun File.doShell() = "sh $absolutePath >> logfile".doCommand()
56 |
57 |
58 | /**
59 | * 执行命令.
60 | * @receiver String
61 | */
62 | fun String.doCommand() {
63 | println("run command: $this")
64 | val process = Runtime.getRuntime().exec(this)
65 | val read = BufferedReader(InputStreamReader(process.inputStream))
66 | var line: String? = read.readLine()
67 | while (line != null) {
68 | println(line)
69 | line = read.readLine()
70 | }
71 | read.close()
72 | val error = BufferedReader(InputStreamReader(process.errorStream))
73 | line = error.readLine()
74 | var isError = false
75 | while (line != null) {
76 | println(line)
77 | line = error.readLine()
78 | isError = true
79 | }
80 | // process.waitFor()
81 | process.destroy()
82 | // 如果出现错误直接抛出异常 终止执行
83 | if (isError) throw Exception("cmd命令执行失败,$this")
84 | }
85 |
86 | /**
87 | * 获取签名.
88 | */
89 | val ApplicationVariant.keystore: AndroidReinforceTask.KeyStore
90 | get() = with(signingConfig) {
91 | if (storeFile == null || !storeFile.exists()) {
92 | throw IllegalArgumentException("您没有配置签名")
93 | }
94 | AndroidReinforceTask.KeyStore(storeFile.absolutePath, storePassword, keyAlias, keyPassword)
95 | }
96 |
97 | /**
98 | * 判断是否是window系统.
99 | */
100 | val isWindowOs: Boolean
101 | get() = System.getProperty("os.name")?.toLowerCase() == "window"
102 |
103 | /**
104 | * 获取项目local.properties文件的属性.
105 | * @receiver Project
106 | * @param key String
107 | * @param default String
108 | * @return String
109 | */
110 | fun Project.getLocalProperty(key: String, default: String = ""): String =
111 | Properties().also { p ->
112 | p.load(rootProject.file("local.properties").inputStream())
113 | }.getProperty(key, default)
114 |
115 | /**
116 | * 删除文件.
117 | * @receiver String
118 | */
119 | fun String.deleteFile() {
120 | val file = File(this)
121 | if (file.exists()) {
122 | file.deleteRecursively()
123 | }
124 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
10 |
12 |
14 |
16 |
18 |
20 |
22 |
24 |
26 |
28 |
30 |
32 |
34 |
36 |
38 |
40 |
42 |
44 |
46 |
48 |
50 |
52 |
54 |
56 |
58 |
60 |
62 |
64 |
66 |
68 |
70 |
72 |
74 |
75 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/plugin/src/main/kotlin/com/tlz/androidreinforceplugin/tasks/AndroidReinforceTask.kt:
--------------------------------------------------------------------------------
1 | package com.tlz.androidreinforceplugin.tasks
2 |
3 | import biz.Legu
4 | import com.tlz.androidreinforceplugin.*
5 | import org.gradle.api.DefaultTask
6 | import org.gradle.api.tasks.TaskAction
7 | import java.io.File
8 | import java.util.*
9 |
10 | /**
11 | * 加固任务.
12 | * 一定要注意权限问题 一定要给加固工具添加相应的权限.
13 | * Created by Tomlezen.
14 | * Date: 2019/1/20.
15 | * Time: 6:17 PM.
16 | */
17 | open class AndroidReinforceTask : DefaultTask() {
18 |
19 | @TaskAction
20 | fun doTask() {
21 | log("******************AndroidReinforceTask Start******************")
22 | // 配置信息
23 | val reinforceExtension = project.extensions.getByType(AndroidReinforceExtension::class.java)
24 | // 获取环境变量里的签名配置
25 | val keyStore = getKeystoreEnv()
26 | // 读取加固类型
27 | val reinforceType = getProperty(Constants.Params.P_REINFORCE_TYPE, reinforceExtension.reinforceType)
28 | when (reinforceType) {
29 | Constants.ReinforceType.TYPE_360 -> do360Reinforce(reinforceExtension, keyStore)
30 | Constants.ReinforceType.TYPE_LE -> doLeReinforce(reinforceExtension, keyStore)
31 | Constants.ReinforceType.TYPE_QUERY -> {
32 | do360Reinforce(reinforceExtension, keyStore, true)
33 | doLeReinforce(reinforceExtension, keyStore, true)
34 | }
35 | Constants.ReinforceType.TYPE_ALL -> {
36 | do360Reinforce(reinforceExtension, keyStore)
37 | doLeReinforce(reinforceExtension, keyStore)
38 | }
39 | else -> log("disable reinforce")
40 | }
41 | log("******************AndroidReinforceTask End******************")
42 | log("******************温馨提示:加固并签名的包请务必测试是否正常运行******************")
43 | }
44 |
45 | /**
46 | * 执行360加固.
47 | * @param keyStore KeyStore
48 | * @param isQuery Boolean 是否是查询模式.
49 | */
50 | private fun do360Reinforce(
51 | reinforceExtension: AndroidReinforceExtension,
52 | keyStore: KeyStore,
53 | isQuery: Boolean = false
54 | ) {
55 | // 判断是否支持360加固
56 | if (!(getProperty(
57 | Constants.Params.P_SUPPORT_360,
58 | reinforceExtension.reinforce360.support.toString()
59 | )?.toBoolean() ?: reinforceExtension.reinforce360.support)
60 | ) {
61 | log("您设置不支持360加固")
62 | return
63 | }
64 |
65 | log("==================360加固开始")
66 | // 检查360的环境以及参数.
67 | val jarPath = getProperty(
68 | Constants.Params.P_360_PATH,
69 | getEnv(Constants.ENV_360_PATH, reinforceExtension.reinforce360.path)
70 | ).checkNull("缺少360加固路径: ${Constants.Params.P_360_PATH}")
71 | val user =
72 | getProperty(
73 | Constants.Params.P_360_USER,
74 | getEnv(Constants.ENV_360_USER, reinforceExtension.reinforce360.user)
75 | ).checkNull("缺少360加固平台账户")
76 | val pass =
77 | getProperty(
78 | Constants.Params.P_360_PASS,
79 | getEnv(Constants.ENV_360_PASS, reinforceExtension.reinforce360.pass)
80 | ).checkNull("缺少360加固平台账户密码")
81 | val addConfig =
82 | getProperty(
83 | Constants.Params.P_360_ADD_CONFIG,
84 | reinforceExtension.reinforce360.addConfig
85 | ) ?: ""
86 | val autoSign =
87 | getProperty(
88 | Constants.Params.P_360_AUTO_SIGN,
89 | reinforceExtension.reinforce360.autoSign.toString()
90 | )?.toBoolean() ?: true
91 | val mulpkg =
92 | getProperty(
93 | Constants.Params.P_360_MULPKG,
94 | reinforceExtension.reinforce360.mulpkg.toString()
95 | )?.toBoolean() ?: false
96 | val mulPkgConfigPath =
97 | getProperty(
98 | Constants.Params.P_360_MULPKG_CONFIG_PATH,
99 | reinforceExtension.reinforce360.mulpkgConfigPath
100 | ) ?: ""
101 | // 如果开启多渠道 则检查配置路径参数
102 | if (mulpkg) {
103 | mulPkgConfigPath.checkNull("缺少多渠道配置文件路径")
104 | }
105 | // 找到apk执行加固
106 | applicationVariants?.filter {
107 | val fName = it.flavorName.toLowerCase(Locale.getDefault())
108 | !isQuery || KEYWORDS_360.any { k -> fName.contains(k) }
109 | }?.forEach { appVariant ->
110 | // 获取签名信息.
111 | val (storePath, storePassword, keyAlias, keyPassword) = if (keyStore.path.isBlank()) appVariant.keystore else keyStore
112 | appVariant.outputs.firstOrNull { it.outputFile.exists() }
113 | ?.let { variant ->
114 | // apk文件
115 | val apkFile = variant.outputFile
116 | // apk路径
117 | val apkPath = apkFile.absolutePath
118 | // apk所在的文件夹路径
119 | val outApkPath = File(apkFile.parentFile, "reinforce_360").let {
120 | if (!it.exists()) {
121 | it.mkdirs()
122 | }
123 | it.absolutePath
124 | }
125 |
126 | // 这里不用shell脚本是为了兼容window
127 | "java -jar $jarPath -version".doCommand()
128 | "java -jar $jarPath -login $user $pass".doCommand()
129 | "java -jar $jarPath -config $addConfig".doCommand()
130 | "java -jar $jarPath -importsign $storePath $storePassword $keyAlias $keyPassword".doCommand()
131 | "java -jar $jarPath -showsign".doCommand()
132 | if (mulpkg) {
133 | "java -jar $jarPath -importmulpkg $mulPkgConfigPath".doCommand()
134 | "java -jar $jarPath -showmulpkg".doCommand()
135 | }
136 | "java -jar $jarPath -jiagu $apkPath $outApkPath${if (autoSign) " -autosign" else ""}${if (mulpkg) " -automulpkg" else ""}".doCommand()
137 | } ?: log("没有找到需要360加固的apk: ${appVariant.flavorName}")
138 | } ?: log("没有找到需要360加固的apk")
139 | log("==================360加固 结束")
140 | }
141 |
142 | /**
143 | * 执行乐固加固.
144 | * @param keyStore KeyStore
145 | * @param isQuery Boolean
146 | */
147 | private fun doLeReinforce(
148 | reinforceExtension: AndroidReinforceExtension,
149 | keyStore: KeyStore,
150 | isQuery: Boolean = false
151 | ) {
152 | // 判断是否支持乐固
153 | if (!(getProperty(
154 | Constants.Params.P_SUPPORT_LE,
155 | reinforceExtension.reinforceLe.support.toString()
156 | )?.toBoolean() ?: reinforceExtension.reinforceLe.support)
157 | ) {
158 | log("do not support le reinforce")
159 | return
160 | }
161 | log("==================乐固 开始")
162 | // 检查乐固的环境以及参数.
163 | val jarPath = getProperty(
164 | Constants.Params.P_LE_PATH,
165 | getEnv(Constants.ENV_LE_PATH, reinforceExtension.reinforceLe.path)
166 | )
167 | val secretId =
168 | getProperty(
169 | Constants.Params.P_LE_SECRET_ID,
170 | getEnv(Constants.ENV_LE_SECRET_ID, reinforceExtension.reinforceLe.secretId)
171 | ).checkNull("缺少乐固认证id: secretId")
172 | val secretKey =
173 | getProperty(
174 | Constants.Params.P_LE_SECRET_KEY,
175 | getEnv(Constants.ENV_LE_SECRET_KEY, reinforceExtension.reinforceLe.secretKey)
176 | ).checkNull("缺少乐固认证key: secretKey")
177 | val autoSign =
178 | getProperty(
179 | Constants.Params.P_LE_AUTO_SIGN,
180 | reinforceExtension.reinforceLe.autoSign.toString()
181 | )?.toBoolean() ?: true
182 |
183 | // 找到apk加固
184 | applicationVariants?.filter {
185 | val fName = it.flavorName.toLowerCase(Locale.getDefault())
186 | !isQuery || KEYWORDS_LE.any { k -> fName.contains(k) }
187 | }?.forEach { appVariant ->
188 | // 获取签名信息.
189 | val (storePath, storePassword, keyAlias, keyPass) = if (keyStore.path.isBlank()) appVariant.keystore else keyStore
190 | appVariant.outputs.firstOrNull { it.outputFile.exists() }
191 | ?.let { variant ->
192 | // apk文件
193 | val apkFile = variant.outputFile
194 | // apk路径
195 | val apkPath = apkFile.absolutePath
196 | // apk所在的文件夹路径
197 | val outApkPath = File(apkFile.parentFile, "reinforce_le").let {
198 | if (!it.exists()) {
199 | it.mkdirs()
200 | }
201 | it.absolutePath
202 | }
203 |
204 | // 执行加固操作
205 |
206 | // 如果没有设置jar路径 则使用内置
207 | if (jarPath.isNullOrBlank()) {
208 | val innerJarPath = Legu::class.java.protectionDomain.codeSource.location.file
209 | log("使用内置乐固jar包: $innerJarPath")
210 | "java -Dfile.encoding=utf-8 -jar $innerJarPath -sid $secretId -skey $secretKey -uploadPath $apkPath -downloadPath $outApkPath".doCommand()
211 | } else {
212 | "java -Dfile.encoding=utf-8 -jar $jarPath -sid $secretId -skey $secretKey -uploadPath $apkPath -downloadPath $outApkPath".doCommand()
213 | }
214 |
215 | if (autoSign) {
216 | // android sdk 路径
217 | val androidHome = getEnv(
218 | Constants.ENV_ANDROID_HOME,
219 | project.getLocalProperty("sdk.dir")
220 | ).checkNull("缺少android sdk环境变量")
221 | // 编译工具
222 | val buildToolPath = File(androidHome, "build-tools").listFiles()?.last()?.absolutePath
223 | // 签名工具路径
224 | val signerPath = File(buildToolPath, if (isWindowOs) "apksigner.bat" else "apksigner").absolutePath
225 | // 对齐工具路径
226 | val zipalignPath = File(buildToolPath, if (isWindowOs) "zipalign.exe" else "zipalign").absolutePath
227 | val apkFileName = apkFile.nameWithoutExtension
228 | val leFile = File(outApkPath, "${apkFileName}_legu.apk")
229 | val leSignedFile = File(outApkPath, "${apkFileName}_le_signed.apk")
230 | val zipFile = File(outApkPath, "${apkFileName}_le_align.apk")
231 |
232 | // 删除存在文件.
233 | zipFile.delete()
234 | leSignedFile.delete()
235 |
236 | log("------ 开始压缩对齐 ------")
237 | "$zipalignPath -v 4 ${leFile.absolutePath} ${zipFile.absolutePath}".doCommand()
238 | log("------ 开始签名 ------")
239 | "$signerPath sign --ks $storePath --ks-pass pass:$storePassword --ks-key-alias $keyAlias --key-pass pass:$keyPass ${zipFile.absolutePath}".doCommand()
240 | log("------ 完成签名 ------")
241 |
242 | // 重新命名该文件
243 | zipFile.renameTo(leSignedFile)
244 |
245 | // 删除中间文件
246 | leFile.delete()
247 | zipFile.delete()
248 | log("apk路径:${leSignedFile.absolutePath}")
249 | }
250 | } ?: log("没有找到需要乐固加固的apk: ${appVariant.flavorName}")
251 | } ?: log("没有找到需要乐固加固的apk")
252 | log("==================乐固 结束")
253 | }
254 |
255 | /**
256 | * 获取签名配置.
257 | * 先读取属性参数 在读取环境变量
258 | * @return KeyStore
259 | */
260 | private fun getKeystoreEnv(): KeyStore {
261 | val keyStorePath = getProperty(Constants.Params.P_STORE_PATH, getEnv(Constants.ENV_STORE_PATH))
262 | val keyStorePass = getProperty(Constants.Params.P_STORE_PASS, getEnv(Constants.ENV_STORE_PASS))
263 | val keyStoreAlias = getProperty(Constants.Params.P_STORE_KEY_ALIAS, getEnv(Constants.ENV_KEY_ALIAS))
264 | val keyStoreAliasPass = getProperty(Constants.Params.P_STORE_KEY_PASS, getEnv(Constants.ENV_KEY_PASS))
265 | return KeyStore(keyStorePath ?: "", keyStorePass ?: "", keyStoreAlias ?: "", keyStoreAliasPass ?: "")
266 | }
267 |
268 | /**
269 | * 检查参数是否为空.
270 | * @receiver String?
271 | * @param message String
272 | * @return String
273 | */
274 | private fun String?.checkNull(message: String): String {
275 | if (isNullOrBlank()) {
276 | throw AssertionError(message)
277 | }
278 | return this.toString()
279 | }
280 |
281 | /**
282 | * 新建一个shell文件.
283 | * @param fileName String
284 | * @return File
285 | */
286 | private fun newShellFile(fileName: String): File {
287 | val shellFile = project.file(fileName)
288 | if (shellFile.exists()) {
289 | shellFile.delete()
290 | }
291 | shellFile.createNewFile()
292 | return shellFile
293 | }
294 |
295 | data class KeyStore(
296 | val path: String,
297 | val pass: String,
298 | val alias: String,
299 | val aliasPass: String
300 | )
301 |
302 | companion object {
303 | /** 乐固关键词. */
304 | val KEYWORDS_LE = arrayOf("yyb", "legu", "yingyongbao", "tencent")
305 | /** 360加固关键词. */
306 | val KEYWORDS_360 = arrayOf("360", "qihu")
307 | }
308 | }
--------------------------------------------------------------------------------