AS Arctic Fox配置360加固命令行模式gradle插件

Posted 爱炒饭

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AS Arctic Fox配置360加固命令行模式gradle插件相关的知识,希望对你有一定的参考价值。

在Gradle中创建自己定义插件,Gradle提供了三种方式:
在build.gradle脚本中直接使用
在buildSrc中使用
在独立Module中使用
本文介绍的是第三种,第三种方式的插件可以上传到本地或者网络上供其它工程或者项目使用

一、配置流程

1.1插件module代码编写

新建一个module,java module或者android library module都可以,反正后面会修改插件module的build.gradle文件,去掉相关依赖。
在插件module的main目录下新建groovy目录,在该目录下使用groovy语法进行功能开发。
自定义Task属性拓展类ConfigBean.groovy

//ConfigBean.groovy
package com.shan.plugin;

public class ConfigBean 
    private String userName;
    private String userPassword;
    private String keyStorePath;
    private String keyStorePassword;
    private String keyAlias;
    private String keyAliasPassword;
    private String jarToolsPath;

    public String getUserName() 
        return userName;
    

    public void setUserName(String userName) 
        this.userName = userName;
    

    public String getUserPassword() 
        return userPassword;
    

    public void setUserPassword(String userPassword) 
        this.userPassword = userPassword;
    

    public String getKeyStorePath() 
        return keyStorePath;
    

    public void setKeyStorePath(String keyStorePath) 
        this.keyStorePath = keyStorePath;
    

    public String getKeyStorePassword() 
        return keyStorePassword;
    

    public void setKeyStorePassword(String keyStorePassword) 
        this.keyStorePassword = keyStorePassword;
    

    public String getKeyAlias() 
        return keyAlias;
    

    public void setKeyAlias(String keyAlias) 
        this.keyAlias = keyAlias;
    

    public String getKeyAliasPassword() 
        return keyAliasPassword;
    

    public void setKeyAliasPassword(String keyAliasPassword) 
        this.keyAliasPassword = keyAliasPassword;
    

    public String getJarToolsPath() 
        return jarToolsPath;
    

    public void setJarToolsPath(String jarToolsPath) 
        this.jarToolsPath = jarToolsPath;
    

    @Override
    public String toString() 
        return "ConfigBean" +
                "userName='" + userName + '\\'' +
                ", userPassword='" + userPassword + '\\'' +
                ", keyStorePath='" + keyStorePath + '\\'' +
                ", keyStorePassword='" + keyStorePassword + '\\'' +
                ", keyAlias='" + keyAlias + '\\'' +
                ", keyAliasPassword='" + keyAliasPassword + '\\'' +
                ", jarToolsPath='" + jarToolsPath + '\\'' +
                '';
    

插件类JiaguPlugin.groovy

//JiaguPlugin.groovy
package com.shan.plugin;
import org.gradle.api.Action;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import com.android.build.gradle.AppExtension;
import com.android.build.gradle.api.ApplicationVariant;
import com.android.build.gradle.api.BaseVariantOutput;


/**
 * Author by wuyishan, Date on 2022/1/19.
 */
class JiaguPlugin implements Plugin<Project> 

    @Override
    void apply(Project project) 
        println 'JiaguPlugin start'
        println 'JiaguPlugin start1'
        final ConfigBean mConfigExtension = project.getExtensions().create("mConfigExtension",ConfigBean.class);
        println 'JiaguPlugin start2'
        project.afterEvaluate(new Action<Project>() 
            @Override
            void execute(Project project2) 
                println 'JiaguPlugin start3'
                AppExtension appExtension = project2.getExtensions().getByType(AppExtension.class);
                println 'JiaguPlugin start4'
                appExtension.getApplicationVariants().all(new Action<ApplicationVariant>()
                    @Override
                    void execute(ApplicationVariant applicationVariant) 
                        println 'JiaguPlugin start5'
                        applicationVariant.getOutputs().all(new Action<BaseVariantOutput>() 
                            @Override
                            void execute(BaseVariantOutput baseVariantOutput) 
                                File outputFile = baseVariantOutput.getOutputFile();
                                println 'JiaguPlugin start6===='+outputFile.getName()
                                if (outputFile.getName().contains("release")) 
                                    project2.getTasks().create("jiagu",JiaguTask.class,outputFile,mConfigExtension);
                                
                            
                        );
                    
                );
            
        );
    

插件任务类1JiaguTask.groovy

//JiaguTask.groovy
package com.shan.plugin;
import org.gradle.api.Action;
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.TaskAction;
import org.gradle.process.ExecSpec;
import javax.inject.Inject;
/**
 * Author by wuyishan, Date on 2022/1/20.
 */
