Skip to content

Commit

Permalink
Merge branch 'dev_v2'
Browse files Browse the repository at this point in the history
  • Loading branch information
2BAB committed Nov 21, 2018
2 parents 21f19c0 + 0f1c181 commit 3fc4fea
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
10 changes: 9 additions & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[中文说明][[English]](./README.md)

## ScratchPaper 都做了些什么
## ScratchPaper 做了些什么

> 如果你在一台设备上安装两个 App,一个是 Debug 版一个是 Release 版,那么你很难区分出来到底哪个是哪个(不打开的情况下)。
Expand All @@ -23,6 +23,14 @@ ScatchPaper 支持生成编译信息并打包到你的 Apk 中(从 assets 中
- Dependencies
- ...

## 为什么一定要试试 ScratchPaper

其实市面上不乏有类似的解决方案,例如:akonior/icon-version, akaita/easylauncher-gradle-plugin。但是他们的最重要问题在于:不支持 AAPT2!由于 Google 会在 18 年底停止对 aapt1 的支持(enableAapt2=false 将被移除),所以尽早迁移到 AAPT2 其实是一个明智的选择。最后 AAPT2 还会带来额外的诸多好处:

- 其实对 local debug build 时的性能是有一定提升的
- 修复很多 AAPT1 的低级 Bug (我曾写过一个插件来修复各类 AAPT 处理 Manifest 时的 Bug,具体查阅 https://github.com/2BAB/Seal)
- 同样有办法支持插件化的开发

## 如何使用?

**0x01. Add the plugin to classpath:**
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies {
implementation gradleApi()
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation 'com.android.tools.build:gradle:3.2.0'
implementation 'com.android.tools.build:gradle:3.2.1'
implementation 'org.jfree:jfreesvg:3.3'
}

Expand All @@ -57,6 +57,6 @@ if (project.extensions.findByName("buildScan") != null) {

// publish
group 'me.2bab'
version '2.3.1'
version '2.3.2'
apply from: 'bintray.gradle'
apply from: 'mavenlocal.gradle'
61 changes: 61 additions & 0 deletions src/main/kotlin/me/xx2bab/scratchpaper/utils/AGPVersion.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package me.xx2bab.scratchpaper.utils


class AGPVersion(var version: String) : Comparable<AGPVersion> {

init {
// Any alpha/beta/rc version we deem it as the formal one
if (version.contains("-")) {
val indexOfDash = version.indexOf("-")
version = version.substring(0, indexOfDash)
}
// should only include
if (!version.matches("[0-9]+(\\.[0-9]+)*".toRegex())) {
throw IllegalArgumentException("Invalid version format")
}
}

override fun compareTo(other: AGPVersion): Int {
val thisParts = this.get().split("\\.".toRegex())
val thatParts = other.get().split("\\.".toRegex())
val length = Math.max(thisParts.size, thatParts.size)
for (i in 0 until length) {
val thisPart = if (i < thisParts.size)
Integer.parseInt(thisParts[i])
else
0
val thatPart = if (i < thatParts.size)
Integer.parseInt(thatParts[i])
else
0
if (thisPart < thatPart) {
return -1
}
if (thisPart > thatPart) {
return 1
}
}
return 0
}

fun get(): String {
return version
}


override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as AGPVersion

if (this != other) return false

return true
}

override fun hashCode(): Int {
return version.hashCode()
}

}
18 changes: 13 additions & 5 deletions src/main/kotlin/me/xx2bab/scratchpaper/utils/AndroidPluginUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@ package me.xx2bab.scratchpaper.utils

import com.android.build.gradle.AppPlugin
import com.android.build.gradle.BasePlugin
import com.android.build.gradle.internal.scope.GlobalScope
import com.android.builder.core.AndroidBuilder
import com.android.builder.model.Version
import org.gradle.api.Project


class AndroidPluginUtils(val project: Project) {

@Throws(Exception::class)
fun getAndroidBuilder(): AndroidBuilder? {
val basePlugin = project.plugins.findPlugin(AppPlugin::class.java)
return if (null == basePlugin) {
null
fun getAndroidBuilder(): AndroidBuilder {
val basePlugin = project.plugins.findPlugin(AppPlugin::class.java) as BasePlugin<*>
val currentAGPVersion = AGPVersion(Version.ANDROID_GRADLE_PLUGIN_VERSION)
val androidBuilderApiMinAGPVersion = AGPVersion("3.2.1")
return if (currentAGPVersion < androidBuilderApiMinAGPVersion) {
getField(BasePlugin::class.java, basePlugin,
"androidBuilder") as AndroidBuilder
} else {
getField(BasePlugin::class.java, basePlugin, "androidBuilder") as AndroidBuilder
val scope = getField(BasePlugin::class.java, basePlugin,
"globalScope") as GlobalScope
scope.androidBuilder
}
}

Expand All @@ -24,4 +31,5 @@ class AndroidPluginUtils(val project: Project) {
return field.get(instance) as Any
}


}

0 comments on commit 3fc4fea

Please sign in to comment.