Skip to content

Commit

Permalink
add net library
Browse files Browse the repository at this point in the history
  • Loading branch information
loongwind committed Aug 19, 2022
1 parent 169aaec commit 18095f3
Show file tree
Hide file tree
Showing 27 changed files with 597 additions and 9 deletions.
8 changes: 5 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ android {

dependencies {

implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation "androidx.core:core-ktx:${versions.core_ktx}"
implementation "androidx.appcompat:appcompat:${versions.appcompat}"
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation "androidx.constraintlayout:constraintlayout:${versions.constraintlayout}"
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.0'
implementation 'com.loongwind.ardf:recyclerview-ext:1.0.1'
api "com.squareup.okhttp3:okhttp:4.9.3"
// implementation 'com.loongwind.ardf:base:1.0.1'
// implementation project(path: ':recyclerview')
implementation project(path: ':base')
implementation project(path: ':net')

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidFramework"
android:networkSecurityConfig="@xml/network_security_config"
tools:targetApi="31" >
<activity
android:name=".MainActivity"
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/loongwind/ardf/demo/ApiService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.loongwind.ardf.demo

import retrofit2.http.GET

interface ApiService {

@GET("user")
suspend fun getUser():User
}
2 changes: 2 additions & 0 deletions app/src/main/java/com/loongwind/ardf/demo/App.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.loongwind.ardf.demo

import android.app.Application
import com.loongwind.ardf.net.retrofitModule
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.androidx.viewmodel.dsl.viewModel
Expand All @@ -16,6 +17,7 @@ class App : Application() {
androidLogger()
androidContext(this@App)
modules(appModule)
modules(retrofitModule)
}
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/com/loongwind/ardf/demo/AppModule.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package com.loongwind.ardf.demo

import com.loongwind.ardf.base.BaseViewModel
import com.loongwind.ardf.net.ARDF_BASE_URL
import com.loongwind.ardf.net.ARDF_DEBUG
import okhttp3.Interceptor
import org.koin.android.ext.android.getKoin
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.core.definition.Definition
import org.koin.core.module.KoinDefinition
import org.koin.core.module.Module
import org.koin.core.module._singleInstanceFactory
import org.koin.core.qualifier.Qualifier
import org.koin.core.qualifier.named
import org.koin.dsl.module
import retrofit2.Retrofit

val appModule = module {

Expand All @@ -13,4 +25,14 @@ val appModule = module {
viewModel{ MultiItemViewModel()}
viewModel{ MainViewModel()}
viewModel{ TestViewModel()}

single(named(ARDF_BASE_URL)) {
"https://www.fastmock.site/mock/6d5084df89b4c7a49b28052a0f51c29a/test/"
}

single(named(ARDF_DEBUG)) { true }

single {
get<Retrofit>().create(ApiService::class.java)
}
}
18 changes: 18 additions & 0 deletions app/src/main/java/com/loongwind/ardf/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,35 @@ package com.loongwind.ardf.demo

import android.content.Intent
import com.loongwind.ardf.base.BaseBindingViewModelActivity
import com.loongwind.ardf.base.ext.toast
import com.loongwind.ardf.demo.databinding.ActivityMainBinding
import com.loongwind.ardf.net.request
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.koin.android.ext.android.inject

class MainActivity : BaseBindingViewModelActivity<ActivityMainBinding, MainViewModel>() {

val apiService : ApiService by inject()

override fun onEvent(eventId: Int) {
super.onEvent(eventId)
when(eventId){
MainViewModel.EVENT_SIMPLE -> startActivity(Intent(this, RecycleViewSimpleActivity::class.java))
MainViewModel.EVENT_ITEM_CLICK -> startActivity(Intent(this, RecycleViewSimpleItemClickActivity::class.java))
MainViewModel.EVENT_ITEM_EVENT -> startActivity(Intent(this, RecycleViewSimpleItemEventActivity::class.java))
MainViewModel.EVENT_MULTI_ITEM_VIEW -> startActivity(Intent(this, RecycleViewSimpleMultiItemViewActivity::class.java))
MainViewModel.EVENT_API -> requestApi()
}

}

private fun requestApi(){
request{
val user = apiService.getUser()
toast(user.name)
}

}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/loongwind/ardf/demo/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class MainViewModel : BaseViewModel() {
const val EVENT_ITEM_CLICK = 0x01
const val EVENT_ITEM_EVENT = 0x02
const val EVENT_MULTI_ITEM_VIEW = 0x03
const val EVENT_API = 0x04
}


Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@
android:text="多类型item view"
android:onClick="@{()->vm.onClick(MainViewModel.EVENT_MULTI_ITEM_VIEW)}"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="@+id/api"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="api请求"
android:onClick="@{()->vm.onClick(MainViewModel.EVENT_API)}"
app:layout_constraintTop_toTopOf="parent"/>


</LinearLayout>
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/xml/network_security_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
10 changes: 5 additions & 5 deletions base/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ android {

dependencies {

implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
api "io.insert-koin:koin-core:3.2.0"
api "io.insert-koin:koin-android:3.2.0"
api 'pub.devrel:easypermissions:3.0.0'
implementation "androidx.core:core-ktx:${versions.core_ktx}"
implementation "androidx.appcompat:appcompat:${versions.appcompat}"
api "io.insert-koin:koin-core:${versions.koin}"
api "io.insert-koin:koin-android:${versions.koin}"
api "pub.devrel:easypermissions:${versions.easypermissions}"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ plugins {
id 'org.jetbrains.kotlin.android' version '1.7.0' apply false
}

apply from: 'versions.gradle'

task clean(type: Delete) {
delete rootProject.buildDir
}
1 change: 1 addition & 0 deletions net/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
154 changes: 154 additions & 0 deletions net/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'maven-publish'
id 'signing'
}

android {
compileSdk 32

defaultConfig {
minSdk 23
targetSdk 32
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {

implementation "androidx.core:core-ktx:${versions.core_ktx}"
implementation "androidx.appcompat:appcompat:${versions.appcompat}"
api "io.insert-koin:koin-core:${versions.koin}"
api "com.squareup.okhttp3:okhttp:${versions.okhttp}"
api "com.squareup.okhttp3:logging-interceptor:$versions.okhttp"
api "com.squareup.retrofit2:retrofit:$versions.retrofit"
api "com.squareup.retrofit2:converter-gson:$versions.retrofit"
api "com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:$versions.retrofit_coroutines"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}




task androidSourcesJar(type: Jar) {
archiveClassifier.set('sources')
from android.sourceSets.main.java.source
}

task javadocs(type: Javadoc) {
source = 'src/main/java/null'
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
excludes = ['**/*.kt']
}

task javadocJar(type: Jar, dependsOn: javadocs) {
archiveClassifier.set('javadoc')
from javadocs.destinationDir
}

artifacts {
archives androidSourcesJar
archives javadocJar
}


Properties properties = new Properties()
properties.load(project.rootProject.file('maven.properties').newDataInputStream())

publishing {
publications {
maven(MavenPublication) {
groupId properties.getProperty('GROUP_ID') //开通maven central时候定义的
artifactId 'net' //资源名称
version properties.getProperty('VERSION_NAME') //版本名称
pom {
name = 'net'
description = 'net framework'
url = properties.getProperty('POM_URL')
inceptionYear = properties.getProperty('POM_INCEPTION_YEAR')

scm {
url = properties.getProperty('POM_URL')
connection = properties.getProperty('POM_SCM_CONNECTION')
developerConnection = properties.getProperty('POM_SCM_DEV_CONNECTION')
}

licenses {
license {
name = properties.getProperty('POM_LICENCE_NAME')
url = properties.getProperty('POM_LICENCE_URL')
distribution = properties.getProperty('POM_LICENCE_DIST')
comments = properties.getProperty('POM_LICENCE_COMMENTS')
}
}

developers {
developer {
id = properties.getProperty('POM_DEVELOPER_ID')
name = properties.getProperty('POM_DEVELOPER_NAME')
email = properties.getProperty('POM_DEVELOPER_EMAIL')
url = properties.getProperty('POM_DEVELOPER_URL')
}
}

issueManagement {
system = properties.getProperty('POM_ISSUE_MANAGEMENT_SYSTEM')
url = properties.getProperty('POM_ISSUE_MANAGEMENT_URL')
}
}
// pom文件中声明依赖,从而传递到使用方
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.implementation.allDependencies.each {
// 避免出现空节点或 artifactId=unspecified 的节点
if (it.group != null && (it.name != null && "unspecified" != it.name) && it.version != null) {
println "dependency=${it.toString()}"
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
dependencyNode.appendNode('scope', 'implementation')
}
}
}
artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
artifact androidSourcesJar
artifact javadocJar
}
}
repositories {
maven {
def snapshotsUrl = properties.getProperty('MAVEN_SNAPSHOTS_URL')
def releaseUrl = properties.getProperty('MAVEN_RELEASE_URL')
url = properties.getProperty('VERSION_NAME').endsWith('SNAPSHOT') ? snapshotsUrl : releaseUrl
credentials {
username sonatypeUsername
password sonatypePassword
}
}
}
}
signing {
sign publishing.publications.maven //签名配置
}
21 changes: 21 additions & 0 deletions net/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.loongwind.ardf.net

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

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

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.loongwind.ardf.net", appContext.packageName)
}
}
Loading

0 comments on commit 18095f3

Please sign in to comment.