Skip to content

Commit

Permalink
🦄 refactor: Convert to kotlin for android
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 25, 2023
1 parent ffe9480 commit 3fbdd5f
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 83 deletions.
3 changes: 2 additions & 1 deletion android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ dependencies {
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("com.caoccao.javet:javet-android:3.0.2")
// implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.aar"))))
testImplementation("junit:junit:4.13.2")
testImplementation("junit:junit:5.10.1")
testImplementation("org.jetbrains.kotlin:kotlin-test:1.9.21")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.caoccao.javet.shell

package com.caoccao.javet.shell;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
* @see [Testing documentation](http://d.android.com/tools/testing)
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
public void useAppContext() {
fun useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.caoccao.javet.shell", appContext.getPackageName());
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
Assert.assertEquals("com.caoccao.javet.shell", appContext.packageName)
}
}

This file was deleted.

52 changes: 52 additions & 0 deletions android/app/src/main/java/com/caoccao/javet/shell/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2023. caoccao.com Sam Cao
*
* 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.
*/
package com.caoccao.javet.shell

import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.caoccao.javet.exceptions.JavetException
import com.caoccao.javet.interop.V8Host
import com.caoccao.javet.interop.V8Runtime
import com.caoccao.javet.interop.loader.JavetLibLoader
import com.caoccao.javet.utils.JavetOSUtils

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView = findViewById<TextView>(R.id.textView)
val sb = StringBuilder()
sb.append("Javet is Java + V8 (JAVa + V + EighT). It is an awesome way of embedding Node.js and V8 in Java.\n\n")
sb.append("Javet v").append(JavetLibLoader.LIB_VERSION).append(" supports Android (arm, arm64, x86 and x86_64) ABI >= 24.\n\n")
try {
V8Host.getV8Instance().createV8Runtime<V8Runtime>().use { v8Runtime ->
sb.append(v8Runtime.getExecutor("'Hello Javet'").executeString()).append("\n")
sb.append("OS Name = ").append(JavetOSUtils.OS_NAME).append("\n")
sb.append("OS Arch = ").append(JavetOSUtils.OS_ARCH).append("\n")
sb.append("V8 = ").append(v8Runtime.version).append("\n")
sb.append("Now = ").append(v8Runtime.getExecutor("new Date()").executeZonedDateTime()).append("\n")
}
} catch (e: JavetException) {
sb.append(e.message).append("\n")
sb.append("Error Code = ").append(e.error.code).append("\n")
if (e.cause != null) {
sb.append(e.cause!!.message).append("\n")
}
}
textView.text = sb.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.caoccao.javet.shell

package com.caoccao.javet.shell;

import org.junit.Test;

import static org.junit.Assert.*;
import org.junit.Assert
import org.junit.Test

/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
* @see [Testing documentation](http://d.android.com/tools/testing)
*/
public class ExampleUnitTest {
class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
fun addition_isCorrect() {
Assert.assertEquals(4, (2 + 2).toLong())
}
}
2 changes: 1 addition & 1 deletion console/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ version = "0.1.0"

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.10")
implementation("com.caoccao.javet:javet:3.0.1")
implementation("com.caoccao.javet:javet:3.0.2")

// https://github.com/Kotlin/kotlinx-cli
// https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-cli-jvm
Expand Down

0 comments on commit 3fbdd5f

Please sign in to comment.