Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化SplitCompatResourcesLoader,添加混淆规则到AAR #51

Merged
merged 2 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/multidexkeep.pro
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}

# ${yourApplicationId}.QigsawConfig, QigsawVersion >= 1.2.2
-keep class com.iqiyi.qigsaw.sample.QigsawConfig {
-keep class **.QigsawConfig {
*;
}

Expand Down
1 change: 1 addition & 0 deletions qigsaw-android/splitcore/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ android {
buildTypes {
release {
minifyEnabled false
consumerProguardFiles 'proguard-rules.pro'
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Expand Down
15 changes: 15 additions & 0 deletions qigsaw-android/splitcore/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@
# hide the original source file name.
#-renamesourcefileattribute SourceFile

#copy from multidexkeep.pro
-keep class com.iqiyi.android.qigsaw.core.Qigsaw {
<init>(...);
void install(...);
void onAppGetResources(Resources);
}

# ${yourApplicationId}.QigsawConfig, QigsawVersion >= 1.2.2
-keep class **.QigsawConfig {
*;
}

-keep class com.iqiyi.android.qigsaw.core.extension.ComponentInfo {
*;
}



Expand Down
1 change: 1 addition & 0 deletions qigsaw-android/splitdownloader/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ android {
buildTypes {
release {
minifyEnabled false
consumerProguardFiles 'proguard-rules.pro'
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Expand Down
4 changes: 4 additions & 0 deletions qigsaw-android/splitdownloader/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
# hide the original source file name.
#-renamesourcefileattribute SourceFile

#copy from multidexkeep.pro
-keep class * implements com.iqiyi.android.qigsaw.core.splitdownload.Downloader {
<init>(...);
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,24 @@
import android.util.DisplayMetrics;
import android.view.ContextThemeWrapper;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;

import com.iqiyi.android.qigsaw.core.common.SplitBaseInfoProvider;
import com.iqiyi.android.qigsaw.core.common.SplitLog;
import com.iqiyi.android.qigsaw.core.splitload.compat.SplitResourcesLoader;

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.ServiceLoader;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;

Expand All @@ -68,21 +68,23 @@ public class SplitCompatResourcesLoader {
private static final String TAG = "SplitCompatResourcesLoader";

private static final Object sLock = new Object();
@NonNull
private static final SplitResourcesLoader resourcesLoader;

static {
resourcesLoader = getSplitResourcesLoader();
}

/**
* Check if split res dir has been added into {@link Resources}, this method should be invoked in {@link Activity#getResources()}.
* After Android 7.0, WebView.apk resources is added dynamically.
*/
public static void loadResources(Context context, Resources resources) throws Throwable {
if (!fullyLoadedRes.containsKey(resources)) checkOrUpdateResources(context, resources);
resourcesLoader.loadResources(context, resources);
}

static void loadResources(Context context, Resources preResources, String splitApkPath) throws Throwable {
List<String> loadedResDirs = getLoadedResourcesDirs(preResources.getAssets());
if (!loadedResDirs.contains(splitApkPath)) {
installSplitResDirs(context, preResources, Collections.singletonList(splitApkPath));
SplitLog.d(TAG, "Install split %s resources for application.", splitApkPath);
}
resourcesLoader.loadResources(context, preResources, splitApkPath);
}

private static void checkOrUpdateResources(Context context, Resources resources) throws SplitCompatResourcesException {
Expand All @@ -103,11 +105,10 @@ private static void checkOrUpdateResources(Context context, Resources resources)
}
try {
installSplitResDirs(context, resources, unloadedSplitPaths);
if (isFullyLoadedRes()) fullyLoadedRes.put(resources, null);
} catch (Throwable e) {
throw new SplitCompatResourcesException("Failed to install resources " + unloadedSplitPaths.toString() + " for " + context.getClass().getName(), e);
}
} else if (isFullyLoadedRes()) fullyLoadedRes.put(resources, null);
}
}
}

Expand Down Expand Up @@ -562,32 +563,27 @@ static Method getGetApkAssetsMethod() throws NoSuchMethodException {
}
}

//region fullyLoadedRes
private static final Map<Resources, Void> fullyLoadedRes = new WeakHashMap<>();
private static List<String> dynamicFeatures;
private static class DefaultSplitResourcesLoader implements SplitResourcesLoader {

private static boolean isFullyLoadedRes() {
Collection<String> dynamicFeatures = getDynamicFeatures();
Collection<String> loadedSplitNames = getLoadedSplitNames();
if (dynamicFeatures != null && loadedSplitNames != null) {
return loadedSplitNames.containsAll(dynamicFeatures);
} else return false;
}
@Override
public void loadResources(@NonNull Context context, @NonNull Resources resources) throws Throwable {
checkOrUpdateResources(context, resources);
}

@Nullable
private static Collection<String> getDynamicFeatures() {
if (dynamicFeatures != null && dynamicFeatures.size() > 0) return dynamicFeatures;
String[] features = SplitBaseInfoProvider.getDynamicFeatures();
dynamicFeatures = features != null ? Arrays.asList(features) : null;
return dynamicFeatures;
@Override
public void loadResources(@NonNull Context context, @NonNull Resources preResources, @NonNull String splitApkPath) throws Throwable {
List<String> loadedResDirs = getLoadedResourcesDirs(preResources.getAssets());
if (!loadedResDirs.contains(splitApkPath)) {
installSplitResDirs(context, preResources, Collections.singletonList(splitApkPath));
SplitLog.d(TAG, "Install split %s resources for application.", splitApkPath);
}
}
}

private static Collection<String> getLoadedSplitNames() {
SplitLoadManager loadManager = SplitLoadManagerService.getInstance();
if (loadManager != null) {
return loadManager.getLoadedSplitNames();
}
return null;
@NonNull
private static SplitResourcesLoader getSplitResourcesLoader() {
ServiceLoader<SplitResourcesLoader> compats = ServiceLoader.load(SplitResourcesLoader.class);
Iterator<SplitResourcesLoader> iterator = compats.iterator();
return iterator.hasNext() ? iterator.next() : new DefaultSplitResourcesLoader();
}
//endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.iqiyi.android.qigsaw.core.splitload.compat;

import android.content.Context;
import android.content.res.Resources;

import androidx.annotation.NonNull;

/**
* fetch with ServiceLoader
*/
public interface SplitResourcesLoader {
void loadResources(@NonNull Context context, @NonNull Resources resources) throws Throwable;

void loadResources(@NonNull Context context, @NonNull Resources preResources, @NonNull String splitApkPath) throws Throwable;
}