初步完成依赖自动切换实现

This commit is contained in:
yummylau
2019-08-07 14:38:07 +08:00
parent 05141c8d2a
commit d0c375c14c
37 changed files with 209 additions and 11 deletions

View File

@@ -12,7 +12,7 @@ android {
buildToolsVersion "29.0.1"
defaultConfig {
applicationId "com.plugin.module"
minSdkVersion 15
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
@@ -35,7 +35,9 @@ dependencies {
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
println 'dependencies中执行的代码'
implementation misPublication('com.eastwood.demo:library-sdk')
implementation misPublication('com.effective.android:library-sdk')
implementation project(':module_lib')
}
// 创建一个Task

View File

@@ -2,9 +2,11 @@ package com.plugin.module;
import android.os.Bundle;
import com.effective.android.module.ModuleMamager;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.plugin.library.IAction;
import com.plugin.library.LibraryAction;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
@@ -22,7 +24,7 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ModuleMamager.register(IAction.class, new LibraryAction());
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
@@ -31,7 +33,13 @@ public class MainActivity extends AppCompatActivity {
.setAction("Action", null).show();
}
});
((TextView) findViewById(R.id.text)).setText("");
// IAction action = new IAction() {
// @Override
// public String getName() {
// return null;
// }
// }
((TextView) findViewById(R.id.text)).setText(ModuleMamager.getService(IAction.class).getName());
}
@Override

View File

@@ -10,7 +10,7 @@ android {
buildToolsVersion "29.0.1"
defaultConfig {
minSdkVersion 15
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"

View File

@@ -10,7 +10,8 @@ module {
publications {
main {
groupId 'com.eastwood.demo'
localProject 'library'
groupId 'com.effective.android'
artifactId 'library-sdk'
// version '1.0.0' // 初次配置时不设置发布至maven时设置

View File

@@ -1,6 +1,6 @@
package com.plugin.library;
public class Action implements IAction {
public class LibraryAction implements IAction {
@Override
public String getName() {
@@ -8,6 +8,6 @@ public class Action implements IAction {
}
}
//public class Action {
//public class LibraryAction {
//
//}

View File

@@ -12,6 +12,6 @@ public class LibraryMainActivity extends AppCompatActivity {
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_layout);
Toast.makeText(this, new Action().getName(), Toast.LENGTH_LONG).show();
Toast.makeText(this, new LibraryAction().getName(), Toast.LENGTH_LONG).show();
}
}

View File

@@ -100,15 +100,24 @@ class ModulePlugin implements Plugin<Project> {
project.dependencies.metaClass.misPublication { Object value ->
String[] gav = MisUtil.filterGAV(value)
if (isRunAlone && assembleTask.isAssemble) {
project.dependencies {
implementation PublicationUtil.getPublication(gav[0], gav[1])
}
return project.project(':library')
}
return PublicationUtil.getPublication(gav[0], gav[1])
}
List<Publication> publications = ModuleRuntime.publicationManager.getPublicationByProject(project)
project.dependencies {
publications.each {
implementation PublicationUtil.getPublication(it.groupId, it.artifactId)
api PublicationUtil.getPublication(it.groupId, it.artifactId)
}
}
if (project.gradle.startParameter.taskNames.isEmpty()) {
publications.each {
PublicationUtil.addPublicationDependencies(project, it)

View File

@@ -16,6 +16,7 @@ class Publication {
String groupId //依赖分组id
String artifactId //依赖id
String version //依赖版本
String localProject //依赖版本
String versionNew
@@ -35,6 +36,10 @@ class Publication {
this.groupId = groupId
}
void localProject(String name) {
this.localProject = name
}
void artifactId(String artifactId) {
this.artifactId = artifactId
}

1
module_lib/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

34
module_lib/build.gradle Normal file
View File

@@ -0,0 +1,34 @@
apply plugin: 'com.android.library'
android {
compileSdkVersion 29
buildToolsVersion "29.0.1"
defaultConfig {
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

21
module_lib/proguard-rules.pro vendored Normal file
View File

@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@@ -0,0 +1,27 @@
package com.effective.android.module;
import android.content.Context;
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.effective.android.module.test", appContext.getPackageName());
}
}

View File

@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.effective.android.module" />

View File

@@ -0,0 +1,68 @@
package com.effective.android.module;
import android.util.ArrayMap;
import androidx.annotation.NonNull;
import java.lang.ref.WeakReference;
public class ModuleMamager {
private static ArrayMap<Class, Object> sServiceArrayMap;
private static ArrayMap<Class, WeakReference<Object>> sWeekServiceArrayMap;
public static void register(@NonNull Class serviceKey, @NonNull Object serviceObjectOrClass) {
if (!serviceKey.isInterface()) {
throw new IllegalArgumentException("register service key must be interface class.");
}
if (serviceObjectOrClass.getClass().isInterface()) {
throw new IllegalArgumentException("register service object must not be interface.");
}
Class realClass = serviceObjectOrClass instanceof Class ? (Class) serviceObjectOrClass : serviceObjectOrClass.getClass();
if (!serviceKey.isAssignableFrom(realClass)) {
throw new IllegalArgumentException(String.format("register service object must implement interface %s.", serviceKey));
}
if (sServiceArrayMap == null) {
sServiceArrayMap = new ArrayMap<>();
}
sServiceArrayMap.put(serviceKey, serviceObjectOrClass);
}
public static void unregister(Class serviceKey) {
if (serviceKey == null || sServiceArrayMap == null) return;
sServiceArrayMap.remove(serviceKey);
}
public static <T> T getService(Class<T> serviceKey) {
if (sServiceArrayMap == null) return null;
Object object = sServiceArrayMap.get(serviceKey);
if (object == null) return null;
if (object instanceof Class) {
Object result = null;
if (sWeekServiceArrayMap == null) {
sWeekServiceArrayMap = new ArrayMap<>();
}
WeakReference<Object> cachedObject = sWeekServiceArrayMap.get(serviceKey);
if (cachedObject != null && cachedObject.get() != null) {
result = cachedObject.get();
} else {
try {
result = ((Class) object).newInstance();
sWeekServiceArrayMap.put(serviceKey, new WeakReference<>(result));
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
return (T) result;
} else {
return (T) object;
}
}
}

View File

@@ -0,0 +1,3 @@
<resources>
<string name="app_name">module_lib</string>
</resources>

View File

@@ -0,0 +1,17 @@
package com.effective.android.module;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
}

View File

@@ -1,4 +1,4 @@
include ':app', ':library'
include ':app', ':library', ':module_lib'
//调试插件使用
includeBuild './module-plugin'