ライブラリ(コアとモジュール)をほとんど作成せず、すべてのライブラリをアプリケーションに追加し、コアライブラリのモジュールからメソッドを呼び出す必要があります。すべてのモジュールはオプションでなければなりません。例えば、私はそれを見るように:モジュールを使ってライブラリを作成する方法は?
アプリケーション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-library
でList<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'
})
}
ありがとうございます。 'compileOnly'はモジュールに使用できますか?コード 'compileOnly project( ':mymodule1')'エラーを生成する 'org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler'型のオブジェクトの引数[project ':mymodule1]に対するメソッドcompileOnly()を見つけることができませんでした。あるいは、すべてのモジュールで毎回aarファイルを手動でコンパイルする必要がありますか? – BArtWell
完全なbuild.gradleファイルを表示する 'java'プラグインは' compileOnly'ソースセットを追加する必要があります – JBirdVegas
更新された質問をご覧ください。コアモジュール用に完全なbuild.gradleを追加します。 – BArtWell