├── settings.gradle
├── example-android
├── aar-lib
│ ├── src
│ │ └── main
│ │ │ ├── assets
│ │ │ └── aar-lib-asset.txt
│ │ │ ├── res
│ │ │ ├── values
│ │ │ │ └── strings.xml
│ │ │ └── layout
│ │ │ │ └── activity_aar_lib.xml
│ │ │ ├── AndroidManifest.xml
│ │ │ └── java
│ │ │ └── me
│ │ │ └── vigi
│ │ │ └── fataar
│ │ │ └── demo
│ │ │ └── aarLib
│ │ │ └── AarLibActivity.java
│ ├── build.gradle
│ └── proguard-rules.pro
├── settings.gradle
├── library
│ ├── src
│ │ └── main
│ │ │ ├── res
│ │ │ ├── values
│ │ │ │ └── strings.xml
│ │ │ └── layout
│ │ │ │ └── activity_lib.xml
│ │ │ ├── AndroidManifest.xml
│ │ │ └── java
│ │ │ └── me
│ │ │ └── vigi
│ │ │ └── fataar
│ │ │ └── demo
│ │ │ └── lib
│ │ │ └── LibActivity.java
│ ├── libs
│ │ └── commons-lang-2.6.jar
│ ├── proguard-rules.pro
│ └── build.gradle
├── gradle
│ └── wrapper
│ │ ├── gradle-wrapper.jar
│ │ └── gradle-wrapper.properties
├── app
│ ├── src
│ │ └── main
│ │ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ └── layout
│ │ │ │ └── activity_main.xml
│ │ │ ├── AndroidManifest.xml
│ │ │ └── java
│ │ │ └── me
│ │ │ └── vigi
│ │ │ └── fataar
│ │ │ └── demo
│ │ │ └── MainActivity.java
│ ├── proguard-rules.pro
│ └── build.gradle
├── java-lib
│ ├── build.gradle
│ └── src
│ │ └── main
│ │ └── java
│ │ └── me
│ │ └── vigi
│ │ └── fataar
│ │ └── demo
│ │ └── javaLib
│ │ └── JavaLibClass.java
├── build.gradle
├── gradle.properties
├── gradlew.bat
└── gradlew
├── gradle.properties
├── src
└── main
│ ├── resources
│ └── META-INF
│ │ └── gradle-plugins
│ │ └── me.vigi.fat-aar.properties
│ ├── java
│ └── me
│ │ └── vigi
│ │ └── fataar
│ │ ├── AndroidPluginHelper.java
│ │ ├── AndroidArchiveLibrary.java
│ │ └── RSourceGenerator.java
│ └── groovy
│ └── me
│ └── vigi
│ └── fataar
│ ├── ExplodedHelper.groovy
│ ├── FatLibraryPlugin.groovy
│ └── VariantProcessor.groovy
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── LICENSE.txt
├── publish.gradle
├── gradlew.bat
├── README.md
└── gradlew
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name = 'fat-aar-plugin'
--------------------------------------------------------------------------------
/example-android/aar-lib/src/main/assets/aar-lib-asset.txt:
--------------------------------------------------------------------------------
1 | aar-lib-asset
--------------------------------------------------------------------------------
/example-android/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':library', ':aar-lib', ':java-lib'
2 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | GROUP=me.vigi
2 | ARTIFACT_ID=fat-aar-plugin
3 | VERSION_NAME=0.2.8
--------------------------------------------------------------------------------
/src/main/resources/META-INF/gradle-plugins/me.vigi.fat-aar.properties:
--------------------------------------------------------------------------------
1 | implementation-class=me.vigi.fataar.FatLibraryPlugin
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/vigidroid/fat-aar-plugin/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/example-android/aar-lib/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | finish
3 |
4 |
--------------------------------------------------------------------------------
/example-android/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | button
3 |
4 |
--------------------------------------------------------------------------------
/example-android/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/vigidroid/fat-aar-plugin/HEAD/example-android/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/example-android/library/libs/commons-lang-2.6.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/vigidroid/fat-aar-plugin/HEAD/example-android/library/libs/commons-lang-2.6.jar
--------------------------------------------------------------------------------
/example-android/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/vigidroid/fat-aar-plugin/HEAD/example-android/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example-android/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/vigidroid/fat-aar-plugin/HEAD/example-android/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example-android/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/vigidroid/fat-aar-plugin/HEAD/example-android/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example-android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/vigidroid/fat-aar-plugin/HEAD/example-android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example-android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/vigidroid/fat-aar-plugin/HEAD/example-android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example-android/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | fat-aar-plugin
3 | Jump to LibActivity
4 |
5 |
--------------------------------------------------------------------------------
/example-android/java-lib/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java'
2 |
3 | dependencies {
4 | compile fileTree(dir: 'libs', include: ['*.jar'])
5 | }
6 |
7 | sourceCompatibility = "1.7"
8 | targetCompatibility = "1.7"
9 |
--------------------------------------------------------------------------------
/example-android/java-lib/src/main/java/me/vigi/fataar/demo/javaLib/JavaLibClass.java:
--------------------------------------------------------------------------------
1 | package me.vigi.fataar.demo.javaLib;
2 |
3 | public class JavaLibClass {
4 |
5 | public static int plus(int a, int b) {
6 | return a + b;
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/example-android/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Jan 14 18:32:47 CST 2017
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-2.5-all.zip
7 |
--------------------------------------------------------------------------------
/example-android/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
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-3.3-all.zip
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Gradle files
2 | .gradle/
3 | build/
4 |
5 | # Local configuration file (sdk path, etc)
6 | local.properties
7 |
8 | # Android Studio Navigation editor temp files
9 | .navigation/
10 |
11 | # Android Studio captures folder
12 | captures/
13 |
14 | # Android Studio other files
15 | .idea/
16 | *.iml
17 |
18 | # project local repository
19 | repo/
--------------------------------------------------------------------------------
/example-android/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/example-android/aar-lib/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/example-android/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/example-android/aar-lib/src/main/java/me/vigi/fataar/demo/aarLib/AarLibActivity.java:
--------------------------------------------------------------------------------
1 | package me.vigi.fataar.demo.aarLib;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 |
7 | /**
8 | * Created by Vigi on 2017/1/14.
9 | */
10 |
11 | public class AarLibActivity extends Activity {
12 |
13 | @Override
14 | protected void onCreate(Bundle savedInstanceState) {
15 | super.onCreate(savedInstanceState);
16 | setContentView(R.layout.activity_aar_lib);
17 | }
18 |
19 | public void onFinishClick(View view) {
20 | finish();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/example-android/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
16 |
--------------------------------------------------------------------------------
/example-android/aar-lib/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 |
7 | defaultConfig {
8 | minSdkVersion 8
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | // aar, has dependency, has res, has R.txt, minsdk=10
23 | // compile 'com.pddstudio:encrypted-preferences:1.3.0'
24 | }
25 |
--------------------------------------------------------------------------------
/example-android/aar-lib/src/main/res/layout/activity_aar_lib.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
17 |
--------------------------------------------------------------------------------
/example-android/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/example-android/aar-lib/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/example-android/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/example-android/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | maven {
7 | url "http://dl.bintray.com/vigidroid/maven"
8 | }
9 | // mavenLocal()
10 | }
11 | dependencies {
12 | classpath 'com.android.tools.build:gradle:2.2.3'
13 | classpath 'me.vigi:fat-aar-plugin:0.2.8'
14 |
15 | // NOTE: Do not place your application dependencies here; they belong
16 | // in the individual module build.gradle files
17 | }
18 | }
19 |
20 | allprojects {
21 | repositories {
22 | jcenter()
23 | }
24 | }
25 |
26 | task clean(type: Delete) {
27 | delete rootProject.buildDir
28 | }
29 |
--------------------------------------------------------------------------------
/example-android/app/src/main/java/me/vigi/fataar/demo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package me.vigi.fataar.demo;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.view.View;
8 |
9 | import me.vigi.fataar.demo.lib.LibActivity;
10 |
11 | /**
12 | * Created by Vigi on 2017/1/14.
13 | */
14 |
15 | public class MainActivity extends AppCompatActivity {
16 |
17 | @Override
18 | protected void onCreate(@Nullable Bundle savedInstanceState) {
19 | super.onCreate(savedInstanceState);
20 | setContentView(R.layout.activity_main);
21 | }
22 |
23 | public void onJumpBtnClick(View view) {
24 | startActivity(new Intent(this, LibActivity.class));
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/example-android/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 | defaultConfig {
7 | applicationId "me.vigi.fataar.demo"
8 | minSdkVersion 9
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled true
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | repositories {
22 | maven() {
23 | url uri('../../repo')
24 | }
25 | }
26 |
27 | dependencies {
28 | compile 'com.android.support:appcompat-v7:25.1.1'
29 |
30 | // compile 'me.vigi.fat-aar:demo.lib:1.0'
31 | compile project(':library')
32 | }
33 |
--------------------------------------------------------------------------------
/example-android/library/src/main/res/layout/activity_lib.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
17 |
18 |
22 |
--------------------------------------------------------------------------------
/example-android/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
19 | # If you use android gradle plugin 2.3.0 and higher, uncomment the line below.
20 | # Otherwise fat-aar-plugin will work incorrectly, and result in a build exception.
21 | # android.enableBuildCache=false
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Vigi
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/src/main/java/me/vigi/fataar/AndroidPluginHelper.java:
--------------------------------------------------------------------------------
1 | package me.vigi.fataar;
2 |
3 | import com.google.common.base.Strings;
4 | import org.gradle.api.Project;
5 | import org.gradle.api.Task;
6 | import org.gradle.util.VersionNumber;
7 | import org.joor.Reflect;
8 |
9 | import java.io.File;
10 |
11 | /**
12 | * Created by Vigi on 2017/3/5.
13 | */
14 | public class AndroidPluginHelper {
15 |
16 | /**
17 | * Resolve from com.android.builder.Version#ANDROID_GRADLE_PLUGIN_VERSION
18 | *
19 | * Throw exception if can not found
20 | */
21 | public static String getAndroidPluginVersion() {
22 | return Reflect.on("com.android.builder.Version").get("ANDROID_GRADLE_PLUGIN_VERSION");
23 | }
24 |
25 | /**
26 | * return bundle dir of specific variant
27 | */
28 | public static File resolveBundleDir(Project project, Object variant) {
29 | if (VersionNumber.parse(getAndroidPluginVersion()).compareTo(VersionNumber.parse("2.3.0")) < 0) {
30 | String dirName = Reflect.on(variant).call("getDirName").get();
31 | if (Strings.isNullOrEmpty(dirName)) {
32 | return null;
33 | }
34 | return project.file(project.getBuildDir() + "/intermediates/bundles/" + dirName);
35 | } else {
36 | // do the trick getting assets task output
37 | Task mergeAssetsTask = Reflect.on(variant).call("getMergeAssets").get();
38 | File assetsDir = Reflect.on(mergeAssetsTask).call("getOutputDir").get();
39 | return assetsDir.getParentFile();
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/example-android/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | -renamesourcefileattribute SourceFile
20 | -keepattributes SourceFile,LineNumberTable
21 |
22 | # for guava 20.0
23 | -dontwarn javax.annotation.**
24 | -dontwarn javax.inject.**
25 | -dontwarn sun.misc.Unsafe
26 | -dontwarn java.lang.**
27 | -dontwarn com.google.errorprone.annotations.**
28 | -dontwarn com.google.j2objc.annotations.**
29 | -dontwarn org.codehaus.mojo.**
30 |
31 | # for okhttp
32 | -dontwarn okhttp3.**
33 |
34 | # for rxjava
35 | -keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* {
36 | long producerIndex;
37 | long consumerIndex;
38 | }
39 | -keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
40 | rx.internal.util.atomic.LinkedQueueNode producerNode;
41 | }
42 | -keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef {
43 | rx.internal.util.atomic.LinkedQueueNode consumerNode;
44 | }
45 |
46 | # keep whatever we like
47 | # -keep class com.google.**
48 | # -keep class me.vigi.fataar.**
49 | # -keep class rx.**
50 | # -keep class com.android.volley.**
51 |
52 | # get rid of all duplicate notes
53 | -dontnote me.vigi.fataar.demo.**
54 | -dontnote com.google.**
55 | -dontnote com.android.volley.**
56 | -dontnote rx.**
57 | -dontnote pl.droidsonroids.gif.**
58 |
--------------------------------------------------------------------------------
/example-android/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'me.vigi.fat-aar'
3 |
4 | android {
5 | compileSdkVersion 25
6 | buildToolsVersion "25.0.2"
7 |
8 | defaultConfig {
9 | minSdkVersion 9
10 | targetSdkVersion 25
11 | versionCode 1
12 | versionName '1.0'
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled true
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | // productFlavors {
21 | // flavor1
22 | // }
23 | defaultPublishConfig 'debug'
24 | }
25 |
26 | dependencies {
27 | // aar project
28 | embed project(':aar-lib')
29 | // jar project
30 | embed project(':java-lib')
31 |
32 | // aar, minsdk=9, has res, has proguard.txt, has public.txt, has R.txt
33 | // has dependencies(support-annotations, support-compat, support-core-ui)
34 | // embed 'com.android.support:recyclerview-v7:25.1.1'
35 | // jar, no compile dependency
36 | embed 'com.google.guava:guava:20.0'
37 | // aar, minsdk=9, has dependency(io.reactivex:rxjava:1.1.6)
38 | embed 'io.reactivex:rxandroid:1.2.1'
39 | // jar, no dependency
40 | embed 'io.reactivex:rxjava:1.2.5'
41 | // aar, minsdk=9, has res, has proguard.txt, has R.txt, has jni
42 | embed 'pl.droidsonroids.gif:android-gif-drawable:1.2.5'
43 |
44 | // jar file
45 | compile files('libs/commons-lang-2.6.jar')
46 |
47 | compile 'com.squareup.okhttp3:okhttp:3.6.0'
48 | }
49 |
50 | apply plugin: 'maven'
51 | uploadArchives {
52 | repositories {
53 | mavenDeployer {
54 | pom.groupId = 'me.vigi.fat-aar'
55 | pom.artifactId = 'demo.lib'
56 | pom.version = '1.0'
57 | repository(url: uri('../../repo'))
58 | }
59 | }
60 | }
61 |
62 | project.gradle.addListener(new TaskExecutionListener() {
63 | @Override
64 | void beforeExecute(Task task) {}
65 |
66 | @Override
67 | void afterExecute(Task task, TaskState state) {
68 | task.inputs.files.each {
69 | // println ' input=' + it
70 | }
71 | task.outputs.files.each {
72 | // println ' output=' + it
73 | }
74 | }
75 | })
--------------------------------------------------------------------------------
/publish.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'maven'
2 | apply plugin: 'com.jfrog.bintray'
3 |
4 | def projectDesc = 'This is a gradle plugin that helps to output fat aar from android library.'
5 | def projectGitUrl = 'https://github.com/Vigi0303/fat-aar-plugin'
6 | def projectScmConnection = 'scm:git@github.com:Vigi0303/fat-aar-plugin.git'
7 | def projectIssueUrl = 'https://github.com/Vigi0303/fat-aar-plugin/issues'
8 |
9 | install {
10 | repositories.mavenInstaller {
11 | pom.groupId = GROUP
12 | pom.artifactId = ARTIFACT_ID
13 | pom.version = VERSION_NAME
14 |
15 | pom.project {
16 | name 'fat aar plugin'
17 | description projectDesc
18 | url projectGitUrl
19 |
20 | scm {
21 | url projectGitUrl
22 | connection projectScmConnection
23 | developerConnection projectScmConnection
24 | }
25 |
26 | developers {
27 | developer {
28 | id 'vigidroid'
29 | name 'Vigi'
30 | email 'vigidroid@gmail.com'
31 | }
32 | }
33 | }
34 | }
35 | }
36 |
37 | task sourcesJar(type: Jar) {
38 | classifier = 'sources'
39 | group BasePlugin.BUILD_GROUP
40 | description = 'Assembles a jar archive containing the main sources of this project.'
41 | from sourceSets.main.allSource
42 | }
43 |
44 | task javadocJar(type: Jar) {
45 | classifier = 'javadoc'
46 | group BasePlugin.BUILD_GROUP
47 | description = 'Assembles a jar archive containing the generated Javadoc API documentation of this project.'
48 | from groovydoc
49 | }
50 |
51 | artifacts {
52 | archives sourcesJar
53 | // archives javadocJar
54 | }
55 |
56 | bintray {
57 | user = project.hasProperty('bintrayUser') ? project.property('bintrayUser') : System.getenv('BINTRAY_USER')
58 | key = project.hasProperty('bintrayApiKey') ? project.property('bintrayApiKey') : System.getenv('BINTRAY_API_KEY')
59 | publish = true
60 | configurations = ['archives']
61 | pkg {
62 | repo = 'maven'
63 | name = project.name
64 | desc = projectDesc
65 | websiteUrl = projectGitUrl
66 | vcsUrl = projectGitUrl
67 | issueTrackerUrl = projectIssueUrl
68 | licenses = ['MIT']
69 | labels = ['gradle-plugin', 'android']
70 | }
71 | }
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/example-android/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 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
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 Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/example-android/library/src/main/java/me/vigi/fataar/demo/lib/LibActivity.java:
--------------------------------------------------------------------------------
1 | package me.vigi.fataar.demo.lib;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 | import android.widget.Button;
7 | import android.widget.TextView;
8 |
9 | import com.google.common.base.Joiner;
10 | import com.google.common.collect.Lists;
11 |
12 | import org.apache.commons.lang.StringUtils;
13 |
14 | import java.io.IOException;
15 | import java.util.List;
16 |
17 | import pl.droidsonroids.gif.GifDrawable;
18 | import rx.Observable;
19 | import rx.android.schedulers.AndroidSchedulers;
20 | import rx.functions.Action1;
21 | import rx.functions.Func1;
22 | import rx.schedulers.Schedulers;
23 |
24 | /**
25 | * Created by Vigi on 2017/1/14.
26 | */
27 |
28 | public class LibActivity extends Activity {
29 |
30 | Button mButton;
31 | TextView mContentText;
32 |
33 | @Override
34 | protected void onCreate(Bundle savedInstanceState) {
35 | super.onCreate(savedInstanceState);
36 | setContentView(R.layout.activity_lib);
37 | mButton = (Button) findViewById(R.id.lib_bt);
38 | mContentText = (TextView) findViewById(R.id.lib_tv);
39 |
40 | // use android-gif-drawable
41 | try {
42 | new GifDrawable("test");
43 | } catch (IOException ignored) {}
44 | mContentText.append("Have android-gif-drawable\n");
45 |
46 | // use guava
47 | Lists.newArrayList();
48 | mContentText.append("Have guava\n");
49 |
50 | // use java-lib
51 | // mContentText.append("Have java-lib(project) and output=" + JavaLibClass.plus(1, 2) + "\n");
52 |
53 | // use rxandroid and rxjava
54 | Observable.just("Have", "rxandroid", "and", "rxjava")
55 | .toList()
56 | .map(new Func1, String>() {
57 | @Override
58 | public String call(List strings) {
59 | return Joiner.on(' ').join(strings);
60 | }
61 | })
62 | .subscribeOn(Schedulers.immediate())
63 | .observeOn(AndroidSchedulers.mainThread())
64 | .subscribe(new Action1() {
65 | @Override
66 | public void call(String s) {
67 | mContentText.append(s);
68 | }
69 | });
70 |
71 | // use commons-lang
72 | String[] strArray = new String[]{"Have", "commons-lang(jar file)"};
73 | mContentText.append(StringUtils.join(strArray, ' ') + '\n');
74 | }
75 |
76 | public void onButtonClick(View view) {
77 | // use aar-lib
78 | // mContentText.append("Have aar-lib(project)\n");
79 | // startActivity(new Intent(this, AarLibActivity.class));
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/src/main/groovy/me/vigi/fataar/ExplodedHelper.groovy:
--------------------------------------------------------------------------------
1 | package me.vigi.fataar
2 |
3 | import org.gradle.api.Project
4 |
5 | /**
6 | * Created by Vigi on 2017/1/20.
7 | */
8 | class ExplodedHelper {
9 |
10 | public static void processIntoJars(Project project,
11 | Collection androidLibraries, Collection jarFiles,
12 | File folderOut) {
13 | for (androidLibrary in androidLibraries) {
14 | if (!androidLibrary.rootFolder.exists()) {
15 | println 'fat-aar-->[warning]' + androidLibrary.rootFolder + ' not found!'
16 | continue
17 | }
18 | // println 'fat-aar-->copy aar from: ' + androidLibrary.rootFolder
19 | def prefix = androidLibrary.name + '-' + androidLibrary.version
20 | project.copy {
21 | from(androidLibrary.classesJarFile)
22 | into folderOut
23 | rename { prefix + '.jar' }
24 | }
25 | project.copy {
26 | from(androidLibrary.localJars)
27 | into folderOut
28 | rename { prefix + '-' + it }
29 | }
30 | }
31 | for (jarFile in jarFiles) {
32 | if (!jarFile.exists()) {
33 | println 'fat-aar-->[warning]' + jarFile + ' not found!'
34 | continue
35 | }
36 | // println 'fat-aar-->copy jar from: ' + jarFile
37 | project.copy {
38 | from(jarFile)
39 | into folderOut
40 | }
41 | }
42 | }
43 |
44 | public static void processIntoClasses(Project project,
45 | Collection androidLibraries, Collection jarFiles,
46 | File folderOut) {
47 | Collection allJarFiles = new ArrayList<>()
48 | for (androidLibrary in androidLibraries) {
49 | if (!androidLibrary.rootFolder.exists()) {
50 | println 'fat-aar-->[warning]' + androidLibrary.rootFolder + ' not found!'
51 | continue
52 | }
53 | allJarFiles.add(androidLibrary.classesJarFile)
54 | allJarFiles.addAll(androidLibrary.localJars)
55 | }
56 | for (jarFile in jarFiles) {
57 | if (!jarFile.exists()) {
58 | println 'fat-aar-->[warning]' + jarFile + ' not found!'
59 | continue
60 | }
61 | allJarFiles.add(jarFile)
62 | }
63 | for (jarFile in allJarFiles) {
64 | // println 'fat-aar-->copy classes from: ' + jarFile
65 | project.copy {
66 | from project.zipTree(jarFile)
67 | into folderOut
68 | include '**/*.class'
69 | exclude 'META-INF/'
70 | }
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/main/java/me/vigi/fataar/AndroidArchiveLibrary.java:
--------------------------------------------------------------------------------
1 | package me.vigi.fataar;
2 |
3 | import org.gradle.api.Project;
4 | import org.gradle.api.artifacts.ModuleVersionIdentifier;
5 | import org.gradle.api.artifacts.ResolvedArtifact;
6 |
7 | import java.io.File;
8 | import java.util.ArrayList;
9 | import java.util.Collection;
10 | import java.util.List;
11 |
12 | /**
13 | * Created by Vigi on 2017/2/16.
14 | *
15 | * @see com.android.builder.dependency.LibraryDependency
16 | */
17 | public class AndroidArchiveLibrary {
18 |
19 | private final Project mProject;
20 |
21 | private final ResolvedArtifact mArtifact;
22 |
23 | public AndroidArchiveLibrary(Project project, ResolvedArtifact artifact) {
24 | if (!"aar".equals(artifact.getType())) {
25 | throw new IllegalArgumentException("artifact must be aar type!");
26 | }
27 | mProject = project;
28 | mArtifact = artifact;
29 | }
30 |
31 | public String getGroup() {
32 | return mArtifact.getModuleVersion().getId().getGroup();
33 | }
34 |
35 | public String getName() {
36 | return mArtifact.getModuleVersion().getId().getName();
37 | }
38 |
39 | public String getVersion() {
40 | return mArtifact.getModuleVersion().getId().getVersion();
41 | }
42 |
43 | public File getRootFolder() {
44 | File explodedRootDir = mProject.file(mProject.getBuildDir() + "/intermediates" + "/exploded-aar/");
45 | ModuleVersionIdentifier id = mArtifact.getModuleVersion().getId();
46 | return mProject.file(explodedRootDir + "/" + id.getGroup() + "/" + id.getName() + "/" + id.getVersion());
47 | }
48 |
49 | private File getJarsRootFolder() {
50 | return new File(getRootFolder(), "jars");
51 | }
52 |
53 | public File getAidlFolder() {
54 | return new File(getRootFolder(), "aidl");
55 | }
56 |
57 | public File getAssetsFolder() {
58 | return new File(getRootFolder(), "assets");
59 | }
60 |
61 | public File getClassesJarFile() {
62 | return new File(getJarsRootFolder(), "classes.jar");
63 | }
64 |
65 | public Collection getLocalJars() {
66 | List localJars = new ArrayList<>();
67 | File[] jarList = new File(getJarsRootFolder(), "libs").listFiles();
68 | if (jarList != null) {
69 | for (File jars : jarList) {
70 | if (jars.isFile() && jars.getName().endsWith(".jar")) {
71 | localJars.add(jars);
72 | }
73 | }
74 | }
75 |
76 | return localJars;
77 | }
78 |
79 | public File getJniFolder() {
80 | return new File(getRootFolder(), "jni");
81 | }
82 |
83 | public File getResFolder() {
84 | return new File(getRootFolder(), "res");
85 | }
86 |
87 | public File getManifest() {
88 | return new File(getRootFolder(), "AndroidManifest.xml");
89 | }
90 |
91 | public File getLintJar() {
92 | return new File(getJarsRootFolder(), "lint.jar");
93 | }
94 |
95 | public File getProguardRules() {
96 | return new File(getRootFolder(), "proguard.txt");
97 | }
98 |
99 | public File getSymbolFile() {
100 | return new File(getRootFolder(), "R.txt");
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/src/main/groovy/me/vigi/fataar/FatLibraryPlugin.groovy:
--------------------------------------------------------------------------------
1 | package me.vigi.fataar
2 |
3 | import org.gradle.api.Plugin
4 | import org.gradle.api.Project
5 | import org.gradle.api.ProjectConfigurationException
6 | import org.gradle.api.artifacts.Configuration
7 | import org.gradle.api.artifacts.DependencyResolutionListener
8 | import org.gradle.api.artifacts.ResolvableDependencies
9 | import org.gradle.api.artifacts.ResolvedArtifact
10 |
11 | /**
12 | *
13 | * Created by Vigi on 2017/1/14.
14 | */
15 | class FatLibraryPlugin implements Plugin {
16 |
17 | private Project project
18 | private Configuration embedConf
19 |
20 | private Set artifacts
21 |
22 | @Override
23 | void apply(Project project) {
24 | this.project = project
25 | checkAndroidPlugin()
26 | createConfiguration()
27 | project.afterEvaluate {
28 | resolveArtifacts()
29 | project.android.libraryVariants.all { variant ->
30 | processVariant(variant)
31 | }
32 | }
33 | }
34 |
35 | private void checkAndroidPlugin() {
36 | if (!project.plugins.hasPlugin('com.android.library')) {
37 | throw new ProjectConfigurationException('fat-aar-plugin must be applied in project that' +
38 | ' has android library plugin!', null)
39 | }
40 | }
41 |
42 | private void createConfiguration() {
43 | embedConf = project.configurations.create('embed')
44 | embedConf.visible = false
45 | project.gradle.addListener(new DependencyResolutionListener() {
46 | @Override
47 | void beforeResolve(ResolvableDependencies resolvableDependencies) {
48 | embedConf.dependencies.each { dependency ->
49 | /**
50 | * use provided instead of compile.
51 | * advantage:
52 | * 1. prune dependency node in generated pom file when upload aar library archives.
53 | * 2. make invisible to the android application module, thus to avoid some duplicated processes.
54 | * side effect:
55 | * 1. [Fixed]incorrect R.txt in bundle. I fixed it by another way.
56 | * 2. [Fixed]loss R.java that is supposed to be generated. I make it manually.
57 | * 3. [Fixed]proguard.txt of embedded dependency is excluded when proguard.
58 | * 4. any other...
59 | */
60 | project.dependencies.add('provided', dependency)
61 | }
62 | project.gradle.removeListener(this)
63 | }
64 |
65 | @Override
66 | void afterResolve(ResolvableDependencies resolvableDependencies) {}
67 | })
68 | }
69 |
70 | private void resolveArtifacts() {
71 | def set = new HashSet<>()
72 | embedConf.resolvedConfiguration.resolvedArtifacts.each { artifact ->
73 | // jar file wouldn't be here
74 | if ('aar'.equals(artifact.type) || 'jar'.equals(artifact.type)) {
75 | println 'fat-aar-->[embed detected][' + artifact.type + ']' + artifact.moduleVersion.id
76 | } else {
77 | throw new ProjectConfigurationException('Only support embed aar and jar dependencies!', null)
78 | }
79 | set.add(artifact)
80 | }
81 | artifacts = Collections.unmodifiableSet(set)
82 | }
83 |
84 | private void processVariant(variant) {
85 | def processor = new VariantProcessor(project, variant)
86 | for (artifact in artifacts) {
87 | if ('aar'.equals(artifact.type)) {
88 | AndroidArchiveLibrary archiveLibrary = new AndroidArchiveLibrary(project, artifact)
89 | processor.addAndroidArchiveLibrary(archiveLibrary)
90 | }
91 | if ('jar'.equals(artifact.type)) {
92 | processor.addJarFile(artifact.file)
93 | }
94 | }
95 | processor.processVariant()
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Deprecated
2 |
3 | **This repo is Deprecated. As the version of android gradle plugin keep increasing, I don't have enough time to keep supporting. And it stopped at `0.2.8`, which supports android plugin at [`2.2.3` and prior](https://developer.android.com/studio/releases/gradle-plugin#2-2-0).**
4 |
5 | # fat-aar-plugin
6 |
7 | [  ](https://bintray.com/vigidroid/maven/fat-aar-plugin/_latestVersion)
8 |
9 | This is a gradle plugin that helps to output fat aar from android library. I am inspired by [android-fat-aar][1]. And aim to make more flexible and functional. It's convenient to **sdk developer**(developer that provide a single aar library).
10 |
11 | It works with [the android gradle plugin][3], the android plugin's version of the development is `2.2.3`, other revision is not tested actually. Commit an issue as you encounter some compatibility.
12 |
13 | **Update: Android plugin version 2.3.0 and later, is not well supported. [disable build-cache][4] may do the trick. So the recommend version is 2.2.3 and prior. Look [this issue][6] for more.**
14 |
15 | Essentially, `fat-aar-plugin` makes a hack way, to collect resources, jar files and something others in embedded dependencies, into the bundled output aar. Click [here](#about-aar-file) to know more about `AAR`.
16 |
17 | **[Features]**
18 | * Support embed `android library project`, `java project`, `android library and java library from maven repositories`. Local jar file is not needed to use `embed`, `compile` is enough.
19 | * Work fine with the original features provided by `android plugin`. Such as multi build type, product flavor, manifest placeholder, proguard... If you find something wrong, commit an issue.
20 | * The jar files in embedded dependencies will be bundled into `libs\` in aar when the proguard is off. Otherwise , they will be bundled into `classes.jar` in aar, it means classes in dependencies will also be obfuscated.
21 |
22 | ## Getting Started
23 |
24 | #### Step 1: Apply plugin
25 |
26 | Add snippet below to your root build script file:
27 |
28 | ```gradle
29 | buildscript {
30 | repositories {
31 | maven {
32 | url "http://dl.bintray.com/vigidroid/maven"
33 | }
34 | }
35 | dependencies {
36 | classpath 'com.android.tools.build:gradle:xxx'
37 | classpath 'me.vigi:fat-aar-plugin:0.2.8'
38 | }
39 | }
40 | ```
41 |
42 | Add snippet below to the `build.gradle` of your android library:
43 |
44 | ```gradle
45 | apply plugin: 'me.vigi.fat-aar'
46 | ```
47 |
48 | #### Step 2: Embed dependencies
49 |
50 | change `compile` to `embed` while you want to embed the dependency in the library. Like this:
51 |
52 | ```gradle
53 | dependencies {
54 | // aar project
55 | embed project(':aar-lib')
56 | // java project
57 | embed project(':java-lib')
58 | // java dependency
59 | embed 'com.google.guava:guava:20.0'
60 | // aar dependency
61 | embed 'com.android.volley:volley:1.0.0'
62 |
63 | // other dependencies you don't want to embed in
64 | compile 'com.squareup.okhttp3:okhttp:3.6.0'
65 | }
66 | ```
67 |
68 | **More usage see [example](./example-android).**
69 |
70 | ## About AAR File
71 |
72 | AAR is a file format for android library.
73 | The file itself is a zip file that containing useful stuff in android.
74 | See [anatomy of an aar file here][2].
75 |
76 | **support list for now:**
77 |
78 | - [x] manifest merge
79 | - [x] classes jar and external jars merge
80 | - [x] res merge
81 | - [x] R.txt merge
82 | - [x] assets merge
83 | - [x] jni libs merge
84 | - [x] proguard.txt merge
85 | - [ ] lint.jar merge
86 | - [ ] aidl merge?
87 | - [ ] public.txt merge?
88 |
89 | ## Known Defects or Issues
90 |
91 | * **Proguard note.** Produce lots of(maybe) `Note: duplicate definition of library class`, while proguard is on. A workaround is to add `-dontnote` in `proguard-rules.pro`.
92 | * **The overlay order of res merge is changed:** Embedded dependency has higher priority than other dependencies.
93 | * **Res merge conflicts.** If the library res folder and embedded dependencies res have the same res Id(mostly `string/app_name`). A duplicate resources build exception will be thrown. To avoid res conflicts, consider using a prefix to each res Id, both in library res and aar dependencies if possible.
94 | * [More issue or defect][5] is welcomed...
95 |
96 | ## Thanks
97 | [android-fat-aar][1]
98 |
99 | [1]: https://github.com/adwiv/android-fat-aar
100 | [2]: https://developer.android.com/studio/projects/android-library.html#aar-contents
101 | [3]: https://developer.android.com/studio/releases/gradle-plugin.html
102 | [4]: https://developer.android.com/studio/build/build-cache.html#disable_build_cache
103 | [5]: https://github.com/Vigi0303/fat-aar-plugin/issues
104 | [6]: https://github.com/Vigi0303/fat-aar-plugin/issues/4
105 |
--------------------------------------------------------------------------------
/src/main/java/me/vigi/fataar/RSourceGenerator.java:
--------------------------------------------------------------------------------
1 | package me.vigi.fataar;
2 |
3 | import com.google.common.base.Charsets;
4 | import com.google.common.base.Strings;
5 | import com.google.common.collect.Lists;
6 | import com.google.common.collect.Maps;
7 | import com.google.common.io.Files;
8 | import com.squareup.javapoet.FieldSpec;
9 | import com.squareup.javapoet.JavaFile;
10 | import com.squareup.javapoet.TypeName;
11 | import com.squareup.javapoet.TypeSpec;
12 | import org.w3c.dom.Document;
13 | import org.w3c.dom.Element;
14 |
15 | import javax.lang.model.element.Modifier;
16 | import javax.xml.parsers.DocumentBuilderFactory;
17 | import java.io.File;
18 | import java.io.IOException;
19 | import java.util.List;
20 | import java.util.Map;
21 |
22 | /**
23 | * Created by Vigi on 2017/2/28.
24 | */
25 | public class RSourceGenerator {
26 |
27 | public static void generate(File outputDir, AndroidArchiveLibrary androidLibrary) throws IOException {
28 | // check
29 | File symbolFile = androidLibrary.getSymbolFile();
30 | File manifestFile = androidLibrary.getManifest();
31 | if (!symbolFile.exists()) {
32 | return;
33 | }
34 | if (!manifestFile.exists()) {
35 | throw new RuntimeException("Can not find " + manifestFile);
36 | }
37 | // read R.txt
38 | List lines = Files.readLines(symbolFile, Charsets.UTF_8);
39 | Map> symbolItemsMap = Maps.newHashMap();
40 | for (String line : lines) {
41 | String[] strings = line.split(" ", 4);
42 | TextSymbolItem symbolItem = new TextSymbolItem();
43 | symbolItem.type = strings[0];
44 | symbolItem.clazz = strings[1];
45 | symbolItem.name = strings[2];
46 | symbolItem.value = strings[3];
47 | List symbolItems = symbolItemsMap.get(symbolItem.clazz);
48 | if (symbolItems == null) {
49 | symbolItems = Lists.newArrayList();
50 | symbolItemsMap.put(symbolItem.clazz, symbolItems);
51 | }
52 | symbolItems.add(symbolItem);
53 | }
54 | if (symbolItemsMap.isEmpty()) {
55 | // empty R.txt
56 | return;
57 | }
58 | // parse package name
59 | String packageName = null;
60 | try {
61 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
62 | Document doc = dbf.newDocumentBuilder().parse(manifestFile);
63 | Element element = doc.getDocumentElement();
64 | packageName = element.getAttribute("package");
65 | } catch (Exception ignored) {}
66 | if (Strings.isNullOrEmpty(packageName)) {
67 | throw new RuntimeException("Parse package from " + manifestFile + " error!");
68 | }
69 |
70 | // write R.java
71 | TypeSpec.Builder classBuilder = TypeSpec.classBuilder("R")
72 | .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
73 | .addJavadoc("AUTO-GENERATED FILE. DO NOT MODIFY.\n")
74 | .addJavadoc("\n")
75 | .addJavadoc("This class was automatically generated by the\n"
76 | + "fat-aar-plugin (https://github.com/Vigi0303/fat-aar-plugin)\n"
77 | + "from the R.txt of the dependency it found.\n"
78 | + "It should not be modified by hand.");
79 | for (String clazz : symbolItemsMap.keySet()) {
80 | TypeSpec.Builder icb = TypeSpec.classBuilder(clazz)
81 | .addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL);
82 | List tsis = symbolItemsMap.get(clazz);
83 | for (TextSymbolItem item : tsis) {
84 | TypeName typeName = null;
85 | if ("int".equals(item.type)) {
86 | typeName = TypeName.INT;
87 | }
88 | if ("int[]".equals(item.type)) {
89 | typeName = TypeName.get(int[].class);
90 | }
91 | if (typeName == null) {
92 | throw new RuntimeException("Unknown class type in " + symbolFile);
93 | }
94 | FieldSpec fieldSpec = FieldSpec.builder(typeName, item.name)
95 | .addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) // Is the "final" necessary?
96 | .initializer(item.value)
97 | .build();
98 | icb.addField(fieldSpec);
99 | }
100 | classBuilder.addType(icb.build());
101 | }
102 | JavaFile javaFile = JavaFile.builder(packageName, classBuilder.build()).build();
103 | javaFile.writeTo(outputDir);
104 | }
105 |
106 | private static class TextSymbolItem {
107 | String type, clazz, name, value;
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/example-android/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
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 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
165 | if [[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]]; then
166 | cd "$(dirname "$0")"
167 | fi
168 |
169 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
170 |
--------------------------------------------------------------------------------
/src/main/groovy/me/vigi/fataar/VariantProcessor.groovy:
--------------------------------------------------------------------------------
1 | package me.vigi.fataar
2 |
3 | import org.gradle.api.Project
4 | import org.gradle.api.Task
5 |
6 | /**
7 | * Created by Vigi on 2017/2/24.
8 | */
9 | class VariantProcessor {
10 |
11 | private final Project mProject
12 |
13 | private final mVariant
14 |
15 | private Collection mAndroidArchiveLibraries = new ArrayList<>()
16 |
17 | private Collection mJarFiles = new ArrayList<>()
18 |
19 | public VariantProcessor(Project project, variant) {
20 | mProject = project
21 | mVariant = variant
22 | }
23 |
24 | public void addAndroidArchiveLibrary(AndroidArchiveLibrary library) {
25 | mAndroidArchiveLibraries.add(library)
26 | }
27 |
28 | public void addJarFile(File jar) {
29 | mJarFiles.add(jar)
30 | }
31 |
32 | public void processVariant() {
33 | String taskPath = 'prepare' + mVariant.name.capitalize() + 'Dependencies'
34 | Task prepareTask = mProject.tasks.findByPath(taskPath)
35 | if (prepareTask == null) {
36 | throw new RuntimeException("Can not find task ${taskPath}!")
37 | }
38 |
39 | processClassesAndJars()
40 |
41 | if (mAndroidArchiveLibraries.isEmpty()) {
42 | return
43 | }
44 | processManifest()
45 | processResourcesAndR()
46 | processRSources()
47 | processAssets()
48 | processJniLibs()
49 | processProguardTxt(prepareTask)
50 | }
51 |
52 | /**
53 | * merge manifest
54 | *
55 | * TODO process each variant.getOutputs()
56 | * TODO "InvokeManifestMerger" deserve more android plugin version check
57 | * TODO add setMergeReportFile
58 | * TODO a better temp manifest file location
59 | */
60 | private void processManifest() {
61 | Class invokeManifestTaskClazz = null
62 | String className = 'com.android.build.gradle.tasks.InvokeManifestMerger'
63 | try {
64 | invokeManifestTaskClazz = Class.forName(className)
65 | } catch (ClassNotFoundException ignored) {}
66 | if (invokeManifestTaskClazz == null) {
67 | throw new RuntimeException("Can not find class ${className}!")
68 | }
69 | Task processManifestTask = mVariant.getOutputs().get(0).getProcessManifest()
70 | def manifestOutput = mProject.file(mProject.buildDir.path + '/intermediates/fat-aar/' + mVariant.dirName + '/AndroidManifest.xml')
71 | File manifestOutputBackup = processManifestTask.getManifestOutputFile()
72 | processManifestTask.setManifestOutputFile(manifestOutput)
73 |
74 | Task manifestsMergeTask = mProject.tasks.create('merge' + mVariant.name.capitalize() + 'Manifest', invokeManifestTaskClazz)
75 | manifestsMergeTask.setVariantName(mVariant.name)
76 | manifestsMergeTask.setMainManifestFile(manifestOutput)
77 | List list = new ArrayList<>()
78 | for (archiveLibrary in mAndroidArchiveLibraries) {
79 | list.add(archiveLibrary.getManifest())
80 | }
81 | manifestsMergeTask.setSecondaryManifestFiles(list)
82 | manifestsMergeTask.setOutputFile(manifestOutputBackup)
83 | manifestsMergeTask.dependsOn processManifestTask
84 | processManifestTask.finalizedBy manifestsMergeTask
85 | }
86 |
87 | private void processClassesAndJars() {
88 | if (mVariant.getBuildType().isMinifyEnabled()) {
89 | for (archiveLibrary in mAndroidArchiveLibraries) {
90 | File thirdProguard = archiveLibrary.proguardRules
91 | if (!thirdProguard.exists()) {
92 | continue
93 | }
94 | mProject.android.getDefaultConfig().proguardFile(thirdProguard)
95 | }
96 | Task javacTask = mVariant.getJavaCompile()
97 | if (javacTask == null) {
98 | // warn: can not find javaCompile task, jack compile might be on.
99 | return
100 | }
101 | javacTask.doLast {
102 | def dustDir = mProject.file(mProject.buildDir.path + '/intermediates/classes/' + mVariant.dirName)
103 | ExplodedHelper.processIntoClasses(mProject, mAndroidArchiveLibraries, mJarFiles, dustDir)
104 | }
105 | } else {
106 | String taskPath = 'transformClassesAndResourcesWithSyncLibJarsFor' + mVariant.name.capitalize()
107 | Task syncLibTask = mProject.tasks.findByPath(taskPath)
108 | if (syncLibTask == null) {
109 | throw new RuntimeException("Can not find task ${taskPath}!")
110 | }
111 | syncLibTask.doLast {
112 | def dustDir = mProject.file(AndroidPluginHelper.resolveBundleDir(mProject, mVariant).path + '/libs')
113 | // FileUtils.cleanDirectory(dustDir)
114 | ExplodedHelper.processIntoJars(mProject, mAndroidArchiveLibraries, mJarFiles, dustDir)
115 | }
116 | }
117 | }
118 |
119 | /**
120 | * merge R.txt(actually is to fix issue caused by provided configuration) and res
121 | *
122 | * Here I have to inject res into "main" instead of "variant.name".
123 | * To avoid the res from embed dependencies being used, once they have the same res Id with main res.
124 | *
125 | * Now the same res Id will cause a build exception: Duplicate resources, to encourage you to change res Id.
126 | * Adding "android.disableResourceValidation=true" to "gradle.properties" can do a trick to skip the exception, but is not recommended.
127 | */
128 | private void processResourcesAndR() {
129 | String taskPath = 'generate' + mVariant.name.capitalize() + 'Resources'
130 | Task resourceGenTask = mProject.tasks.findByPath(taskPath)
131 | if (resourceGenTask == null) {
132 | throw new RuntimeException("Can not find task ${taskPath}!")
133 | }
134 | resourceGenTask.doFirst {
135 | for (archiveLibrary in mAndroidArchiveLibraries) {
136 | mProject.android.sourceSets."main".res.srcDir(archiveLibrary.resFolder)
137 | }
138 | }
139 | }
140 |
141 | /**
142 | * generate R.java
143 | */
144 | private void processRSources() {
145 | Task processResourcesTask = mVariant.getOutputs().get(0).getProcessResources()
146 | processResourcesTask.doLast {
147 | for (archiveLibrary in mAndroidArchiveLibraries) {
148 | RSourceGenerator.generate(processResourcesTask.getSourceOutputDir(), archiveLibrary)
149 | }
150 | }
151 | }
152 |
153 | /**
154 | * merge assets
155 | *
156 | * AaptOptions.setIgnoreAssets and AaptOptions.setIgnoreAssetsPattern will work as normal
157 | */
158 | private void processAssets() {
159 | Task assetsTask = mVariant.getMergeAssets()
160 | if (assetsTask == null) {
161 | throw new RuntimeException("Can not find task in variant.getMergeAssets()!")
162 | }
163 | for (archiveLibrary in mAndroidArchiveLibraries) {
164 | assetsTask.getInputs().dir(archiveLibrary.assetsFolder)
165 | }
166 | assetsTask.doFirst {
167 | for (archiveLibrary in mAndroidArchiveLibraries) {
168 | // the source set here should be main or variant?
169 | mProject.android.sourceSets."main".assets.srcDir(archiveLibrary.assetsFolder)
170 | }
171 | }
172 | }
173 |
174 | /**
175 | * merge jniLibs
176 | */
177 | private void processJniLibs() {
178 | String taskPath = 'merge' + mVariant.name.capitalize() + 'JniLibFolders'
179 | Task mergeJniLibsTask = mProject.tasks.findByPath(taskPath)
180 | if (mergeJniLibsTask == null) {
181 | throw new RuntimeException("Can not find task ${taskPath}!")
182 | }
183 | for (archiveLibrary in mAndroidArchiveLibraries) {
184 | mergeJniLibsTask.getInputs().dir(archiveLibrary.jniFolder)
185 | }
186 | mergeJniLibsTask.doFirst {
187 | for (archiveLibrary in mAndroidArchiveLibraries) {
188 | // the source set here should be main or variant?
189 | mProject.android.sourceSets."main".jniLibs.srcDir(archiveLibrary.jniFolder)
190 | }
191 | }
192 | }
193 |
194 | /**
195 | * merge proguard.txt
196 | */
197 | private void processProguardTxt(Task prepareTask) {
198 | String taskPath = 'merge' + mVariant.name.capitalize() + 'ProguardFiles'
199 | Task mergeFileTask = mProject.tasks.findByPath(taskPath)
200 | if (mergeFileTask == null) {
201 | throw new RuntimeException("Can not find task ${taskPath}!")
202 | }
203 | for (archiveLibrary in mAndroidArchiveLibraries) {
204 | File thirdProguard = archiveLibrary.proguardRules
205 | if (!thirdProguard.exists()) {
206 | continue
207 | }
208 | mergeFileTask.getInputs().file(thirdProguard)
209 | }
210 | mergeFileTask.doFirst {
211 | Collection proguardFiles = mergeFileTask.getInputFiles()
212 | for (archiveLibrary in mAndroidArchiveLibraries) {
213 | File thirdProguard = archiveLibrary.proguardRules
214 | if (!thirdProguard.exists()) {
215 | continue
216 | }
217 | proguardFiles.add(thirdProguard)
218 | }
219 | }
220 | mergeFileTask.dependsOn prepareTask
221 | }
222 | }
223 |
--------------------------------------------------------------------------------