Skip to content

Commit

Permalink
Added Product Flavors support
Browse files Browse the repository at this point in the history
  • Loading branch information
mainlxl committed Mar 23, 2023
1 parent 80e6140 commit 48c9732
Show file tree
Hide file tree
Showing 77 changed files with 1,606 additions and 91 deletions.
8 changes: 2 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.idea/
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
AnalyzeSoPlugin/gradle.properties
25 changes: 24 additions & 1 deletion AnalyzeSoPlugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies {
compileOnly gradleApi()
compileOnly("com.android.tools.build:gradle:${ANDROID_GRADLE_PLUGIN}")
implementation("com.google.code.gson:gson:2.9.0")
implementation 'org.jetbrains.kotlin:kotlin-reflect:1.7.10'
}
repositories {
mavenCentral()
Expand All @@ -43,7 +44,29 @@ publishing {
}
repositories {
maven {
url = file("${project.rootDir}/repos")
url = rootProject.file(REPO_NAME)
}

def localPro = new Properties()
def localProFile = rootProject.file('local.properties')
if (localProFile.exists()) {
localProFile.withReader('UTF-8') { reader ->
localPro.load(reader)
}
}
if (Boolean.parseBoolean(localPro.ENABLE_NEXUS)) {
maven {
allowInsecureProtocol = true
credentials {
username localPro.NEXUS_MAVEN_USERNAME
password localPro.NEXUS_MAVEN_PASSWORD
}
if (version.endsWith('-SNAPSHOT')) {
url = localPro.NEXUS_MAVEN_URL_SNAPSHOT
} else {
url = localPro.NEXUS_MAVEN_URL
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
package com.mainli.plugin

import com.android.build.gradle.AppExtension
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import org.codehaus.groovy.runtime.StringGroovyMethods
import org.gradle.api.Plugin
import org.gradle.api.Project


class AnalyzeSoPlugin : Plugin<Project> {

private val name: String = "mainli"
private lateinit var android: AppExtension
private val name: String = "Analyze So Library"
private lateinit var android: ApplicationAndroidComponentsExtension

override fun apply(project: Project) {
android = project.extensions.getByType(AppExtension::class.java)
android.buildTypes.stream().map { it.name }.forEach {
val variantName = StringGroovyMethods.capitalize(it)
android = project.extensions.getByType(ApplicationAndroidComponentsExtension::class.java)
android.onVariants { variant ->
val variantName = variant.name
val variantNameBycapitalize = StringGroovyMethods.capitalize(variantName)
project.tasks.register(
"analyze${variantName}So",
"analyze${variantNameBycapitalize}So",
AnalyzeSoTask::class.java
) { task ->
task.group = name
task.variantName.set(project.provider { it })
task.reportJSONDir.set(project.layout.buildDirectory.dir("outputs/analyze-so/${it}"))
task.variantName.set(project.provider { variantName })
task.reportJSONDir.set(project.layout.buildDirectory.dir("outputs/analyze-so/${variantName}"))
}
project.tasks.register(
"assembleAnalyze${variantName}So",
"assembleAnalyze${variantNameBycapitalize}So",
AnalyzeSoTask::class.java
) { task ->
task.group = name
task.dependsOn.add("assemble${variantName}")
task.variantName.set(project.provider { it })
task.reportJSONDir.set(project.layout.buildDirectory.dir("outputs/analyze-so/${it}"))
task.dependsOn.add("assemble${variantNameBycapitalize}")
task.variantName.set(project.provider { variantName })
task.reportJSONDir.set(project.layout.buildDirectory.dir("outputs/analyze-so/${variantName}"))
}
}
}
Expand Down
26 changes: 24 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ buildscript {
}
dependencies {
...
classpath("com.github.Android-Mainli:AnalyzeSoPlugin:1.0.2")
classpath("com.github.Android-Mainli:AnalyzeSoPlugin:1.0.3")
}
}
Expand All @@ -28,14 +28,36 @@ apply plugin: "analyze-so"
gradle命令执行

```shell
demo配置了productFlavors可通过如下命令查看不同变体so
./gradlew tasks 查看所有tasks 本插件分组为Analyze So library tasks如下

...
Analyze So Library tasks
------------------------
analyzeFlavors1DebugSo
analyzeFlavors1ReleaseSo
analyzeFlavors2DebugSo
analyzeFlavors2ReleaseSo
assembleAnalyzeFlavors1DebugSo
assembleAnalyzeFlavors1ReleaseSo
assembleAnalyzeFlavors2DebugSo
assembleAnalyzeFlavors2ReleaseSo
...

//挑选一个task进行变体分析 如果之前未执行assemble相关任务时可能获取到的so不全 请改用assemble开头task执行打包并分析
./gradlew :app:analyzeFlavors1DebugSo -q

******************************************************************
如果项目未配置了productFlavors可通过如下命令查看不同变体so
./gradlew :app:analyzeDebugSo -q
./gradlew :app:analyzeReleaseSo -q
//上述两个命令如果之前未执行assemble相关任务时可能获取到的so不全,可以使用如下两个命令 先进行打包再执行分析
./gradlew :app:assembleAnalyzeDebugSo -q
./gradlew :app:assembleAnalyzeReleaseSo -q
```

会在`app/build/outputs/analyze-so/release(debug)`保存一份json输出
会在`app/build/outputs/analyze-so/变体(release、debug、xxxxx)`保存一份json输出

```json
[
{
Expand Down
19 changes: 18 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,30 @@ android {
defaultConfig {
applicationId "com.mainli.demo"
minSdk 21
targetSdk 31
//noinspection ExpiredTargetSdkVersion
targetSdk 28
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

flavorDimensions "flavors"
productFlavors {
flavors1 {
dimension "flavors"
versionName "0.0.1-flavors1"
}
flavors2 {
dimension "flavors"
versionName "0.0.1-flavors2"
}
}

buildTypes {
release {
debuggable true
signingConfig signingConfigs.debug
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
Expand Down Expand Up @@ -49,6 +64,8 @@ dependencies {
implementation 'com.github.bumptech.glide:glide:4.13.0'
implementation 'com.mainli:blur:1.0.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.13.0'
implementation "org.jetbrains.kotlin:kotlin-reflect:1.7.0-Beta@jar"
implementation "com.jcodecraeer:xrecyclerview:1.6.0"
}

ruler {
Expand Down
5 changes: 3 additions & 2 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
*** rewind();
}

# for DexGuard only
#-keepresourcexmlelements manifest/application/meta-data@value=GlideModule
-keep class kotlin.reflect.jvm.** {
*;
}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity
android:name=".MainActivity"
android:name=".RefreshActivity"
android:exported="true"
android:label="@string/app_name">
<intent-filter>
Expand Down
67 changes: 67 additions & 0 deletions app/src/main/java/com/mainli/demo/AA.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.mainli.demo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;

public class AA {
private static volatile boolean kotlin_class_klass_error;
private static volatile Constructor kotlin_kclass_constructor;
private static volatile Method kotlin_kclass_getConstructors;
private static volatile Method kotlin_kfunction_getParameters;
private static volatile Method kotlin_kparameter_getName;
private static boolean kotlin_error;

public static String[] getKoltinConstructorParameters(Class clazz) {
if (kotlin_kclass_constructor == null && !kotlin_class_klass_error) {
try {
Class class_kotlin_kclass = Class.forName("kotlin.reflect.jvm.internal.KClassImpl");
kotlin_kclass_constructor = class_kotlin_kclass.getConstructor(Class.class);
kotlin_kclass_getConstructors = class_kotlin_kclass.getMethod("getConstructors");

Class class_kotlin_kfunction = Class.forName("kotlin.reflect.KFunction");
kotlin_kfunction_getParameters = class_kotlin_kfunction.getMethod("getParameters");

Class class_kotlinn_kparameter = Class.forName("kotlin.reflect.KParameter");
kotlin_kparameter_getName = class_kotlinn_kparameter.getMethod("getName");
} catch (Throwable e) {
kotlin_class_klass_error = true;
}
}

if (kotlin_kclass_constructor == null) {
return null;
}

if (kotlin_error) {
return null;
}

try {
Object constructor = null;
Object kclassImpl = kotlin_kclass_constructor.newInstance(clazz);
Iterable it = (Iterable) kotlin_kclass_getConstructors.invoke(kclassImpl);
for (Iterator iterator = it.iterator(); iterator.hasNext(); iterator.hasNext()) {
Object item = iterator.next();
List parameters = (List) kotlin_kfunction_getParameters.invoke(item);
if (constructor != null && parameters.size() == 0) {
continue;
}
constructor = item;
}

List parameters = (List) kotlin_kfunction_getParameters.invoke(constructor);
String[] names = new String[parameters.size()];
for (int i = 0; i < parameters.size(); i++) {
Object param = parameters.get(i);
names[i] = (String) kotlin_kparameter_getName.invoke(param);
}
return names;
} catch (Throwable e) {
kotlin_error = true;
}

return null;
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/com/mainli/demo/BB.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mainli.demo

import android.util.Log


data class BB(val string: String, val int: Int) {}

fun aa() {
val koltinConstructorParameters =
AA.getKoltinConstructorParameters(MyIotApplianceListPayload::class.java)
Log.println(Log.WARN, "Mainli", "aa: ${koltinConstructorParameters}")
}
Loading

0 comments on commit 48c9732

Please sign in to comment.