Skip to content

Commit 6ab11db

Browse files
committed
Add Android without Jetpack Compose
1 parent 583e1eb commit 6ab11db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1207
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'org.jetbrains.kotlin.android'
4+
id 'com.chaquo.python'
5+
}
6+
7+
android {
8+
namespace 'com.pythonnative.pythonnative'
9+
compileSdk 33
10+
11+
defaultConfig {
12+
applicationId "com.pythonnative.pythonnative"
13+
minSdk 24
14+
targetSdk 33
15+
versionCode 1
16+
versionName "1.0"
17+
18+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
19+
ndk {
20+
abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
21+
}
22+
python {
23+
pip {
24+
install "matplotlib"
25+
}
26+
}
27+
}
28+
29+
buildTypes {
30+
release {
31+
minifyEnabled false
32+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
33+
}
34+
}
35+
compileOptions {
36+
sourceCompatibility JavaVersion.VERSION_1_8
37+
targetCompatibility JavaVersion.VERSION_1_8
38+
}
39+
kotlinOptions {
40+
jvmTarget = '1.8'
41+
}
42+
buildFeatures {
43+
viewBinding true
44+
}
45+
}
46+
47+
dependencies {
48+
49+
implementation 'androidx.core:core-ktx:1.8.0'
50+
implementation 'androidx.appcompat:appcompat:1.4.1'
51+
implementation 'com.google.android.material:material:1.5.0'
52+
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
53+
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.1'
54+
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'
55+
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.2'
56+
implementation 'androidx.navigation:navigation-ui-ktx:2.5.2'
57+
testImplementation 'junit:junit:4.13.2'
58+
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
59+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
60+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.pythonnative.pythonnative
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.pythonnative.pythonnative", appContext.packageName)
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:dataExtractionRules="@xml/data_extraction_rules"
8+
android:fullBackupContent="@xml/backup_rules"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/Theme.Pythonnative"
14+
tools:targetApi="31">
15+
<activity
16+
android:name=".MainActivity"
17+
android:exported="true"
18+
android:label="@string/app_name">
19+
<intent-filter>
20+
<action android:name="android.intent.action.MAIN" />
21+
22+
<category android:name="android.intent.category.LAUNCHER" />
23+
</intent-filter>
24+
</activity>
25+
</application>
26+
27+
</manifest>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.pythonnative.pythonnative
2+
3+
import android.os.Bundle
4+
import com.google.android.material.bottomnavigation.BottomNavigationView
5+
import androidx.appcompat.app.AppCompatActivity
6+
import androidx.navigation.findNavController
7+
import androidx.navigation.ui.AppBarConfiguration
8+
import androidx.navigation.ui.setupActionBarWithNavController
9+
import androidx.navigation.ui.setupWithNavController
10+
import com.pythonnative.pythonnative.databinding.ActivityMainBinding
11+
12+
class MainActivity : AppCompatActivity() {
13+
14+
private lateinit var binding: ActivityMainBinding
15+
16+
override fun onCreate(savedInstanceState: Bundle?) {
17+
super.onCreate(savedInstanceState)
18+
19+
binding = ActivityMainBinding.inflate(layoutInflater)
20+
setContentView(binding.root)
21+
22+
val navView: BottomNavigationView = binding.navView
23+
24+
val navController = findNavController(R.id.nav_host_fragment_activity_main)
25+
// Passing each menu ID as a set of Ids because each
26+
// menu should be considered as top level destinations.
27+
val appBarConfiguration = AppBarConfiguration(
28+
setOf(
29+
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
30+
)
31+
)
32+
setupActionBarWithNavController(navController, appBarConfiguration)
33+
navView.setupWithNavController(navController)
34+
}
35+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.pythonnative.pythonnative.ui.dashboard
2+
3+
import android.os.Bundle
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import android.widget.TextView
8+
import androidx.fragment.app.Fragment
9+
import androidx.lifecycle.ViewModelProvider
10+
import com.pythonnative.pythonnative.databinding.FragmentDashboardBinding
11+
12+
class DashboardFragment : Fragment() {
13+
14+
private var _binding: FragmentDashboardBinding? = null
15+
16+
// This property is only valid between onCreateView and
17+
// onDestroyView.
18+
private val binding get() = _binding!!
19+
20+
override fun onCreateView(
21+
inflater: LayoutInflater,
22+
container: ViewGroup?,
23+
savedInstanceState: Bundle?
24+
): View {
25+
val dashboardViewModel =
26+
ViewModelProvider(this).get(DashboardViewModel::class.java)
27+
28+
_binding = FragmentDashboardBinding.inflate(inflater, container, false)
29+
val root: View = binding.root
30+
31+
val textView: TextView = binding.textDashboard
32+
dashboardViewModel.text.observe(viewLifecycleOwner) {
33+
textView.text = it
34+
}
35+
return root
36+
}
37+
38+
override fun onDestroyView() {
39+
super.onDestroyView()
40+
_binding = null
41+
}
42+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.pythonnative.pythonnative.ui.dashboard
2+
3+
import androidx.lifecycle.LiveData
4+
import androidx.lifecycle.MutableLiveData
5+
import androidx.lifecycle.ViewModel
6+
7+
class DashboardViewModel : ViewModel() {
8+
9+
private val _text = MutableLiveData<String>().apply {
10+
value = "This is dashboard Fragment"
11+
}
12+
val text: LiveData<String> = _text
13+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.pythonnative.pythonnative.ui.home
2+
3+
import android.graphics.BitmapFactory
4+
import android.os.Bundle
5+
import android.view.LayoutInflater
6+
import android.view.View
7+
import android.view.ViewGroup
8+
import android.widget.TextView
9+
import androidx.fragment.app.Fragment
10+
import androidx.lifecycle.ViewModelProvider
11+
import androidx.lifecycle.lifecycleScope
12+
import com.pythonnative.pythonnative.databinding.FragmentHomeBinding
13+
import com.chaquo.python.PyException
14+
import com.chaquo.python.Python
15+
import com.chaquo.python.android.AndroidPlatform
16+
import kotlinx.coroutines.Dispatchers
17+
import kotlinx.coroutines.launch
18+
import kotlinx.coroutines.withContext
19+
20+
class HomeFragment : Fragment() {
21+
22+
private var _binding: FragmentHomeBinding? = null
23+
24+
// This property is only valid between onCreateView and
25+
// onDestroyView.
26+
private val binding get() = _binding!!
27+
28+
override fun onCreateView(
29+
inflater: LayoutInflater,
30+
container: ViewGroup?,
31+
savedInstanceState: Bundle?
32+
): View {
33+
_binding = FragmentHomeBinding.inflate(inflater, container, false)
34+
35+
// Initialize Chaquopy
36+
if (!Python.isStarted()) {
37+
Python.start(AndroidPlatform(requireContext()))
38+
}
39+
val py = Python.getInstance()
40+
val plotModule = py.getModule("plot")
41+
42+
// Variables to keep the user's input
43+
val xInput = "1 2 3 4 5"
44+
val yInput = "1 4 9 16 25"
45+
46+
lifecycleScope.launch {
47+
try {
48+
val bytes = plotModule.callAttr(
49+
"plot",
50+
xInput,
51+
yInput
52+
).toJava(ByteArray::class.java)
53+
withContext(Dispatchers.IO) {
54+
val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
55+
binding.imageHome.setImageBitmap(bitmap)
56+
}
57+
} catch (e: PyException) {
58+
binding.textHome.text = e.message
59+
}
60+
}
61+
62+
return binding.root
63+
}
64+
65+
override fun onDestroyView() {
66+
super.onDestroyView()
67+
_binding = null
68+
}
69+
}

0 commit comments

Comments
 (0)