Popularity
0.7
Stable
Activity
0.0
Stable
3
3
1

Description

GhostFish is a fast and lightweight dependency injection container. It can inject application scoped beans in other beans or even non-managed objects.

Most of the work is done by the annotation processor.

Example

@ApplicationScoped public class SampleService {

public String helloWorld() { return "Hello, world!"; }

@PostConstruct private void init() { // code } }

public class MainActivity extends AppCompatActivity {

@Inject private SampleService sampleService; @Override protected void onCreate(Bundle savedInstanceState) { ... TextView textView = findViewById(R.id.message); textView.setText(sampleService.helloWorld()); ... } }

Programming language: Java
License: Apache License 2.0
Latest version: v2.0.0

android-ghostfish alternatives and similar packages

Based on the "Dependency Injection" category.
Alternatively, view android-ghostfish alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of android-ghostfish or a related project?

Add another 'Dependency Injection' Package

README

android-ghostfish Download Android Arsenal

Dependency injection container for Android

1. Annotations

  • ApplicationScoped - specifies that a bean is application scoped.
  • Inject - injectable instance fields or constructors.
  • PostConstruct - method that needs to be run after instantiating an application scoped bean.

2. Dependency injection

Using annotation processors, GhostFish compiles a list of application scoped beans in asset/beans.txt file.

3. Example

3.1 Add GhostFish dependency to your gradle file:

implementation "com.danielpuiu:ghostfish:2.0.0"
annotationProcessor "com.danielpuiu:ghostfish-compiler:2.0.0"

3.2 Add GhostFish bind call to your application

Subclass Android application and add the following code to onCreate method:

@Override
public void onCreate() {
    super.onCreate();

    GhostFish.create(this);
}

Don't forget to add this class to your manifest file right under application tag

<application
    ...
    android:name="<class_name>"
    ...

3.3 Proguard

If you use Proguard and minified, then make sure to add the following rule to your proguard rules file:

-keep @com.danielpuiu.ghostfish.annotations.ApplicationScoped class * {*;}

3.4 Sample code

3.4.1 Service class

@ApplicationScoped
public class SampleService {

    public String helloWorld() {
        return "Hello, world!";
    }

    @PostConstruct
    private void init() {
        // code
    }
}

3.4.2 Activity class

public class MainActivity extends AppCompatActivity {

    @Inject
    private SampleService sampleService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        GhostFish.bind(this);
        ...
        TextView textView = findViewById(R.id.message);
        textView.setText(sampleService.helloWorld());
        ...
    }
}

4 Developed By

Daniel PUIU

5 License

Copyright 2018 Daniel PUIU

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


*Note that all licence references and agreements mentioned in the android-ghostfish README section above are relevant to that project's source code only.