class JiaguTask extends DefaultTask 
    @Internal
    def mApk
    private final ConfigBean mConfigExtension
    @Internal
    def userName;
    @Internal
    def userPassword;
    @Internal
    def keyStorePath;
    @Internal
    def keyStorePassword;
    @Internal
    def keyAlias;
    @Internal
    def keyAliasPassword;
    @Internal
    def jarToolsPath;

    @Inject
    JiaguTask(File mApk, ConfigBean mConfigExtension) 
        setGroup("jiagu")
        println 'JiaguTask start,path:'+mApk.getAbsolutePath()
        println 'JiaguTask start,mConfigExtension:'+mConfigExtension.toString()
        this.mApk = mApk
        this.mConfigExtension = mConfigExtension
        userName = mConfigExtension.getUserName()
        userPassword = mConfigExtension.getUserPassword()
        keyStorePath = mConfigExtension.getKeyStorePath()
        keyStorePassword = mConfigExtension.getKeyStorePassword()
        keyAlias = mConfigExtension.getKeyAlias()
        keyAliasPassword = mConfigExtension.getKeyAliasPassword()
        jarToolsPath = mConfigExtension.getJarToolsPath()

    

    @TaskAction
    void doAction() 
        // SystemOutPrint.println("JiaguTask is running ");
        println 'JiaguTask is running,mConfigExtension='+mConfigExtension.toString()
        getProject().exec(new Action<ExecSpec>() 
            @Override
            public void execute(ExecSpec execSpec) 
                //println 'JiaguTask is running, getProject().exe='+mConfigExtension.getJarToolsPath()
                execSpec.commandLine("java","-jar",jarToolsPath,"-login",userName,userPassword)
            
        );

        getProject().exec(new Action<ExecSpec>() 
            @Override
            public void execute(ExecSpec execSpec) 
                execSpec.commandLine("java","-jar",jarToolsPath,"-importsign",keyStorePath,keyStorePassword,keyAlias, keyAliasPassword)
            
        );

        getProject().exec(new Action<ExecSpec>() 
            @Override
            public void execute(ExecSpec execSpec) 
                println 'JiaguTask end,mApk path='+mApk.getAbsolutePath()
                println 'JiaguTask end,mApk Parent='+mApk.getParent()
                execSpec.commandLine("java","-jar",jarToolsPath,"-jiagu",mApk.getAbsolutePath(),mApk.getParent(),"-autosign");
            
        );
    

插件任务类2JiaguTask.groovy(1和2选一即可,只是使用的拓展属性方式不一样)

package com.shan.plugin;

import org.gradle.api.Action;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;
import org.gradle.process.ExecSpec;

import java.io.File;

import javax.inject.Inject;

/**
 * Author by wuyishan, Date on 2022/1/20.
 */
class JiaguTask extends DefaultTask 
    def mApk
    private final ConfigBean mConfigExtension

    @Inject
    JiaguTask(File mApk, ConfigBean mConfigExtension) 
        setGroup("jiagu")
        println 'JiaguTask start,path:'+mApk.getAbsolutePath()
        println 'JiaguTask start,mConfigExtension:'+mConfigExtension.toString()
        this.mApk = mApk
        this.mConfigExtension = mConfigExtension
    

    @TaskAction
    void doAction() 
        println 'JiaguTask is running,mConfigExtension='+mConfigExtension.toString()
        getProject().exec(new Action<ExecSpec>() 
            @Override
            public void execute(ExecSpec execSpec) 
                // execSpec.commandLine("java","-jar",jarToolsPath,"-login",userName,userPassword)
                execSpec.commandLine("java","-jar",project.extensions.mConfigExtension.jarToolsPath,"-login",project.extensions.mConfigExtension.userName,project.extensions.mConfigExtension.userPassword)
            
        );

        getProject().exec(new Action<ExecSpec>() 
            @Override
            public void execute(ExecSpec execSpec) 
                execSpec.commandLine("java","-jar",project.extensions.mConfigExtension.jarToolsPath,"-importsign",project.extensions.mConfigExtension.keyStorePath,project.extensions.mConfigExtension.keyStorePassword,project.extensions.mConfigExtension.keyAlias, project.extensions.mConfigExtension.keyAliasPassword)
            
        );

        getProject().exec(new Action<ExecSpec>() 
            @Override
            public void execute(ExecSpec execSpec) 
                println 'JiaguTask end,mApk path='+mApk.getAbsolutePath()
                println 'JiaguTask end,jarToolsPath='+project.extensions.mConfigExtension.jarToolsPath
                execSpec.commandLine("java","-jar",project.extensions.mConfigExtension.jarToolsPath,"-jiagu",mApk.getAbsolutePath(),mApk.getParent(),"-autosign");
            
        );
    


在插件module的main目录下新建resources目录,在resources目录新建META-INF目录,在META-INF目录新建gradle-plugins目录,在gradle-plugins目录下面新建文件com.shan.plugin.properties,前面的com.shan.plugin是包名,后面在app module的build.gradle文件通过apply plugin: 'com.shan.plugin’来调用插件。com.shan.plugin.properties内容显示了插件的包名以及类名

