初步完成依赖自动切换实现
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -10,7 +10,7 @@ android {
|
||||
buildToolsVersion "29.0.1"
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 15
|
||||
minSdkVersion 19
|
||||
targetSdkVersion 29
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
|
||||
@@ -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时设置
|
||||
|
||||
|
||||
@@ -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 {
|
||||
//
|
||||
//}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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)
|
||||
|
||||
@@ -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
1
module_lib/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build
|
||||
34
module_lib/build.gradle
Normal file
34
module_lib/build.gradle
Normal 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
21
module_lib/proguard-rules.pro
vendored
Normal 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
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
2
module_lib/src/main/AndroidManifest.xml
Normal file
2
module_lib/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,2 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.effective.android.module" />
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
module_lib/src/main/res/values/strings.xml
Normal file
3
module_lib/src/main/res/values/strings.xml
Normal file
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="app_name">module_lib</string>
|
||||
</resources>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
include ':app', ':library'
|
||||
include ':app', ':library', ':module_lib'
|
||||
|
||||
//调试插件使用
|
||||
includeBuild './module-plugin'
|
||||
|
||||
Reference in New Issue
Block a user