Skip to content

Commit

Permalink
update AGP 7.3.0并向下兼容APG版本
Browse files Browse the repository at this point in the history
  • Loading branch information
mainlxl committed Oct 9, 2022
1 parent ed30ee3 commit fd2e53d
Show file tree
Hide file tree
Showing 180 changed files with 885 additions and 365 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ SoLoadHook.setSoLoadProxy(new XXXSoLoadProxy())
根据com.android.tools.build:gradle:x.x.x中版本号不同选择使用哪个
3.4.0版本及以下使用SoFileTransformPlugin
3.5.0 - 3.6.0版本使用SoFileAttachMergeTaskPlugin
4.1.0版本使用ApkSoFileAdjustPlugin
4.1.0以上包含7.3.0版本使用ApkSoFileAdjustPlugin

> 4.1.0中添加了compressed_assets机制导致无法把压缩后的so文件放入asstes中顾调整为针对已出包apk进行so文件操作并重新签名
Expand All @@ -138,7 +138,28 @@ SoLoadHook.setSoLoadProxy(new XXXSoLoadProxy())
**接入方式参考顶部最开始部分**

### 三、常见问题

1. 安装时报错`Failure [INSTALL_FAILED_INVALID_APK: Failed to extract native libraries, res=-2]`

请在`application`标签添加属性`android:extractNativeLibs="true"` 如下:

```xml
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<application android:extractNativeLibs="true">
...
</manifest>

```

> 关于extractNativeLibs属性
>
> - 进行apk打包时,`android:extractNativeLibs=false`会对Module中的so库进行压缩,最终得到的apk体积较小。
> - `好处是:`用户在应用市场下载和升级时,因为消耗的流量较小,用户有更强的下载和升级意愿。
> - `缺点是:`因为so是压缩存储的,因此用户安装时,系统会将so解压出来,重新存储一份。因此安装时间会变长,占用的用户磁盘存储空间反而会增大。
> - `minSdkVersion < 23 或 Android Gradle plugin < 3.6.0`,打包时默认值 `android:extractNativeLibs=true`;
> - `minSdkVersion >= 23 并且 Android Gradle plugin >= 3.6.0`,打包时默认值`android:extractNativeLibs=false`;

欢迎加我微信进行交流

Expand Down
12 changes: 1 addition & 11 deletions android-un7z/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ plugins {
}

