如何让图书馆知道非活动类?

时间:2017-06-27 12:43:08

标签: java android dependencies shared-libraries static-methods

也许这个答案存在,但我在这里找不到它,也没有google。

我有一个名为处理程序的静态单例类(非活动类)。我在我的项目中添加了一个库,我想在库的 MainActivity 中使用Handler。

-- app
    --java
        --com.mytest
            --Handler
-- library
    --java
        --com.mylib
            --MainActivity

我试图在MainActivity中导入com.mytest.Handler,但它没有用。 也许我应该为库添加一些依赖?我真的迷路了。

编辑:

MainActivity代码:

public class MainActivity extends AppCompatActivity {
    private Button mButtonMoreApps = null;
    private ArrayList<String> mListUrl = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.mylib.R.layout.main_activity);

    }
}

mylib gradle:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner     "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.config
            debuggable true
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-    core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "com.android.support:cardview-v7:25.3.1"
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support:customtabs:25.3.1'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.firebase:firebase-core:10.2.1'
    compile 'com.google.firebase:firebase-database:10.2.1'
    compile 'com.google.firebase:firebase-ads:10.2.1'
    compile 'com.google.firebase:firebase-crash:10.2.1'
    compile 'com.facebook.android:audience-network-sdk:4.20.0'
    compile ('com.facebook.android:facebook-android-sdk:4.20.0') // app     invites
    {
        exclude group: 'com.android.support'
    }
    testCompile 'junit:junit:4.12'
}

1 个答案:

答案 0 :(得分:0)

您不应该通过导入库来调用库中的应用程序方法。您可以使用侦听器。

例如:https://stackoverflow.com/a/31626226/2176401

库类对应于Observable类,您的应用程序类应该实现Observer。之后,您可以使用notifyObservers方法调用您在Handler类中编写的代码