2017-01-04 4 views
0

ライブラリ(コアとモジュール)をほとんど作成せず、すべてのライブラリをアプリケーションに追加し、コアライブラリのモジュールからメソッドを呼び出す必要があります。すべてのモジュールはオプションでなければなりません。例えば、私はそれを見るように:モジュールを使ってライブラリを作成する方法は?

アプリケーションbuild.gradle

compile('core-library:[email protected]') 
compile('module1-library:[email protected]') 
compile('module2-library:[email protected]') 
compile('module3-library:[email protected]') 

すべてのモジュール内のクラスと同じ名前のメソッドを定義します。

public class ModuleClass { 
    public int moduleMethod1() { 
     // Do something and return result 
     return 1; 
    } 

    public String moduleMethod2() { 
     return "Some String"; 
    } 
} 

コアライブラリで:

for(ModuleClass c : getAllModules()) { 
    Log.d("tag", "Result: " + c.moduleMethod1() + "/" + c.moduleMethod2(); 
} 

これはちょうど擬似コードです。それをどうやって実装するのですか?あなたはcore-libraryList<Class>を追加するようなものを試してみてください

apply plugin: 'com.android.library' 

android { 
    compileSdkVersion 24 
    buildToolsVersion "24.0.3" 

    defaultConfig { 
     minSdkVersion 9 
     targetSdkVersion 24 
     versionCode 1 
     versionName "1.0" 

     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 

     buildConfigField "String[]", "KNOWN_MODULES", "{" + 
       "\"module1\"," + 
       "\"module2\"" + 
       "}" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    compileOnly project(':module1') 
    compileOnly project(':module2') 
    testCompile 'junit:junit:4.12' 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
} 

答えて

1

: コアモジュールbuild.gradle

を更新しました。あなたはリストを読み込もうとすると本当に注意する必要がありますが、それを行うことができるはずです。

最初にmoduleN-librarycompileOnlyである必要がありますので、core-libraryに同梱されません。

あなたのcore-libraryには、すべての可能なモジュールクラスを安全にリストするシングルトンを保持します。

package com.jbirdvegas.test; 

import java.util.HashMap; 
import java.util.Map; 

public class ModuleClassManager { 
    private static final Object LOCK = new Object(); 
    private static ModuleClassManager instance; 

    private Map<Class, Methods> moduleClasses = new HashMap<>(); 

    private ModuleClassManager() { 
     try { 
      moduleClasses.put(MayNotExist1.class, new MayNotExist1()); 
     } catch (Exception ignored) { 
     } 
     try { 
      moduleClasses.put(MayNotExist2.class, new MayNotExist2()); 
     } catch (Exception ignored) { 
     } 
     try { 
      moduleClasses.put(MayNotExist3.class, new MayNotExist3()); 
     } catch (Exception ignored) { 
     } 
    } 

    public static ModuleClassManager getInstance() { 
     if (instance == null) { 
      synchronized (LOCK) { 
       instance = new ModuleClassManager(); 
      } 
     } 
     return instance; 
    } 

    public void callMethodOnAll() { 
     moduleClasses.forEach((aClass, methods) -> methods.methodOne()); 
    } 

    // the below interface in the real world would be located 
    // in the `core-library` and the classes that implement the 
    // interface, `Methods` in this example, would exist in the 
    // module's they represent. 
    interface Methods { 
     void methodOne(); 
    } 

    class MayNotExist1 implements Methods { 
     @Override 
     public void methodOne() { 
     } 
    } 

    class MayNotExist2 implements Methods { 
     @Override 
     public void methodOne() { 
     } 
    } 

    class MayNotExist3 implements Methods { 
     @Override 
     public void methodOne() { 
     } 
    } 
} 

または... OSGiを使用します。 OSGiは設定がより複雑ですが、このユースケースを非常にうまく処理します。

+0

ありがとうございます。 'compileOnly'はモジュールに使用できますか?コード 'compileOnly project( ':mymodule1')'エラーを生成する 'org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler'型のオブジェクトの引数[project ':mymodule1]に対するメソッドcompileOnly()を見つけることができませんでした。あるいは、すべてのモジュールで毎回aarファイルを手動でコンパイルする必要がありますか? – BArtWell

+0

完全なbuild.gradleファイルを表示する 'java'プラグインは' compileOnly'ソースセットを追加する必要があります – JBirdVegas

+0

更新された質問をご覧ください。コアモジュール用に完全なbuild.gradleを追加します。 – BArtWell

関連する問題