android {
compileSdkVersion 28
compileSdk 33

defaultConfig {
minSdkVersion 21
versionCode 1
versionName '1.0.0'
consumerProguardFiles 'consumer-proguard-rules.pro'
Expand All @@ -30,17 +29,8 @@ android {
proguardFiles 'consumer-proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
}

def uploadConfig() {
['groupId': 'com.imf.so', 'artifactId': "${name}", 'version': "${SO_PLUGIN_VERSION}"]
}

apply from: "${rootDir}/uploadPlugin.gradle"
5 changes: 4 additions & 1 deletion android-un7z/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hzy.lib7z"></manifest>
package="com.hzy.lib7z">


</manifest>
Binary file added apk/app-debug-7z.apk
Binary file not shown.
Binary file added apk/app-debug-false.apk
Binary file not shown.
Binary file added apk/app-debug-true.apk
Binary file not shown.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ android {
abortOnError false
}

compileSdkVersion 30
compileSdk 33
defaultConfig {
applicationId "com.imf.test"
minSdkVersion 24
minSdkVersion 14
//noinspection ExpiredTargetSdkVersion
targetSdkVersion 28
versionCode 1
Expand Down
1 change: 1 addition & 0 deletions app/libs/aar-release.aar.cache/code-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7:2736fc44c62f905b79d8e8a841811468:59a736c8a89196073f7ea06ccba73b06
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mainli.mylibrary;

/* loaded from: aar-release.aar:classes.jar:com/mainli/mylibrary/BuildConfig.class */
public final class BuildConfig {
public static final boolean DEBUG = false;
public static final String APPLICATION_ID = "com.mainli.mylibrary";
public static final String BUILD_TYPE = "release";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mainli.mylibrary;

/* loaded from: aar-release.aar:classes.jar:com/mainli/mylibrary/NativeLibTest.class */
public class NativeLibTest {
public static native String stringFromJNI();

static {
System.loadLibrary("native-aar-lib");
}
}
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.imf.test">

<uses-sdk tools:overrideLibrary="com.mainli.mylibrary, com.mainli.blur" />
<application
android:allowBackup="true"
android:extractNativeLibs="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
Expand Down
55 changes: 53 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ buildscript {

allprojects {
repositories {
maven { url uri("${rootDir}/maven") }
maven { url uri("${project.rootProject.rootDir}/maven") }
maven { url "https://maven.aliyun.com/repository/google" }
maven { url "https://maven.aliyun.com/repository/jcenter" }
maven { url "https://raw.githubusercontent.com/Android-Mainli/Maven/master" }
Expand All @@ -27,4 +27,55 @@ allprojects {

task clean(type: Delete) {
delete rootProject.buildDir
}
}

subprojects { pro ->
def name = pro.name
if (name == 'app' || name == 'testLibrary') {
return
}
apply plugin: 'maven-publish'
afterEvaluate {
def isJavaProject = true
if (name == 'load-assets-7z' || name == 'android-un7z') {
isJavaProject = false
}
afterEvaluate {
publishing {
publications {
release(MavenPublication) {
groupId 'com.imf.so'
artifactId pro.name
version SO_PLUGIN_VERSION
if (isJavaProject) {
from components.java
} else {
from components.release
}
}
}
repositories {
maven {
url = file("${rootDir}/maven")
}
}
}
}
}
task uploadAll { task ->
task.group = 'upload'
gradle.projectsEvaluated {
task.dependsOn project.tasks.clean
task.dependsOn project.tasks.build

project.tasks.build.mustRunAfter project.tasks.clean

project.subprojects {
if (it.name != 'app') {
task.dependsOn it.tasks.publish
upload.mustRunAfter project.tasks.build
}
}
}
}
}
21 changes: 3 additions & 18 deletions file-plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.72'
id 'org.jetbrains.kotlin.jvm' version '1.7.10'
id 'java'
}

Expand All @@ -9,27 +9,12 @@ repositories {
mavenCentral()
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

dependencies {
//gradle sdk
implementation gradleApi()
//groovy sdk
implementation localGroovy()
implementation 'com.android.tools:common:30.3.0'
implementation "com.google.code.gson:gson:2.8.0"
compileOnly "com.android.tools.build:gradle:$ANDROID_GRADLE_VERSION"
compileOnly "com.android.tools.build:gradle-api:$ANDROID_GRADLE_VERSION"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}


def uploadConfig() {
['groupId': 'com.imf.so', 'artifactId': "${name}", 'version': "${SO_PLUGIN_VERSION}"]
}

apply from: "${rootDir}/uploadPlugin.gradle"

16 changes: 16 additions & 0 deletions file-plugin/src/main/java/com/mainli/apk/FileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.mainli.apk;

import org.jetbrains.annotations.NotNull;

import java.io.File;

public class FileUtils {
@NotNull
public static File join(@NotNull File files, String... paths) {
File tmp = files;
for (String path : paths) {
tmp = new File(tmp, path);
}
return tmp;
}
}
5 changes: 3 additions & 2 deletions file-plugin/src/main/java/com/mainli/apk/ZipUtil.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.mainli.apk;

import com.android.utils.FileUtils;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
Expand Down Expand Up @@ -85,6 +83,9 @@ public static void close(Closeable closeable) {
public static void addFile2ZipOutputStream(File file, String relativePath, ZipOutputStream zos) {
InputStream is = null;
try {
if (!file.exists()) {
return;
}
if (!file.isDirectory()) {
ZipEntry zp = new ZipEntry(relativePath);
zos.putNextEntry(zp);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.imf.plugin.so

import com.android.build.gradle.AppExtension
import com.android.build.gradle.api.ApplicationVariant
import com.android.build.gradle.api.ApkVariant
import com.android.build.gradle.api.BaseVariantOutput
import com.android.ide.common.internal.WaitableExecutor
import com.mainli.apk.ApkSign
import com.mainli.apk.ZipUtil
import org.gradle.api.tasks.Internal
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import java.io.File
Expand All @@ -17,10 +17,10 @@ import javax.inject.Inject


open class ApkSoLibStreamlineTask @Inject constructor(
val android: AppExtension,
val pluginConfig: SoFileExtensions,
val intermediatesDir: File,
val variantName: String
@Internal val android: AppExtension,
@Internal val pluginConfig: SoFileExtensions,
@Internal val intermediatesDir: File,
@Internal val variantName: String
) : DefaultTask() {

@TaskAction
Expand Down Expand Up @@ -48,7 +48,7 @@ open class ApkSoLibStreamlineTask @Inject constructor(
}
}

private fun getApkFile(variant: ApplicationVariant): File? {
private fun getApkFile(variant: ApkVariant): File? {
var outputFile: File? = null
variant.outputs.all { output: BaseVariantOutput ->
try {
Expand All @@ -65,8 +65,8 @@ open class ApkSoLibStreamlineTask @Inject constructor(
}

fun log(msg: Any) {
project.logger.info("[ApkSoLibStreamlineTask]: ${msg}")
// println("[ApkSoLibStreamlineTask]: ${msg}")
// project.logger.info("[ApkSoLibStreamlineTask]: ${msg}")
println("[ApkSoLibStreamlineTask]: ${msg}")
}

open fun streamlineApkSoFile(apk: File?): File? {
Expand All @@ -90,20 +90,16 @@ open class ApkSoLibStreamlineTask @Inject constructor(
}
ZipUtil.addZipFile2ZipOutputStream(inputZip, inputZipEntry, zipOutputStream)
}

val executor: WaitableExecutor = WaitableExecutor.useGlobalSharedThreadPool()
val soHandle = SoHandle(variantName,
val soHandle = SoHandle(
pluginConfig,
object : AssetsOutDestManager(variantName, intermediatesDir) {
override fun buildAssetsOutDestFile(): File {
return File(streamlineFile, JNI_LIBS)
}
})
soHandle.perform7z(
File(streamlineFile, "lib"), executor, null
soHandle.singlePerform7z(
File(streamlineFile, "lib"), null
)
executor.waitForTasksWithQuickFail<Any?>(true);
soHandle.resultWriteToFile()
ZipUtil.addFile2ZipOutputStream(
File(streamlineFile, "jniLibs"), "assets/jniLibs/", zipOutputStream
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.imf.plugin.so

import com.android.utils.FileUtils
import com.mainli.apk.FileUtils
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.lang.String.join
import java.util.jar.JarEntry
import java.util.jar.JarOutputStream

Expand Down
Loading

0 comments on commit fd2e53d

Please sign in to comment.