Skip to content

Commit

Permalink
Merge pull request #79 from icyleaf/feat/androis-native-code
Browse files Browse the repository at this point in the history
Add native codes support for Android
  • Loading branch information
icyleaf authored Nov 27, 2024
2 parents 4e5257e + fc68376 commit 3a7d4c0
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 6 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ jobs:
fail-fast: false
matrix:
ruby: [ ruby-3.1, ruby-3.2, ruby-3.3 ]
experimental: [false]
experimental: [ false ]
include:
- ruby: head
experimental: true
- ruby: truffleruby-head
experimental: true
steps:
- uses: actions/checkout@v4

Expand Down
16 changes: 13 additions & 3 deletions lib/app_info/aab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,33 @@ def components
@components ||= manifest.components.transform_values
end

# @param [String] base_path
# @return [Array<String>]
def native_codes(base_path: BASE_PATH)
@native_codes ||= zip.glob(::File.join(base_path, 'lib/**/*'))
.each_with_object([]) do |entry, obj|
lib = entry.name.split('/')[2]
obj << lib unless obj.include?(lib)
end
end

def each_file
zip.each do |entry|
next unless entry.file?

yield entry.name, @zip.read(entry)
yield entry.name, zip.read(entry)
end
end

def read_file(name, base_path: BASE_PATH)
content = @zip.read(entry(name, base_path: base_path))
content = zip.read(entry(name, base_path: base_path))
return parse_binary_xml(content) if xml_file?(name)

content
end

def entry(name, base_path: BASE_PATH)
entry = @zip.find_entry(::File.join(base_path, name))
entry = zip.find_entry(::File.join(base_path, name))
raise NotFoundError, "'#{name}'" if entry.nil?

entry
Expand Down
5 changes: 5 additions & 0 deletions lib/app_info/android.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ def resource
not_implemented_error!(__method__)
end

# @abstract Subclass and override {#native_codes} to implement.
def native_codes
not_implemented_error!(__method__)
end

# @abstract Subclass and override {#zip} to implement.
def zip
not_implemented_error!(__method__)
Expand Down
8 changes: 8 additions & 0 deletions lib/app_info/apk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ def activities
components.select { |c| c.type == 'activity' }
end

# @return [Array<String>]
def native_codes
@native_codes ||= zip.glob('lib/**/*').each_with_object([]) do |entry, obj|
lib = entry.name.split('/')[1]
obj << lib unless obj.include?(lib)
end
end

# @return [::Android::Apk]
def apk
@apk ||= ::Android::Apk.new(@file)
Expand Down
9 changes: 9 additions & 0 deletions spec/app_info/aab_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
it { expect(subject.manifest.label).to eq('AppInfoDemo') }
it { expect(subject.manifest.label(locale: 'en')).to eq('AppInfoDemo') }
it { expect(subject.manifest.label(locale: 'zh-CN')).to eq('AppInfo演示') }
it { expect(subject.native_codes).to be_empty }

it { expect(subject.signs).to be_kind_of(Hash) }
it { expect(subject.signs).to have_key('META-INF/KEY0.RSA') }
Expand Down Expand Up @@ -82,6 +83,7 @@
it { expect(subject.manifest.label).to eq('My Application') }
it { expect(subject.manifest.label(locale: 'en')).to eq('My Application') }
it { expect(subject.manifest.label(locale: 'zh-CN')).to eq('My Application') }
it { expect(subject.native_codes).to be_empty }
it { expect(subject.certificates).to be_empty }
it { expect(subject.signs).to be_empty }

Expand Down Expand Up @@ -109,5 +111,12 @@
expect(icons.size).to eq(0)
end
end

context 'with Native codes' do
let(:file) { fixture_path('apps/android-native-code.aab') }

it { expect(subject.native_codes.size).to eq(7) }
it { expect(subject.native_codes).to contain_exactly('arm64-v8a', 'armeabi', 'armeabi-v7a', 'mips', 'mips64', 'x86', 'x86_64') }
end
end
end
1 change: 1 addition & 0 deletions spec/app_info/android_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
it { expect { subject.services }.to raise_error NotImplementedError }
it { expect { subject.components }.to raise_error NotImplementedError }
it { expect { subject.manifest }.to raise_error NotImplementedError }
it { expect { subject.native_codes }.to raise_error NotImplementedError }
it { expect { subject.resource }.to raise_error NotImplementedError }
it { expect { subject.zip }.to raise_error NotImplementedError }
it { expect { subject.clear! }.to raise_error NotImplementedError }
Expand Down
11 changes: 11 additions & 0 deletions spec/app_info/apk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
it { expect(subject.manifest.use_permissions).to eq(subject.use_permissions) }
it { expect(subject.deep_links).to eq(['icyleaf.com']) }
it { expect(subject.schemes).to eq(['appinfo']) }
it { expect(subject.native_codes).to be_empty }

# TODO: it will remove soon.
it { expect(subject.certificates).to be_kind_of(Array) }
Expand Down Expand Up @@ -71,6 +72,13 @@
expect(icons.size).to eq(0)
end
end

context 'with Native codes' do
let(:file) { fixture_path('apps/android-native-code.apk') }

it { expect(subject.native_codes.size).to eq(7) }
it { expect(subject.native_codes).to contain_exactly('arm64-v8a', 'armeabi', 'armeabi-v7a', 'mips', 'mips64', 'x86', 'x86_64') }
end
end

describe '#Wear' do
Expand Down Expand Up @@ -101,6 +109,7 @@
it { expect(subject.icons.length).not_to be_nil }
it { expect(subject.min_sdk_version).to eq 21 }
it { expect(subject.target_sdk_version).to eq 23 }
it { expect(subject.native_codes).to be_empty }
end

describe '#TV' do
Expand Down Expand Up @@ -131,6 +140,7 @@
it { expect(subject.icons.length).not_to be_nil }
it { expect(subject.min_sdk_version).to eq 23 }
it { expect(subject.target_sdk_version).to eq 23 }
it { expect(subject.native_codes).to be_empty }
end

describe '#Automotive' do
Expand Down Expand Up @@ -161,5 +171,6 @@
it { expect(subject.icons.length).not_to be_nil }
it { expect(subject.min_sdk_version).to eq 29 }
it { expect(subject.target_sdk_version).to eq 31 }
it { expect(subject.native_codes).to be_empty }
end
end
2 changes: 2 additions & 0 deletions spec/app_info_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
'android-v1-v2-v3-signed.apk' => :apk,
'android-v2-signed-only.apk' => :apk,
'android-v3-signed-only.apk' => :apk,
'android-native-code.apk' => :apk,
'android-native-code.aab' => :aab,
'tv.apk' => :apk,
'wear.apk' => :apk,
'automotive.apk' => :apk,
Expand Down
Binary file added spec/fixtures/apps/android-native-code.aab
Binary file not shown.
Binary file added spec/fixtures/apps/android-native-code.apk
Binary file not shown.

0 comments on commit 3a7d4c0

Please sign in to comment.