Skip to content

Commit

Permalink
【Gradle-12】分析so文件和依赖的关系
Browse files Browse the repository at this point in the history
  • Loading branch information
yechao committed Oct 8, 2023
1 parent 9f01b72 commit 5628fdd
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 148 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.iml
.gradle
/local.properties
.idea/
/.idea/caches
/.idea/libraries
/.idea/modules.xml
Expand Down
5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- [【Gradle-9】Gradle插件发布指南](https://juejin.cn/spost/7280062870669246525)
- [【Gradle-10】不可忽视的构建分析](https://juejin.cn/post/7282150745164005432)
- [【Gradle-11】动态修改VersionName和VersionCode](https://juejin.cn/spost/7282691800858705957)
- [【Gradle-12】分析so文件和依赖的关系]

### 插件使用
step 1
Expand Down
20 changes: 16 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'com.yechaoa.plugin.dependencies'
id 'com.yechaoa.plugin.gradleX'
}

/**
* 想看printDependencies效果:
* 1.先执行./gradlew publish发布本地
* 2.再解开这里和上面插件的注释 //id 'com.yechaoa.plugin.dependencies'
* 2.再解开这里和上面插件的注释 //id 'com.yechaoa.plugin.gradleX'
* 3.还有project > build.gradle里面的classpath
*/
printDependencies {
enable = false
gradleX {
printDependencies = false
analysisSo = true
}

/**
Expand Down Expand Up @@ -56,6 +57,10 @@ android {
versionName getVersionNameByProperty()

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

ndk {
abiFilters 'arm64-v8a', 'armeabi-v7a', 'armeabi-v8a'
}
}

applicationVariants.configureEach { variant ->
Expand Down Expand Up @@ -88,6 +93,8 @@ android {

dependencies {

implementation fileTree(dir: 'libs', include: ['*.aar'])

// implementation project(":plugin")

implementation 'com.squareup.okhttp3:okhttp:4.10.0'
Expand All @@ -113,6 +120,11 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

//音视频终端 SDK
//8.全功能:直播推流(含超低延时直播、RTC连麦)+短视频+播放器+美颜特效
implementation 'com.aliyun.aio:AliVCSDK_Premium:6.4.0'


// 看Gradle源码用
// implementation "com.android.tools.build:gradle:7.4"
}
Expand Down
23 changes: 6 additions & 17 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
/**
* 本地仓库
* 想看printDependencies效果:
* 1.先执行./gradlew publish发布本地
* 2.再解开app > build.gradle里面的插件的注释 //id 'com.yechaoa.plugin.dependencies'
* 3.还有app > build.gradle里面的printDependencies
* 2.再解开app > build.gradle里面的插件的注释 //id 'com.yechaoa.plugin.gradleX'
* 3.还有app > build.gradle里面的gradleX{}
*/
// classpath('com.yechaoa.plugin:dependencies:1.0.0')
classpath('com.yechaoa.plugin:gradleX:1.1.0')
// 远端仓库地址
classpath('com.github.yechaoa.GradleX:dependencies:1.0')
// classpath('com.github.yechaoa.GradleX:dependencies:1.0')
}
}

Expand All @@ -23,15 +23,4 @@ plugins {
id 'com.android.library' version '8.1.1' apply false
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
id "com.dorongold.task-tree" version "2.1.1"
}

//if (hasProperty("isTest")) {
// println("---hasProperty isTest yes")
// if (Boolean.valueOf(getProperty('isTest'))) {
// println("---isTest true")
// } else {
// println("---isTest false")
// }
//} else {
// println("---hasProperty isTest no")
//}
}
45 changes: 22 additions & 23 deletions plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,36 @@ dependencies {
}

group 'com.yechaoa.plugin'
version '1.0.0'
version '1.1.0'

//publishing {
// // 配置Plugin GAV
// publications {
// maven(MavenPublication) {
// groupId = group
// artifactId = 'dependencies'
// version = version
//
// from components.java
// }
// }
// // 配置仓库地址
// repositories {
// maven {
// url layout.buildDirectory.dir("maven-repo")
// }
// }
//}
publishing {
// 配置Plugin GAV
publications {
maven(MavenPublication) {
groupId = group
artifactId = 'gradleX'
version = version

from components.java
}
}
// 配置仓库地址
repositories {
maven {
url layout.buildDirectory.dir("maven-repo")
}
}
}

