Issue: I have two classes MainActivity and BitcoinConfig (see below). Can someone please help me fix issues I'm having syncing the blockchain?
The AndroidManifest.xml and Build.gradle files are also below.
Errors:
I’ve attached the full logcat with the getCause info because it was hard to read on this but one of the errors is:
W/System.err: java.lang.IllegalStateException: Expected the service [FAILED] to be RUNNING, but the service has FAILED (line 50 kit.awaitSync())
Thanks (sorry if this is obvious)!
MAIN ACTIVITY
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private BitcoinConfig btcService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btcService = new BitcoinConfig();
btcService.settingUp();
}
}
import android.util.Log;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.listeners.DownloadProgressTracker;
import org.bitcoinj.kits.WalletAppKit;
import org.bitcoinj.params.RegTestParams;
import org.bitcoinj.params.TestNet3Params;
import org.bitcoinj.utils.BriefLogFormatter;
import java.io.File;
import java.util.Date;
public class BitcoinConfig {
private NetworkParameters params;
private WalletAppKit kit;
private File walletPath;
private static final String TAG = BitcoinConfig.class.getSimpleName();
public BitcoinConfig() {
}
public void settingUp() {
params = TestNet3Params.get();
Log.d(TAG, "getting network");
BriefLogFormatter.init();
Log.d(TAG, "creating wallet");
kit = new WalletAppKit(params, new File("/home/annab"), "UserWallet") {
@Override
protected void onSetupCompleted() {
if (wallet().getImportedKeys().size() < 1)
wallet().importKey(new ECKey());
Log.d(TAG, "in on set up");
}
};
if (params == RegTestParams.get()) {
kit.connectToLocalHost();
Log.d(TAG, "in connect to regtestnet");
}
kit.setBlockingStartup(false);
Log.d(TAG, "setBlockingStartup(false)");
try {
kit.startAsync();
Log.d(TAG, "startAsync()");
} catch(Throwable t){
Log.d(TAG, "startAsync error"); // never goes here
t.getCause();
}
try {
kit.awaitRunning(); // error: java.lang.IllegalStateException: Expected the service [FAILED] to be RUNNING, but the service has FAILED
Log.d(TAG, "awaitRunning()");
} catch (Throwable t) {
System.out.println("awaitingRunning exception"); // goes here
t.getCause();
}
}
}
build.gradle(:module)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.0"
}
}
allprojects {
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle(:app)
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 31
buildToolsVersion "31.0.0"
defaultConfig {
applicationId "com.example.walletexample"
minSdkVersion 23
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
// already here
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation "androidx.annotation:annotation:1.2.0"
// BitcoinJ SDK
implementation 'org.bitcoinj:bitcoinj-core:0.15.10'
// logging
implementation 'org.slf4j:slf4j-api:1.7.12'
implementation 'org.slf4j:slf4j-simple:1.7.12'
implementation 'com.google.guava:guava:29.0-android'
testImplementation 'junit:junit:4.13.2'
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.walletexample">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:exported ="true"
android:theme="@style/Theme.WalletExample">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>