-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for the example app on Android (#24)
* Supporting example app for Android * Refactoring test-app code and applying suggestions * Downgrading react-native to v0.60.6 * CI pipeline definition for android * Printing current working directory on CI * Configure pipeline to build android code without emulator * Skip using a task to install sdk manager * Experimenting with builds on windows * Using a specific node version * Checking what is causing the problem with windows ci * Print file on windows * Print file on windows * Explicitly using cmd * Running gradle command separately * Cleaning-up the build definition file * Addressing PR comments * Attempting to run bash on windows ci agent * Move file level var to the method def * Merging android job definitions * Treat gradlew as executable in build.yml * Update error message. Co-Authored-By: Tommy Nguyen <[email protected]> * Adds a todo with a link to an issue Co-authored-by: Tommy Nguyen <[email protected]>
- Loading branch information
1 parent
d4d43a4
commit 08441e7
Showing
32 changed files
with
1,073 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.sample | ||
|
||
import android.app.Activity | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import com.facebook.react.ReactActivity | ||
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler | ||
import com.facebook.soloader.SoLoader | ||
|
||
class ComponentActivity : ReactActivity(), DefaultHardwareBackBtnHandler { | ||
companion object { | ||
private const val COMPONENT_NAME = "extra:componentName"; | ||
|
||
fun newIntent(activity: Activity, componentName: String): Intent { | ||
return Intent(activity, ComponentActivity::class.java).apply { | ||
putExtra(COMPONENT_NAME, componentName) | ||
} | ||
} | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
SoLoader.init(this, false) | ||
|
||
val componentName = intent.getStringExtra(COMPONENT_NAME) | ||
loadApp(componentName) | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
android/app/src/main/java/com/sample/ComponentListAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.sample | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import android.widget.TextView | ||
import androidx.recyclerview.widget.RecyclerView | ||
|
||
class ComponentListAdapter( | ||
private val layoutInflater: LayoutInflater, | ||
private val components: List<ComponentViewModel>, | ||
private val listener: (ComponentViewModel) -> Unit | ||
) : RecyclerView.Adapter<ComponentListAdapter.ComponentViewHolder>() { | ||
|
||
override fun getItemCount() = components.size | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ComponentViewHolder { | ||
return ComponentViewHolder( | ||
layoutInflater.inflate( | ||
R.layout.recyclerview_item_component, parent, false | ||
) as TextView | ||
) | ||
} | ||
|
||
override fun onBindViewHolder(holder: ComponentViewHolder, position: Int) { | ||
holder.bindTo(components[position]) | ||
} | ||
|
||
inner class ComponentViewHolder(private val view: TextView) : RecyclerView.ViewHolder(view) { | ||
init { | ||
view.setOnClickListener { listener(components[adapterPosition]) } | ||
} | ||
|
||
fun bindTo(component: ComponentViewModel) { | ||
view.text = component.displayName | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.sample | ||
|
||
data class ComponentViewModel(val name: String, val displayName: String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,43 @@ | ||
package com.sample | ||
|
||
import android.os.Bundle | ||
import android.view.KeyEvent | ||
import android.view.LayoutInflater | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.facebook.react.PackageList | ||
import com.facebook.react.ReactInstanceManager | ||
import com.facebook.react.ReactRootView | ||
import com.facebook.react.TestAppPackageList | ||
import com.facebook.react.common.LifecycleState | ||
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler | ||
import com.facebook.soloader.SoLoader | ||
import androidx.recyclerview.widget.DividerItemDecoration | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.sample.manifest.ManifestProvider | ||
import dagger.android.AndroidInjection | ||
import javax.inject.Inject | ||
|
||
class MainActivity : AppCompatActivity(), DefaultHardwareBackBtnHandler { | ||
private lateinit var reactRootView: ReactRootView | ||
private lateinit var reactInstanceManager: ReactInstanceManager | ||
class MainActivity : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
SoLoader.init(this, false) | ||
|
||
reactRootView = ReactRootView(this) | ||
setContentView(reactRootView) | ||
|
||
reactInstanceManager = ReactInstanceManager.builder() | ||
.setInitialLifecycleState(LifecycleState.BEFORE_RESUME) | ||
.addPackages(PackageList(application).packages) | ||
.addPackages(TestAppPackageList().packages) | ||
.setUseDeveloperSupport(BuildConfig.DEBUG) | ||
.setCurrentActivity(this) | ||
.setBundleAssetName("index.android.bundle") | ||
.setJSMainModulePath("index") | ||
.setApplication(application) | ||
.build() | ||
@Inject | ||
lateinit var manifestProvider: ManifestProvider | ||
|
||
reactRootView.startReactApplication( | ||
reactInstanceManager, "TestComponent", null | ||
) | ||
private val listener = { component: ComponentViewModel -> | ||
startActivity(ComponentActivity.newIntent(this, component.name)) | ||
} | ||
|
||
override fun invokeDefaultOnBackPressed() { | ||
onBackPressed() | ||
} | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
AndroidInjection.inject(this) | ||
|
||
override fun onBackPressed() { | ||
reactInstanceManager.onBackPressed() | ||
} | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
|
||
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean { | ||
if (keyCode == KeyEvent.KEYCODE_MENU) { | ||
reactInstanceManager.showDevOptionsDialog() | ||
return true | ||
val manifest = manifestProvider.manifest | ||
?: throw IllegalStateException("app.json is not provided or TestApp is misconfigured") | ||
val components = manifest.components.map { | ||
ComponentViewModel(it.key, it.value.displayName) | ||
} | ||
return super.onKeyUp(keyCode, event) | ||
} | ||
|
||
|
||
override fun onPause() { | ||
super.onPause() | ||
|
||
reactInstanceManager.onHostPause(this) | ||
} | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
|
||
reactInstanceManager.onHostResume(this, this) | ||
} | ||
supportActionBar?.title = manifest.displayName | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
findViewById<RecyclerView>(R.id.recyclerview).apply { | ||
layoutManager = LinearLayoutManager(context) | ||
adapter = ComponentListAdapter(LayoutInflater.from(context), components, listener) | ||
|
||
reactInstanceManager.onHostDestroy(this) | ||
reactRootView.unmountReactApplication() | ||
addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.sample | ||
|
||
import android.app.Application | ||
import com.facebook.react.ReactApplication | ||
import com.facebook.react.ReactNativeHost | ||
import com.sample.di.DaggerTestAppComponent | ||
import dagger.android.AndroidInjector | ||
import dagger.android.DispatchingAndroidInjector | ||
import dagger.android.HasAndroidInjector | ||
import javax.inject.Inject | ||
|
||
class TestApp : Application(), HasAndroidInjector, ReactApplication { | ||
|
||
@Inject | ||
lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Any> | ||
|
||
@Inject | ||
lateinit var reactNativeHostInternal: ReactNativeHost | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
|
||
val testAppComponent = DaggerTestAppComponent.builder() | ||
.binds(this) | ||
.build() | ||
|
||
testAppComponent.inject(this) | ||
} | ||
|
||
override fun androidInjector(): AndroidInjector<Any> = dispatchingAndroidInjector | ||
|
||
override fun getReactNativeHost() = reactNativeHostInternal | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.sample.di | ||
|
||
import javax.inject.Scope | ||
|
||
@Scope | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class ActivityScope |
24 changes: 24 additions & 0 deletions
24
android/app/src/main/java/com/sample/di/TestAppBindings.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.sample.di | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import com.facebook.react.ReactNativeHost | ||
import com.sample.MainActivity | ||
import com.sample.react.TestAppReactNativeHost | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.android.ContributesAndroidInjector | ||
|
||
@Module | ||
abstract class TestAppBindings { | ||
|
||
@Binds | ||
abstract fun bindsContext(application: Application): Context | ||
|
||
@Binds | ||
abstract fun bindsReactNativeHost(reactNativeHost: TestAppReactNativeHost): ReactNativeHost | ||
|
||
@ActivityScope | ||
@ContributesAndroidInjector | ||
abstract fun contributeMainActivityInjector(): MainActivity | ||
} |
Oops, something went wrong.