Skip to content

Commit 3c90219

Browse files
committed
save project as an minimal example
1 parent a59e6ab commit 3c90219

File tree

10 files changed

+189
-0
lines changed

10 files changed

+189
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build
2+
.gradle
3+
gradle/
4+
gradlew
5+
gradlew.bat
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# App for sending curl requests to localhost
2+
## preparations
3+
### java JDK
4+
```sh
5+
# switch to java 17 (`sdk install java 17.0.17-zulu`)
6+
java -version
7+
```
8+
### android command line tools
9+
```sh
10+
mkdir /home/soft/android_sdk
11+
pushd /home/soft/android_sdk
12+
curl --silent https://developer.android.com/studio | grep commandlinetools-linux | grep href
13+
wget https://dl.google.com/android/repository/commandlinetools-linux-13114758_latest.zip
14+
unzip commandlinetools-linux-*.zip
15+
```
16+
```sh
17+
pushd /home/soft/android_sdk
18+
# accept all licenses, the root should be the same as in local.properties
19+
./cmdline-tools/bin/sdkmanager --sdk_root=/home/soft/android_sdk --licenses
20+
```
21+
```sh
22+
pushd /home/soft/android_sdk
23+
./cmdline-tools/bin/sdkmanager --sdk_root=/home/soft/android_sdk --install "platform-tools" "platforms;android-34" "build-tools;34.0.0"
24+
# check with app/build.gradle `android { compileSdk 34
25+
```
26+
27+
### android vs gradle
28+
> Android Gradle Plugin 8.2.2 is not compatible with Gradle 9.x
29+
```sh
30+
gradle wrapper --gradle-version 8.3 --distribution-type all
31+
./gradlew --version
32+
```
33+
34+
## build
35+
```sh
36+
rm -rf build .gradle app/build app/.gradle
37+
gradle --stop
38+
gradle --status
39+
./gradlew --stop
40+
./gradlew --status
41+
42+
./gradlew clean :app:assembleDebug --no-build-cache
43+
ls -la app/build/outputs/apk/debug/app-debug.apk
44+
```
45+
46+
## install application on physical android device
47+
```sh
48+
adb devices
49+
# if no devices: https://github.com/cherkavi/cheat-sheet/blob/master/android-cheat-sheet.md#adb
50+
adb install -r app/build/outputs/apk/debug/app-debug.apk
51+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
namespace 'com.example.buttonapp'
5+
compileSdk 34
6+
defaultConfig {
7+
applicationId "com.example.buttonapp"
8+
minSdk 24
9+
targetSdk 34
10+
}
11+
}
12+
13+
dependencies {
14+
implementation 'androidx.appcompat:appcompat:1.6.1'
15+
implementation 'com.squareup.okhttp3:okhttp:4.12.0'
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<uses-permission android:name="android.permission.INTERNET" />
5+
6+
<application
7+
android:allowBackup="true"
8+
android:label="Button App"
9+
android:usesCleartextTraffic="true"
10+
android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
11+
<activity
12+
android:name=".MainActivity"
13+
android:exported="true">
14+
<intent-filter>
15+
<action android:name="android.intent.action.MAIN" />
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
</manifest>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.example.buttonapp;
2+
3+
import android.os.Bundle;
4+
import android.widget.Button;
5+
import android.widget.Toast;
6+
import androidx.appcompat.app.AppCompatActivity;
7+
import java.io.IOException;
8+
import okhttp3.Call;
9+
import okhttp3.Callback;
10+
import okhttp3.OkHttpClient;
11+
import okhttp3.Request;
12+
import okhttp3.Response;
13+
14+
public class MainActivity extends AppCompatActivity {
15+
16+
private final OkHttpClient client = new OkHttpClient();
17+
// 10.0.2.2 is how the Android Emulator sees your computer's localhost
18+
private final String SERVER_URL = "http://10.0.2.2:8080/";
19+
20+
@Override
21+
protected void onCreate(Bundle savedInstanceState) {
22+
super.onCreate(savedInstanceState);
23+
setContentView(R.layout.activity_main);
24+
25+
Button btnOne = findViewById(R.id.btn_one);
26+
Button btnTwo = findViewById(R.id.btn_two);
27+
28+
btnOne.setOnClickListener(v -> makeRequest("button1"));
29+
btnTwo.setOnClickListener(v -> makeRequest("button2"));
30+
}
31+
32+
private void makeRequest(String path) {
33+
Request request = new Request.Builder()
34+
.url(SERVER_URL + path)
35+
.build();
36+
37+
client.newCall(request).enqueue(new Callback() {
38+
@Override
39+
public void onFailure(Call call, IOException e) {
40+
runOnUiThread(() ->
41+
Toast.makeText(MainActivity.this, "Network Error: " + e.getMessage(), Toast.LENGTH_LONG).show());
42+
}
43+
44+
@Override
45+
public void onResponse(Call call, Response response) throws IOException {
46+
final String responseData = response.isSuccessful() ? "Success!" : "Server Error: " + response.code();
47+
runOnUiThread(() ->
48+
Toast.makeText(MainActivity.this, responseData, Toast.LENGTH_SHORT).show());
49+
}
50+
});
51+
}
52+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical"
6+
android:gravity="center"
7+
android:padding="20dp">
8+
9+
<Button
10+
android:id="@+id/btn_one"
11+
android:layout_width="match_parent"
12+
android:layout_height="60dp"
13+
android:text="Send to /button1"
14+
android:backgroundTint="#2196F3"/>
15+
16+
<Button
17+
android:id="@+id/btn_two"
18+
android:layout_width="match_parent"
19+
android:layout_height="60dp"
20+
android:layout_marginTop="20dp"
21+
android:text="Send to /button2"
22+
android:backgroundTint="#4CAF50"/>
23+
24+
</LinearLayout>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
buildscript {
2+
repositories {
3+
google()
4+
mavenCentral()
5+
}
6+
dependencies {
7+
classpath 'com.android.tools.build:gradle:8.2.2'
8+
}
9+
}
10+
11+
allprojects {
12+
repositories {
13+
google()
14+
mavenCentral()
15+
}
16+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
android.useAndroidX=true
2+
android.enableJetifier=true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sdk.dir=/home/soft/android_sdk
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rootProject.name = "MyButtonApp"
2+
include ':app'

0 commit comments

Comments
 (0)