gradlePlugin {
// 可以有多个plugin配置
plugins {
// register 这个名字可以随便填
dependenciesplugin {
GradleXPlugin {
// 插件id
id = 'com.yechaoa.plugin.dependencies'
id = 'com.yechaoa.plugin.gradleX'
// 插件全路径
implementationClass = "com.yechaoa.plugin.dependencies.DependenciesPlugin"

implementationClass = "com.yechaoa.plugin.base.GradleXPlugin"
}
}
}
115 changes: 115 additions & 0 deletions plugin/src/main/java/com/yechaoa/plugin/base/GradleXPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.yechaoa.plugin.base;


import com.android.build.gradle.AppExtension;
import com.yechaoa.plugin.extension.CommonPluginExtension;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ModuleVersionIdentifier;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
* GitHub : https://github.com/yechaoa
* CSDN : http://blog.csdn.net/yechaoa
* <p>
* Created by yechao on 2023/8/8.
* Describe :
*/

class GradleXPlugin implements Plugin<Project> {

private final String TAG = "GradleXPlugin >>>>> ";

@Override
public void apply(Project project) {
CommonPluginExtension extension = project.getExtensions().create(CommonPluginExtension.NAME_SPACE, CommonPluginExtension.class);

/*
* 扩展的配置要在 project.afterEvaluate 之后获取哦
* 因为配置阶段完成,才能读取参数
* 且配置完成,才能拿到所有的依赖
*/
project.afterEvaluate(pro -> {
if (extension.printDependencies) {
System.out.println(TAG + "已开启依赖打印");
doPrintDependencies(project);
} else {
System.out.println(TAG + "已关闭依赖打印");
}

if (extension.analysisSo) {
System.out.println(TAG + "已开启so打印");
doAnalysisSo(project);
} else {
System.out.println(TAG + "已关闭so打印");
}

});

}

private void doAnalysisSo(Project project) {
AppExtension androidExtension = project.getExtensions().getByType(AppExtension.class);
androidExtension.getApplicationVariants().all(applicationVariant -> {
System.out.println(TAG + "applicationVariant.getName() = " + applicationVariant.getName());

Configuration configuration = project.getConfigurations().getByName(applicationVariant.getName() + "CompileClasspath");
configuration.forEach(file -> {
String fineName = file.getName();
System.out.println(TAG + "fine name = " + fineName);
if (fineName.endsWith(".jar") || fineName.endsWith(".aar")) {
try {
JarFile jarFile = new JarFile(file);
for (Enumeration enums = jarFile.entries(); enums.hasMoreElements(); ) {
JarEntry jarEntry = (JarEntry) enums.nextElement();
if (jarEntry.getName().endsWith(".so")) {
System.out.println(TAG + "----- so name = " + jarEntry.getName());
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
});
}

private void doPrintDependencies(Project project) {
AppExtension androidExtension = project.getExtensions().getByType(AppExtension.class);

androidExtension.getApplicationVariants().all(applicationVariant -> {
// debug/release也可以加配置
System.out.println(TAG + "applicationVariant.getName() = " + applicationVariant.getName());
Configuration configuration = project.getConfigurations().getByName(applicationVariant.getName() + "CompileClasspath");

List<String> androidLibs = new ArrayList<>();
List<String> otherLibs = new ArrayList<>();

// 所有的依赖,包括依赖中的依赖
configuration.getResolvedConfiguration().getLenientConfiguration().getAllModuleDependencies().forEach(resolvedDependency -> {
ModuleVersionIdentifier identifier = resolvedDependency.getModule().getId();
if (identifier.getGroup().contains("androidx") || identifier.getGroup().contains("com.google") || identifier.getGroup().contains("org.jetbrains")) {
androidLibs.add(identifier.getGroup() + ":" + identifier.getName() + ":" + identifier.getVersion());
} else {
otherLibs.add(identifier.getGroup() + ":" + identifier.getName() + ":" + identifier.getVersion());
}
});

System.out.println("--------------官方库 start--------------");
androidLibs.forEach(System.out::println);
System.out.println("--------------官方库 end--------------");

System.out.println("--------------三方库 start--------------");
otherLibs.forEach(System.out::println);
System.out.println("--------------三方库 end--------------");
});
}
}

This file was deleted.

Loading

0 comments on commit 5628fdd

Please sign in to comment.