implementation-class=com.shan.plugin.JiaguPlugin

1.3插件build.gradle发布配置

//build.gradle
plugins 
    id 'groovy'


dependencies 
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation gradleApi()
    //implementation 'com.android.tools.build:gradle:7.0.2'
    implementation 'com.android.tools.build:gradle:4.1.0'


apply plugin: 'maven-publish'

publishing
    publications
        JiaguPlugin(MavenPublication)
            from components.java
            groupId 'com.shan'
            artifactId "jiagu"
            version '1.0'
        
    
/*    repositories 
        maven 
            url "../repo"
        
    */

这样配置以后就可以在android studio右侧的gradle task中显示出publishing目录,我们双机publishJiaguPluginPublicationToMavenLocal,就可以将插件打包发布的本地仓库C:\\Users\\userName.m2\\repository路径下,如果想修改保存位置,可以通过maven.url属性设置。

1.4根目录build.gradle配置

生成了插件以后就可以使用了,在根目录的build.gradle中通过groupId、artifactId、version来指定插件以及版本

//build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript 
    repositories 
        /*maven 
            url "/repo"
        */
        mavenLocal()
        mavenCentral()
        google()
    
    dependencies 
        classpath "com.android.tools.build:gradle:7.0.2"
        classpath "com.shan:jiagu:1.0"
    


task clean(type: Delete) 
    delete rootProject.buildDir

1.5app module中build.gradle配置

app module中build.gradle需要配置一些360打包所需要的参数,也就是ConfigBean.groovy中定义的一些属性需要值。

此时在在android studio右侧的app gradle task中显示出jiagu分组,先编译出apk后双机jiagu任务或者在命令行中通过gradlew jiagu命令就可以完成apk的加固

二、踩坑

2.1.Could not resolve com.android.tools.build:gradle:7.0.2

插件module同步gradle报错

dependencies 
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation gradleApi()
    //implementation 'com.android.tools.build:gradle:7.0.2'
    implementation 'com.android.tools.build:gradle:4.1.0'

解决方案:使用老版本implementation ‘com.android.tools.build:gradle:4.1.0’

2.2.Build was configured to prefer settings repositories over project repositories but repository ‘Gradle Libs’ was added by unknown code

插件module同步gradle报错
解决方案:删掉根目录settings.gradle文件中的repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS),该属性是最新as版本新建工程自动添加的

2.3.不显示Gradle task



解决方案:先在设置中Experimental选项中去掉“Do not build Gradle list during Gradle sync",然后同步一下Gradle

2.4.app module找不到自定义的插件

自定义插件发布到本地仓库后可以在根目录的build.gradle中通过classpath "com.shan:jiagu:1.0"进行依赖,但是在app module的build.gradle文件通过apply plugin: 'com.shan.plugin’去引用时提示找不到
解决方案:可以参考https://www.jianshu.com/p/cce4201a3a2c,本人是由于插件module的resources目录和groovy目录不在同一个目录导致的。

2.5.class com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated cannot be cast to class

com.android.build.api.variant.ApplicationVariant (com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated and com.android.build.api.variant.ApplicationVariant are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @736a0d51)

解决方案:由于gradle:7.0.2不支持com.android.build.gradle.api.BaseVariantOutput类, 版本太高不支持,降低版本

2.6、Cannot run program “E:\\study\\praticeItems\\GradleApp\\app\\java\\bin\\keytool.exe”: CreateProcess error=2, 系统找不到指定的文件


参考360加固文档说明,命令加固模式需要在其提供的指定的java目录去执行,因为在该目录有keytool.exe、aapt.exe、aapt2.exe这几个工具

2.7、Cannot run program “E:\\study\\praticeItems\\GradleApp\\app\\java\\bin\\aapt.exe”: CreateProcess error=2, 系统找不到指定的文件。

Cannot run program “E:\\study\\praticeItems\\GradleApp\\app\\java\\bin\\aapt2.exe”: CreateProcess error=2, 系统找不到指定的文件。
解决方案:同6

以上是关于AS Arctic Fox配置360加固命令行模式gradle插件的主要内容,如果未能解决你的问题,请参考以下文章

AS Arctic Fox配置360加固命令行模式gradle插件

Android Studio Arctic Fox 2021.3.1版本部分Bug

Android Studio Arctic Fox的Git Log和搜索窗口中文乱码问题解决方案(关键词:AS,2020.3.1,显示错误,Find in Path,版块,方块,修复,新版本)(代码片

Android Studio Arctic Fox | 2020.3.1Gradle 7.0升级记录

Android Studio Arctic Fox | 2020.3.1Gradle 7.0升级记录

Android Studio Arctic Fox | 2020.3.1Gradle 7.0升级记录